dtz_identifier/
object_id.rs

1static PREFIX: &str = "object-";
2
3#[derive(Debug, Clone, PartialEq)]
4pub struct ObjectId {
5    pub id: String,
6}
7
8impl Default for ObjectId {
9    fn default() -> Self {
10        Self {
11            id: crate::generate_internal_id(12),
12        }
13    }
14}
15
16impl std::fmt::Display for ObjectId {
17    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18        f.write_str(&format!("{PREFIX}{}", self.id))
19    }
20}
21
22impl TryFrom<String> for ObjectId {
23    type Error = String;
24
25    fn try_from(value: String) -> Result<Self, Self::Error> {
26        if let Some(id_str) = value.strip_prefix(PREFIX) {
27            Ok(ObjectId {
28                id: id_str.to_string(),
29            })
30        } else {
31            Err("invalid format".to_string())
32        }
33    }
34}
35
36impl TryFrom<&str> for ObjectId {
37    type Error = String;
38
39    fn try_from(value: &str) -> Result<Self, Self::Error> {
40        if let Some(id_str) = value.strip_prefix(PREFIX) {
41            Ok(ObjectId {
42                id: id_str.to_string(),
43            })
44        } else {
45            Err("invalid format".to_string())
46        }
47    }
48}
49
50impl<'de> serde::Deserialize<'de> for ObjectId {
51    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
52    where
53        D: serde::Deserializer<'de>,
54    {
55        struct ObjectIdVisitor;
56
57        impl serde::de::Visitor<'_> for ObjectIdVisitor {
58            type Value = ObjectId;
59
60            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
61                formatter.write_str("a string starting with '")?;
62                formatter.write_str(PREFIX)?;
63                formatter.write_str("' followed by a 8-character alphanumeric string")
64            }
65
66            fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
67            where
68                E: serde::de::Error,
69            {
70                if let Some(id_str) = value.strip_prefix(PREFIX) {
71                    Ok(ObjectId {
72                        id: id_str.to_string(),
73                    })
74                } else {
75                    Err(E::custom("invalid format"))
76                }
77            }
78        }
79
80        deserializer.deserialize_str(ObjectIdVisitor)
81    }
82}
83
84impl serde::Serialize for ObjectId {
85    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
86    where
87        S: serde::Serializer,
88    {
89        serializer.serialize_str(&self.to_string())
90    }
91}
92
93#[test]
94fn key_invalid_1() {
95    let k = "abc-dsfdg";
96    let object: Result<ObjectId, String> = ObjectId::try_from(k);
97    assert!(object.is_err())
98}
99
100#[test]
101fn key_valid_1() {
102    let k = "object-dsfdg";
103    let object: Result<ObjectId, String> = ObjectId::try_from(k);
104    assert!(object.is_ok())
105}
106
107#[test]
108fn key_valid_2() {
109    let k = "object-0190c589-eb70-7980-97cf-af67b3a84116";
110    let object: Result<ObjectId, String> = ObjectId::try_from(k);
111    assert!(object.is_ok())
112}
113
114#[test]
115fn key_invalid_2() {
116    let k = "abc-0190c589-eb70-7980-97cf-af67b3a84116";
117    let object: Result<ObjectId, String> = ObjectId::try_from(k);
118    assert!(object.is_err())
119}