Skip to main content

spiral/
ids.rs

1//! Stable identifiers used by control-plane management operations.
2
3use std::fmt::{Display, Formatter, Result as FmtResult};
4
5use serde::{Deserialize, Serialize};
6
7/// A Project authorization grant identifier.
8pub type GrantId = String;
9
10macro_rules! string_id {
11    ($($(#[$doc:meta])* $name:ident;)*) => {$(
12        $(#[$doc])*
13        #[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
14        #[serde(transparent)]
15        pub struct $name(String);
16
17        impl $name {
18            /// The identifier as the API spells it.
19            pub fn as_str(&self) -> &str {
20                &self.0
21            }
22        }
23
24        impl From<String> for $name {
25            fn from(id: String) -> Self {
26                Self(id)
27            }
28        }
29
30        impl From<&str> for $name {
31            fn from(id: &str) -> Self {
32                Self(id.to_owned())
33            }
34        }
35
36        impl AsRef<str> for $name {
37            fn as_ref(&self) -> &str {
38                &self.0
39            }
40        }
41
42        impl Display for $name {
43            fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
44                f.write_str(&self.0)
45            }
46        }
47    )*};
48}
49
50string_id! {
51    /// An OAuth client: an application, a workload or an installation.
52    ClientId;
53    /// An organization.
54    OrgId;
55    /// A project.
56    ProjectId;
57    /// A user.
58    UserId;
59    /// A workload: a non-human identity that runs jobs in a project.
60    WorkloadId;
61}