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(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(pub String);
86
87impl Display for Name {
88    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
89        Display::fmt(&self.0, f)
90    }
91}
92
93impl Deref for Name {
94    type Target = str;
95
96    fn deref(&self) -> &Self::Target {
97        &self.0
98    }
99}