Skip to main content

switchback_traits/
ids.rs

1//! Stable identifiers and version newtypes.
2
3use std::fmt;
4
5/// Intra-contract grouping identifier (protobuf package, OpenAPI tag, etc.).
6///
7/// Serialized on the wire as the group key within a [`ManualContract`](crate::ManualContract).
8#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
9pub struct GroupId(
10    /// Wire-safe group key string (e.g. protobuf package name, OpenAPI tag).
11    pub String,
12);
13
14impl GroupId {
15    /// Borrows the underlying group key.
16    pub fn as_str(&self) -> &str {
17        &self.0
18    }
19}
20
21impl From<String> for GroupId {
22    fn from(value: String) -> Self {
23        Self(value)
24    }
25}
26
27impl From<&str> for GroupId {
28    fn from(value: &str) -> Self {
29        Self(value.to_string())
30    }
31}
32
33impl fmt::Display for GroupId {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        f.write_str(&self.0)
36    }
37}
38
39/// Entity address within a manual: group + category + name.
40///
41/// Used in parser-side [`Entity`](crate::traits::Entity) views and the
42/// [`ResolvedManual`](crate::ResolvedManual) reverse index. Not a standalone wire
43/// message; wire addresses use [`EntityRef`](crate::EntityRef) with module scope.
44#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
45pub struct EntityId {
46    /// Group containing the entity ([`GroupId`] within one contract).
47    pub group: GroupId,
48    /// Family-specific category slug (e.g. `"schemas"`, `"operations"`).
49    pub category: String,
50    /// Entity name within the group and category.
51    pub name: String,
52}
53
54impl EntityId {
55    /// Builds an entity id from group, category, and name components.
56    pub fn new(
57        group: impl Into<GroupId>,
58        category: impl Into<String>,
59        name: impl Into<String>,
60    ) -> Self {
61        Self {
62            group: group.into(),
63            category: category.into(),
64            name: name.into(),
65        }
66    }
67}
68
69/// Top-level module identifier within a reference manual.
70///
71/// Serialized on the wire as the module key on [`EntityRef`](crate::EntityRef) and
72/// related cross-reference types.
73#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
74pub struct ModuleId(
75    /// Wire-safe module key string.
76    pub String,
77);
78
79impl ModuleId {
80    /// Borrows the underlying module key.
81    pub fn as_str(&self) -> &str {
82        &self.0
83    }
84}
85
86impl From<String> for ModuleId {
87    fn from(value: String) -> Self {
88        Self(value)
89    }
90}
91
92impl From<&str> for ModuleId {
93    fn from(value: &str) -> Self {
94        Self(value.to_string())
95    }
96}
97
98impl fmt::Display for ModuleId {
99    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
100        f.write_str(&self.0)
101    }
102}
103
104/// Contract-family spec version string (e.g. `"3.1.1"`, `"2.6.0"`).
105///
106/// Serialized on the wire on [`ManualContract`](crate::ManualContract) and
107/// [`ContractRef`](crate::ContractRef).
108#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
109pub struct SpecVersion(
110    /// Family-specific version label (not the switchback container version).
111    pub String,
112);
113
114impl SpecVersion {
115    /// Borrows the underlying version string.
116    pub fn as_str(&self) -> &str {
117        &self.0
118    }
119}
120
121impl From<String> for SpecVersion {
122    fn from(value: String) -> Self {
123        Self(value)
124    }
125}
126
127impl From<&str> for SpecVersion {
128    fn from(value: &str) -> Self {
129        Self(value.to_string())
130    }
131}
132
133impl fmt::Display for SpecVersion {
134    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
135        f.write_str(&self.0)
136    }
137}