zerokms_protocol/
identified_by.rs

1use std::{
2    fmt::{self, Display, Formatter},
3    ops::Deref,
4};
5
6use serde::{Deserialize, Serialize};
7use uuid::Uuid;
8
9/// A UUID or textual name that can uniquely identify a resource. Whereas a UUID is a global identifier, `name` is not
10/// implied to be *globally* unique, but unique within scope implied scope: e.g. a workspace.
11#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Clone)]
12pub enum IdentifiedBy {
13    // A UUID that uniquely identifies a resource.
14    Uuid(Uuid),
15    // A name that uniquely identifies a resource when used in combination with an implied scope, e.g. a workspace.
16    Name(Name),
17}
18
19impl<'de> Deserialize<'de> for IdentifiedBy {
20    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
21    where
22        D: serde::Deserializer<'de>,
23    {
24        // This is the same as `IdentifiedBy` but is required to avoid a recursive Deserialize stack overflow.
25        #[derive(Deserialize)]
26        enum Stub {
27            Uuid(Uuid),
28            Name(Name),
29        }
30
31        // This representation is how backwards compatibility is dealt with on the wire.
32        #[derive(Deserialize)]
33        #[serde(untagged)]
34        enum Compat {
35            Uuid(Uuid),         // Old style (UUID only)
36            IdentifiedBy(Stub), // New style (UUID or Name)
37        }
38
39        if let Ok(id_compat) = <Compat as Deserialize>::deserialize(deserializer) {
40            match id_compat {
41                Compat::Uuid(uuid) => Ok(IdentifiedBy::Uuid(uuid)),
42                Compat::IdentifiedBy(stub) => match stub {
43                    Stub::Uuid(uuid) => Ok(IdentifiedBy::Uuid(uuid)),
44                    Stub::Name(name) => Ok(IdentifiedBy::Name(name)),
45                },
46            }
47        } else {
48            Err(serde::de::Error::custom(
49                "expected one of: a UUID, IdentifiedBy::Uuid or IdentifiedBy::Name",
50            ))
51        }
52    }
53}
54
55impl From<Uuid> for IdentifiedBy {
56    fn from(value: Uuid) -> Self {
57        Self::Uuid(value)
58    }
59}
60
61impl From<Name> for IdentifiedBy {
62    fn from(value: Name) -> Self {
63        Self::Name(value)
64    }
65}
66
67impl From<String> for Name {
68    fn from(value: String) -> Self {
69        Self { inner: value }
70    }
71}
72
73impl Display for IdentifiedBy {
74    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
75        match self {
76            IdentifiedBy::Uuid(uuid) => Display::fmt(uuid, f),
77            IdentifiedBy::Name(name) => Display::fmt(name, f),
78        }
79    }
80}
81
82/// The unique name of a resource (within some scope: e.g. a workspace).
83#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Clone)]
84#[serde(transparent)]
85pub struct Name {
86    inner: String,
87}
88
89impl Name {
90    /// Crates a new `Name` from an already persisted name. Does not run validations.
91    pub fn new_untrusted(name: &str) -> Self {
92        Self {
93            inner: name.to_owned(),
94        }
95    }
96}
97
98impl Deref for Name {
99    type Target = str;
100
101    fn deref(&self) -> &Self::Target {
102        &self.inner
103    }
104}
105
106const NAME_MAX_LEN: usize = 64;
107
108pub struct InvalidNameError(pub String);
109
110impl TryFrom<&str> for Name {
111    type Error = InvalidNameError;
112
113    fn try_from(value: &str) -> Result<Self, Self::Error> {
114        if !value.len() <= NAME_MAX_LEN {
115            return Err(InvalidNameError(format!(
116                "name length must be <= 64, got {}",
117                value.len()
118            )));
119        }
120
121        if value.is_empty() {
122            return Err(InvalidNameError(
123                "name must not be an empty string".to_string(),
124            ));
125        }
126
127        if !value
128            .chars()
129            .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-' || c == '/')
130        {
131            return Err(InvalidNameError(
132                "name must consist of only these allowed characters: A-Z a-z 0-9 _ - /".to_string(),
133            ));
134        }
135
136        Ok(Name {
137            inner: value.to_string(),
138        })
139    }
140}
141
142impl Display for Name {
143    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
144        Display::fmt(&self.inner, f)
145    }
146}
147
148#[cfg(test)]
149mod test {
150    use super::*;
151
152    #[test]
153    fn name_validation() {
154        assert!(Name::try_from("alias").is_ok());
155        assert!(Name::try_from("foo/bar").is_ok());
156        assert!(Name::try_from(
157            "abcdefghijklmnopqrstuvqwxyzABCDEFGHIJKLMNOPQRSTUVQWXYZ0123456789-_/"
158        )
159        .is_ok());
160        assert!(Name::try_from(" leading-ws").is_err());
161        assert!(Name::try_from("trailing-ws ").is_err());
162        assert!(Name::try_from("punctuation%").is_err());
163    }
164}