forked from 1b-agent/opencadstudio-upstream
Add ocs_doc_api: object-oriented CAD entity API (v2)
New crate crates/ocs_doc_api: object-centric facade (DocApi -> Document -> collections -> typed handles), transport-agnostic wire layer (DocApiEnvelope = single op | read-only query batch), per-op autocommit (no write transactions), bulk ops with upfront-validation all-or-nothing, structured ApiError, and build-time codegen (spec/entities.toml -> ops/query enums, api_reference.md, binding_schema.json). ocs_plugin_api: append-only PluginRequest::DocApiRequest / PluginResponse:: DocApiResponse (serde_bytes payload) + defaulted HostApi::doc_api_dispatch. Host: DocApiBackend impl for HostSession (src/app/doc_api.rs) mapping to scene primitives (solid_models lift-on-miss, register_prepared_solid_model, meshes metrics), entity conversions (doc_api_convert.rs), HostSession accessors + doc_api_dispatch override. Tests: 592 host lib (incl. 7 DocApi integration tests: create/boolean/query, unknown-id, transform-many all-or-nothing, query-batch cap, 2D/geometric/DWG roundtrips), 8 crate unit tests. Examples + README/ARCHITECTURE/IMPLEMENTATION docs.
This commit is contained in:
1 parent
9129c85f97
commit
e41086b058
44 files changed
+4837
-12
No files matched your search
@@ -0,0 +1,59 @@
|
||||
//! Example 5 — Failure semantics: atomicity, OpGroup compensation, revision guard.
|
||||
//!
|
||||
//! Run: `cargo run --example e5_failure_and_guards --features host`
|
||||
//!
|
||||
//! Shows: per-op atomicity (a failed op applies nothing and moves no revision),
|
||||
//! `OpGroup` best-effort cleanup, `ApiError` handling, and the `assert_revision`
|
||||
//! read-guard.
|
||||
|
||||
mod common;
|
||||
|
||||
use ocs_doc_api::{ApiError, DocApi, HasId, OpGroup, ObjectId};
|
||||
|
||||
fn main() -> ocs_doc_api::ApiResult<()> {
|
||||
let api = DocApi::in_process(common::MockBackend::default(), 0);
|
||||
let doc = api.document(api.active_tab());
|
||||
|
||||
// ── Per-op atomicity: a boolean against a deleted id fails cleanly ──
|
||||
let a = doc.solids().create_cuboid([0.0, 0.0, 0.0], [10.0; 3])?;
|
||||
let ghost = doc.solids().create_sphere([5.0, 5.0, 5.0], 4.0)?;
|
||||
ghost.delete()?; // now a stale handle
|
||||
|
||||
let rev_before = doc.revision()?;
|
||||
match a.intersect(&ghost) {
|
||||
Err(ApiError::UnknownId(id)) => println!("boolean failed as expected: unknown id {id:?}"),
|
||||
Err(e) => println!("boolean failed: {e}"),
|
||||
Ok(_) => panic!("expected the boolean to fail"),
|
||||
}
|
||||
// `a` is still live (failed op mutated nothing) and the revision did not move.
|
||||
assert!(a.bounds().is_ok(), "input solid survives a failed boolean");
|
||||
assert_eq!(doc.revision()?, rev_before, "failed op records no revision bump");
|
||||
|
||||
// ── OpGroup: clean up a partially-built logical operation on failure ──
|
||||
let mut grp = OpGroup::new();
|
||||
let p1 = grp.track(doc.solids().create_cuboid([0.0, 0.0, 0.0], [4.0; 3]))?;
|
||||
let p2 = grp.track(doc.solids().create_sphere([2.0, 2.0, 2.0], 3.0))?;
|
||||
// Simulate a downstream failure: abandon the group and delete what we made.
|
||||
grp.compensate(&doc)?;
|
||||
for h in [p1.id(), p2.id()] {
|
||||
assert!(doc.entities().get(h).is_err(), "compensated entity is gone");
|
||||
}
|
||||
println!("OpGroup compensated: created entities deleted");
|
||||
|
||||
// ── assert_revision: read-modify-write guard ──
|
||||
let rev = doc.revision()?;
|
||||
doc.assert_revision(rev)?; // unchanged -> ok
|
||||
doc.solids().create_cuboid([0.0, 0.0, 0.0], [1.0; 3])?; // moves the revision
|
||||
match doc.assert_revision(rev) {
|
||||
Err(ApiError::Validation { reason, .. }) => println!("revision guard caught: {reason}"),
|
||||
other => panic!("expected the revision guard to fail, got {other:?}"),
|
||||
}
|
||||
|
||||
// A totally unknown id also surfaces a structured error.
|
||||
assert!(matches!(
|
||||
doc.entities().get(ObjectId::from_u64(0xDEAD)),
|
||||
Err(_)
|
||||
));
|
||||
println!("failure semantics verified");
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user