Files
makeCAD/crates/engine/src/schema/mod.rs
T
Matthew Knutson 453c6bcb14 Split schema contracts per command file and add shared field types (#2829) (#3558)
* Split the schema contracts into one module per command file

`schema/commands.rs` becomes `schema/features.rs` (solid.box, solid.extrude,
FeatureResult) and `schema/inspect.rs` (engine.describe), each with its own
tests file holding its `schemas_and_the_key_check_agree` key lists and its
`strict_keeps_todays_errors` cases, so conversions of different command files
touch different files. The shared loops become helpers in `schema/tests.rs`;
the assertions are unchanged. `engine.describe` gains a key-list test, and
`schema_defaults_are_valid` now covers every command with a contract.

No behaviour change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Add shared schema field types for the next conversions

For the feature commands to be converted next, each deciding acceptance with
the reader the handlers already use:

- `FaceRef` and `FaceRefs` (`face::face_ref`, `features::face_points`)
- `EdgeRef` and `EdgeRefs` (`features::edge_points`)
- `SketchRef` (`features::sketch_id`)
- `FeatureCommon`: `name`, `body_name`, `body_names` and `participants`,
  which `add_feature` reads for every feature, flattened into a contract
  with `deserialize_with = "feature_common"`, which keeps the key in the
  message and the issue's path.

The shape checks of `face_ref`, `edge_points` and `sketch_id` (and
`feature_sketch`) move into pure helpers (`face_ref_form`, `edge_ref_form`,
`sketch_key`) that the handlers and the types share; the handlers behave as
before. Tests check that each type and its schema accept what the reader
accepts (apart from a face name's byte cap and an index written `1.0`),
that the contract's messages match the handlers' errors, and that the
handlers still look picks up one at a time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 00:54:13 +00:00

65 lines
3.4 KiB
Rust

//! Typed parameter contracts for commands.
//!
//! A converted command declares a [`Contract`]: a Rust type its parameters must deserialize
//! into, from which the input JSON Schema is generated. The contract is checked *in front of*
//! the unchanged handler, which still reads the raw parameters, so a command's behaviour does
//! not change unless the session's [`ParamCheck`] level asks for warnings or refusals.
//!
//! - **Off** (the default): nothing is parsed or checked; the handler runs as before.
//! - **Warn**: the handler runs; a successful object result gains `param_warnings`.
//! - **Strict**: a call with an unknown key or a parameter of the wrong shape is refused, and
//! nothing runs.
//!
//! Unknown keys are the top-level keys a call sends that the contract's schema does not list.
//! Keys the engine reads around a command count too: `frames::to_local` reads `body` for
//! commands with picks (extrude's `face`), which the extrude contract declares; it otherwise
//! rewrites only keys a call already has. `live` is read only for commands with `.live(…)`, and
//! `timeline.redefine` adds only `convention` and `participants`, which the contracts declare.
//!
//! The shared field types in [`types`] decide acceptance by calling the same helpers the
//! handlers use (`params::vec3`, `params::expr`, `Operation::parse`, `features::profiles`…), so
//! "the contract accepts it" means "the handler's own reader accepts it". Where the handler is
//! more lenient (a value of the wrong type read as if the key were absent), the contract is the
//! stricter of the two; that difference is what warn and strict surface.
//!
//! Contracts live in one module per command file: `features.rs` for `cmd/features.rs`,
//! `inspect.rs` for `cmd/inspect.rs`, and so on, each with its own tests (`features_tests.rs`),
//! so conversions of different command files touch different files (apart from a new
//! module's line below).
//!
//! To convert a command: declare its parameters type in the module for its command file (and
//! its reply type, for an output schema), creating the module if it is the file's first
//! contract, from the shared types in [`types`] (the keys every feature takes come from
//! [`FeatureCommon`], which `solid.box` and `solid.extrude` predate); give its `CommandSpec`
//! `checked!(…)`, `.contract(…)` and `.output(…)` as `solid.extrude` does; and add it to that
//! module's `schemas_and_the_key_check_agree` and, for messages it already gives,
//! `strict_keeps_todays_errors`. Then run the whole test suite with the audit recorder (see
//! `record.rs`): a call that succeeds today but breaks the contract is either a form the
//! contract is missing or a caller's mistake.
mod check;
#[cfg_attr(target_arch = "wasm32", path = "record_wasm.rs")]
mod record;
mod types;
#[cfg(test)]
mod tests;
// The contracts, by command file.
pub mod features;
pub mod inspect;
pub use check::{Contract, Issue, ParamCheck, TypedContract, input_schema_for, output_schema_for, run_checked};
pub use types::*;
/// A command's `run` that checks the call against `$contract` before running `$handler`
/// (see [`run_checked`]). Expands to a non-capturing closure, which coerces to the registry's
/// `Run` function pointer.
#[macro_export]
#[doc(hidden)]
macro_rules! checked {
($id:literal, $contract:path, $handler:path) => {
|s: &mut $crate::Session, p: &serde_json::Value| $crate::schema::run_checked(s, p, $id, &$contract, $handler)
};
}