forked from 1b-agent/opencadstudio-upstream
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.
64 lines
2.6 KiB
Rust
64 lines
2.6 KiB
Rust
//! 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, ObjectId, OpGroup};
|
|
|
|
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(())
|
|
}
|