axiolid_contracts/
capability_id.rs

1//! Stable, transport-independent capability identifiers.
2
3use core::fmt;
4
5/// Versioned semantic operation identity, independent of providers and wire encoding.
6#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
7pub struct CapabilityId(&'static str);
8
9impl CapabilityId {
10    pub const fn from_static(value: &'static str) -> Self {
11        Self(value)
12    }
13    pub const fn as_str(self) -> &'static str {
14        self.0
15    }
16}
17
18impl fmt::Debug for CapabilityId {
19    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20        f.debug_tuple("CapabilityId").field(&self.0).finish()
21    }
22}
23impl fmt::Display for CapabilityId {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        f.write_str(self.0)
26    }
27}
28
29pub mod capability_ids {
30    use super::CapabilityId;
31    pub const TESSELLATE: CapabilityId =
32        CapabilityId::from_static("org.axiolid.geometry.tessellate.v1");
33    pub const MESH_BOOLEAN: CapabilityId =
34        CapabilityId::from_static("org.axiolid.geometry.mesh-boolean.v1");
35    pub const MESH_SECTION: CapabilityId =
36        CapabilityId::from_static("org.axiolid.geometry.mesh-section.v1");
37    /// Point-sampled geometry reconstructed into a discrete surface.
38    ///
39    /// Distinct from [`MESH_BOOLEAN`] and friends because the input is not a
40    /// mesh at all: a provider advertising this claims it can turn an
41    /// unstructured point set into a surface, which is a different
42    /// obligation from operating on one that already exists.
43    pub const POINTCLOUD_RECONSTRUCTION: CapabilityId =
44        CapabilityId::from_static("org.axiolid.geometry.pointcloud-reconstruction.v1");
45    pub const MESH_VALIDATE: CapabilityId =
46        CapabilityId::from_static("org.axiolid.geometry.mesh-validate.v1");
47    pub const MESH_MEASURE: CapabilityId =
48        CapabilityId::from_static("org.axiolid.geometry.mesh-measure.v1");
49    pub const RAY_MESH: CapabilityId =
50        CapabilityId::from_static("org.axiolid.geometry.ray-mesh.v1");
51    pub const EXACT_EXTRUDE: CapabilityId =
52        CapabilityId::from_static("org.axiolid.geometry.exact-extrude.v1");
53    pub const GRAPH_TO_MESH: CapabilityId =
54        CapabilityId::from_static("org.axiolid.geometry.graph-to-mesh.v1");
55    /// Graph lowered to an exact B-rep, or refused.
56    ///
57    /// Deliberately distinct from [`GRAPH_TO_MESH`]: a backend advertising this
58    /// promises analytic supports and trims survive, never triangles.
59    pub const GRAPH_TO_EXACT_BREP: CapabilityId =
60        CapabilityId::from_static("org.axiolid.geometry.graph-to-exact-brep.v1");
61    /// Point, tangent and oriented frame at a distance along a curve.
62    ///
63    /// Distinct from tessellation: this is exact evaluation of an
64    /// analytic curve, not a sampled approximation of it.
65    pub const CURVE_EVALUATE: CapabilityId =
66        CapabilityId::from_static("org.axiolid.geometry.curve-evaluate.v1");
67    /// Every identifier this vocabulary defines.
68    ///
69    /// A SLICE rather than a fixed-size array on purpose: with
70    /// `[CapabilityId; N]` the length is part of the public type, so
71    /// registering a capability changed `N` and was technically a breaking
72    /// change for every consumer. Capabilities are added as the kernel
73    /// grows, and taxing each addition with a major bump would either
74    /// throttle them or push the break through silently. A slice makes
75    /// every future addition additive.
76    pub const ALL: &[CapabilityId] = &[
77        TESSELLATE,
78        MESH_BOOLEAN,
79        MESH_SECTION,
80        MESH_VALIDATE,
81        MESH_MEASURE,
82        RAY_MESH,
83        EXACT_EXTRUDE,
84        GRAPH_TO_MESH,
85        GRAPH_TO_EXACT_BREP,
86        CURVE_EVALUATE,
87    ];
88}
89
90#[cfg(test)]
91mod tests {
92    use super::capability_ids::ALL;
93    use std::collections::BTreeSet;
94
95    /// Registering a capability must stay an ADDITIVE change.
96    ///
97    /// `ALL` was `[CapabilityId; N]`, where the count sat in the public
98    /// type: adding an identifier changed the type and broke every
99    /// consumer, so a routine addition demanded a major bump. Binding it
100    /// as a slice here fails to compile if it is ever narrowed back to a
101    /// fixed-size array.
102    #[test]
103    fn the_vocabulary_can_grow_without_a_breaking_change() {
104        let ids: &[super::CapabilityId] = super::capability_ids::ALL;
105        assert!(ids.len() >= 10, "vocabulary should not shrink");
106    }
107    #[test]
108    fn ids_are_unique_versioned_ascii_tokens() {
109        let unique: BTreeSet<_> = ALL.iter().map(|id| id.as_str()).collect();
110        assert_eq!(unique.len(), ALL.len());
111        for id in ALL {
112            let text = id.as_str();
113            assert!(text.ends_with(".v1"));
114            assert!(text
115                .bytes()
116                .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || matches!(b, b'.' | b'-')));
117        }
118    }
119}