Skip to main content

kernel_api/
lib.rs

1//! printCAD's geometry interface: meshes, profiles and solid operations
2//! as plain data, with no geometry code. The app's kernel adapter answers
3//! it, and workbench packages build their solids as [`SolidOp`] plans
4//! through `printcad-bench-api`.
5//!
6//! Versions follow `printcad-bench-api`'s: 0.1.x speaks the workbench
7//! contract `printcad:workbench@0.1`, and a patch release only adds.
8//! [`FaceSurface`] and [`ProfileSegment`], which the app sends a package,
9//! are `#[non_exhaustive]` unless the `exhaustive` feature (printCAD's
10//! own) is on; [`SolidOp`] and the enums inside it keep their variants'
11//! fields within a minor version.
12
13pub mod naming;
14pub mod pmi;
15pub use naming::TopoName;
16pub use pmi::{PmiDatum, PmiDimension, PmiTolerance, SemanticPmi};
17
18mod chain;
19mod fit;
20mod import;
21mod kernel;
22mod mesh;
23mod mesh_op;
24mod queries;
25mod query;
26mod solid_op;
27
28pub use chain::*;
29pub use fit::*;
30pub use import::*;
31pub use kernel::*;
32pub use mesh::*;
33pub use mesh_op::*;
34pub use queries::*;
35pub use query::*;
36pub use solid_op::*;
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    fn pipe_json(frame: &str) -> String {
43        let plane = r#"{"origin":[0,0,0],"x_axis":[1,0,0],"y_axis":[0,1,0],"normal":[0,0,1]}"#;
44        let profile = format!(
45            r#"{{"plane":{plane},"wires":[{{"segments":[{{"Circle":{{"center":[0,0],"radius":1}}}}]}}]}}"#
46        );
47        format!(r#"{{"Pipe":{{"profile":{profile},"spine":{profile},{frame}"op":"NewSolid"}}}}"#)
48    }
49
50    fn frame_of(json: &str) -> PipeFrame {
51        match serde_json::from_str::<SolidOp>(json).unwrap() {
52            SolidOp::Pipe { frame, .. } => frame,
53            other => panic!("not a pipe: {other:?}"),
54        }
55    }
56
57    #[test]
58    fn a_pipe_written_with_the_frenet_flag_reads_as_its_frame() {
59        assert_eq!(frame_of(&pipe_json(r#""frenet":true,"#)), PipeFrame::Frenet);
60        assert_eq!(
61            frame_of(&pipe_json(r#""frenet":false,"#)),
62            PipeFrame::RotationMinimizing
63        );
64        assert_eq!(frame_of(&pipe_json("")), PipeFrame::RotationMinimizing);
65    }
66
67    #[test]
68    fn a_pipe_frame_round_trips() {
69        let frame = PipeFrame::Binormal {
70            direction: [0.0, 0.0, 1.0],
71        };
72        let text = serde_json::to_string(&frame).unwrap();
73        assert_eq!(frame_of(&pipe_json(&format!(r#""frame":{text},"#))), frame);
74    }
75}