Skip to main content

cadmpeg_core/decode/
depth.rs

1// SPDX-License-Identifier: Apache-2.0
2//! How much of a source document a decode transfers.
3
4use std::fmt;
5
6#[cfg(feature = "schema")]
7use schemars::JsonSchema;
8use serde::{Deserialize, Serialize};
9
10/// How much of the source document a decode transfers.
11///
12/// Depths are ordered: each one transfers everything a shallower depth
13/// transfers. A caller requests a depth through the decode context, and the
14/// codec records the depth it delivered on the decoded document.
15#[derive(
16    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize,
17)]
18#[cfg_attr(feature = "schema", derive(JsonSchema))]
19#[serde(rename_all = "snake_case")]
20pub enum DecodeDepth {
21    /// The container layer only: framing, identity, and native container
22    /// records, without entity transfer.
23    Container,
24    /// Everything an exchange output consumes: B-rep, tessellation, product
25    /// structure, placements, appearance, PMI, and attributes. Every design
26    /// arena (features, sketches, parameters, configurations, spreadsheets,
27    /// and their topology bindings) is empty.
28    Geometry,
29    /// Everything the codec transfers, including design intent.
30    #[default]
31    Full,
32}
33
34impl DecodeDepth {
35    /// The wire name of this depth.
36    #[must_use]
37    pub const fn as_str(self) -> &'static str {
38        match self {
39            Self::Container => "container",
40            Self::Geometry => "geometry",
41            Self::Full => "full",
42        }
43    }
44}
45
46impl fmt::Display for DecodeDepth {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        f.pad(self.as_str())
49    }
50}
51
52#[cfg(test)]
53mod tests {
54    use super::DecodeDepth;
55
56    #[test]
57    fn depths_order_by_what_they_transfer() {
58        assert!(DecodeDepth::Container < DecodeDepth::Geometry);
59        assert!(DecodeDepth::Geometry < DecodeDepth::Full);
60        assert_eq!(DecodeDepth::default(), DecodeDepth::Full);
61    }
62
63    #[test]
64    fn wire_names_match_display() {
65        for depth in [
66            DecodeDepth::Container,
67            DecodeDepth::Geometry,
68            DecodeDepth::Full,
69        ] {
70            assert_eq!(
71                serde_json::to_value(depth).expect("depth serializes"),
72                serde_json::Value::String(depth.to_string())
73            );
74        }
75    }
76}