Files
OneCAD-Draft/crates/ocs_doc_api/src/envelope.rs
T
Hakan Seven 3385160493 Merge PR #1028: add document API with atomic edits
Add the typed document facade and plugin IPC adapter. Prepare bulk edits
before mutation, preserve undo and dirty state, keep profile elevations,
validate numeric inputs and session bindings, and generate Python constructors
with typed results.

Reuse existing entity transforms and profile conversion. Compute mass
properties and closed-surface bounds in cadkernel; pin its main revision
6a5e6fc2822f6af6b2611cd605b44437b35158f0. Keep plugin API metadata at version 6.

Validation: 646 native application tests, 597 kernel tests, 12 document API
tests, 91 plugin API tests, 3 Python tests, default/IPC feature builds and
wasm32 check passed.
2026-09-06 21:43:02 +03:00

94 lines
3.0 KiB
Rust

//! The wire envelope. One request = ONE write op OR a query batch
//! (read-only batching is safe: no mutation, no undo, no rollback).
use serde::{Deserialize, Serialize};
use crate::gen::{Operation, Query};
use crate::id::ObjectId;
use crate::query::QueryResult;
use crate::revision::GeometryRevision;
use crate::ENVELOPE_VERSION;
/// The single request unit crossing the transport.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DocApiEnvelope {
/// Crate protocol version (`ENVELOPE_VERSION`). Bridges must check this.
pub version: u16,
pub body: EnvelopeBody,
}
impl DocApiEnvelope {
pub fn validate_version(&self) -> crate::ApiResult<()> {
if self.version != ENVELOPE_VERSION {
return Err(crate::ApiError::Unsupported(format!(
"envelope version {}",
self.version
)));
}
Ok(())
}
pub fn op(operation: Operation) -> Self {
Self {
version: ENVELOPE_VERSION,
body: EnvelopeBody::Op(operation),
}
}
pub fn queries(queries: Vec<Query>) -> Self {
Self {
version: ENVELOPE_VERSION,
body: EnvelopeBody::Queries(queries),
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum EnvelopeBody {
/// Exactly ONE write op (write batching removed, plan §13). Atomic by itself.
Op(Operation),
/// One-or-more read-only queries answered together (safe: no mutation/undo).
Queries(Vec<Query>),
}
/// The outcome of an applied write op.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum OpOutcome {
/// A new entity was created; carries its fresh `ObjectId`.
NewId(ObjectId),
/// An existing entity was updated in place (same `ObjectId` preserved).
Updated(ObjectId),
/// Entities were deleted; carries the deleted ids (one for `Delete`, N for `DeleteMany`).
Deleted(Vec<ObjectId>),
/// Many entities were created (bulk `CreateMany`); carries all fresh ids in order.
NewIds(Vec<ObjectId>),
}
impl OpOutcome {
/// The single fresh id, for ops that create exactly one entity.
pub fn new_id(&self) -> Option<ObjectId> {
match self {
OpOutcome::NewId(id) => Some(*id),
_ => None,
}
}
/// All fresh ids, for bulk creates.
pub fn new_ids(&self) -> &[ObjectId] {
match self {
OpOutcome::NewIds(ids) => ids,
OpOutcome::NewId(id) => std::slice::from_ref(id),
_ => &[],
}
}
}
/// The per-request result (and the query-batch answer carrier).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Receipt {
/// Present for `EnvelopeBody::Op`; absent for pure queries.
pub outcome: Option<OpOutcome>,
/// One `QueryResult` per `Query` in a `Queries` batch (in order).
pub query_results: Vec<QueryResult>,
/// The geometry revision after this request (== before, for pure queries).
pub new_revision: GeometryRevision,
}