Skip to main content

greentic_deploy_spec/
ids.rs

1//! Identifier newtypes used across the deployment object model.
2//!
3//! `EnvId` is re-exported from `greentic-types`. `RevisionId` and `DeploymentId`
4//! are ULIDs (monotonically sortable, 26-char Crockford base32). `BundleId`,
5//! `CustomerId`, `PackId`, and `PartyId` are opaque strings — format and
6//! authoritative source live outside this crate.
7
8use serde::{Deserialize, Serialize};
9use std::fmt;
10use ulid::Ulid;
11
12/// ULID-shaped identifier for a revision (`§5.2`).
13#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
14#[serde(transparent)]
15pub struct RevisionId(pub Ulid);
16
17impl RevisionId {
18    pub fn new() -> Self {
19        Self(Ulid::new())
20    }
21}
22
23impl Default for RevisionId {
24    fn default() -> Self {
25        Self::new()
26    }
27}
28
29impl fmt::Display for RevisionId {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        write!(f, "{}", self.0)
32    }
33}
34
35/// ULID-shaped identifier for a [`BundleDeployment`](crate::BundleDeployment) (`§5.4`).
36#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
37#[serde(transparent)]
38pub struct DeploymentId(pub Ulid);
39
40impl DeploymentId {
41    pub fn new() -> Self {
42        Self(Ulid::new())
43    }
44}
45
46impl Default for DeploymentId {
47    fn default() -> Self {
48        Self::new()
49    }
50}
51
52impl fmt::Display for DeploymentId {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        write!(f, "{}", self.0)
55    }
56}
57
58/// ULID-shaped identifier for a
59/// [`MessagingEndpoint`](crate::MessagingEndpoint) (`Phase M`).
60#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
61#[serde(transparent)]
62pub struct MessagingEndpointId(pub Ulid);
63
64impl MessagingEndpointId {
65    pub fn new() -> Self {
66        Self(Ulid::new())
67    }
68}
69
70impl Default for MessagingEndpointId {
71    fn default() -> Self {
72        Self::new()
73    }
74}
75
76impl fmt::Display for MessagingEndpointId {
77    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78        write!(f, "{}", self.0)
79    }
80}
81
82macro_rules! string_id {
83    ($(#[$meta:meta])* $name:ident) => {
84        $(#[$meta])*
85        #[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
86        #[serde(transparent)]
87        pub struct $name(pub String);
88
89        impl $name {
90            pub fn new(s: impl Into<String>) -> Self {
91                Self(s.into())
92            }
93
94            pub fn as_str(&self) -> &str {
95                &self.0
96            }
97        }
98
99        impl fmt::Display for $name {
100            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
101                f.write_str(&self.0)
102            }
103        }
104
105        impl From<String> for $name {
106            fn from(s: String) -> Self {
107                Self(s)
108            }
109        }
110
111        impl From<&str> for $name {
112            fn from(s: &str) -> Self {
113                Self(s.to_string())
114            }
115        }
116    };
117}
118
119string_id!(
120    /// Stable identifier for a bundle (`bundle_id` field across §5.2–5.4).
121    BundleId
122);
123
124string_id!(
125    /// Billing principal identifier (`§5.4`). Defaults to `local-dev` for `local` env.
126    CustomerId
127);
128
129string_id!(
130    /// Pack identifier (`§5.2`). Resolves through the pack store.
131    PackId
132);
133
134string_id!(
135    /// Revenue-share party identifier (`§5.4`).
136    PartyId
137);