1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/// Represents an entity URI.
///
/// Must start with the [`EntityKind`], followed by a colon (:) and the path to
/// the entity.
///
/// The path may either be just a name, which must refer to an entity in the same
/// scope, a full path to the entity, separated by slashes, or just a UUID.
///
/// Example: my.namespace/MyEntityKind.v1:my-entity-name
/// Example: my.namespace/MyEntityKind.v1:parent/middleman/my-entity-name
/// Example: my.namespace/MyEntityKind.v1:parent/middleman/my-entity-name
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct EntityUri {
    value: String,

    type_start: usize,
    version_start: usize,
    name_start: usize,
}

impl PartialOrd for EntityUri {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for EntityUri {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.value.cmp(&other.value)
    }
}

impl std::hash::Hash for EntityUri {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.value.hash(state);
    }
}

impl EntityUri {
    pub fn as_str(&self) -> &str {
        &self.value
    }

    /// Allowed because this should be used over the Display impl...
    #[allow(clippy::inherent_to_string_shadow_display)]
    pub fn to_string(&self) -> String {
        self.value.clone()
    }

    pub fn into_string(self) -> String {
        self.value
    }

    pub fn kind(&self) -> &str {
        &self.value[..self.name_start - 1]
    }

    pub fn namespace(&self) -> &str {
        &self.value[..self.type_start - 1]
    }

    pub fn entity_type(&self) -> &str {
        &self.value[self.type_start..self.version_start - 2]
    }

    pub fn version(&self) -> &str {
        &self.value[self.version_start..self.name_start - 1]
    }

    pub fn name(&self) -> &str {
        &self.value[self.name_start..]
    }

    pub fn new_kind_name(kind: &str, name: &str) -> Result<Self, EntityUriParseError> {
        // TODO(theduke): this should be more efficient!
        Self::parse(format!("{}:{}", kind, name))
    }

    pub fn parse(s: impl Into<String>) -> Result<Self, EntityUriParseError> {
        let s = s.into();
        let (kind, name) = s.split_once(':').ok_or_else(|| {
            EntityUriParseError::new(s.clone(), EntityUriParseErrorKind::MissingKindNameSeparator)
        })?;

        let (ns, kind) = kind.split_once('/').ok_or_else(|| {
            EntityUriParseError::new(
                s.clone(),
                EntityUriParseErrorKind::MissingKindNamespaceSeparator,
            )
        })?;

        let (ty, version) = kind.split_once('.').ok_or_else(|| {
            EntityUriParseError::new(s.clone(), EntityUriParseErrorKind::InvalidType)
        })?;

        if !is_valid_namespace(ns) {
            return Err(EntityUriParseError::new(
                s,
                EntityUriParseErrorKind::InvalidNamespace,
            ));
        }
        if !is_valid_type_name(ty) {
            return Err(EntityUriParseError::new(
                s,
                EntityUriParseErrorKind::InvalidType,
            ));
        }
        if !is_valid_version(version) {
            return Err(EntityUriParseError::new(
                s,
                EntityUriParseErrorKind::InvalidTypeVersion,
            ));
        }
        if !is_valid_name(name) {
            return Err(EntityUriParseError::new(
                s,
                EntityUriParseErrorKind::InvalidName,
            ));
        }

        let type_start = ns.len() + 1;
        let version_start = type_start + ty.len() + 2;
        let name_start = version_start + version.len();

        Ok(Self {
            value: s,
            type_start,
            version_start,
            name_start,
        })
    }
}

impl std::fmt::Display for EntityUri {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.value.fmt(f)
    }
}

fn is_valid_namespace(s: &str) -> bool {
    s.split('.').all(|p| {
        !p.is_empty()
            && p.chars().all(|c| match c {
                'a'..='z' | 'A'..='Z' | '0'..='9' | '-' => true,
                _ => false,
            })
    })
}

fn is_valid_type_name(s: &str) -> bool {
    !s.is_empty()
        && s.chars().all(|c| match c {
            'a'..='z' | 'A'..='Z' | '0'..='9' => true,
            _ => false,
        })
}

fn is_valid_version(s: &str) -> bool {
    !s.is_empty()
        && s.chars().all(|c| match c {
            'a'..='z' | 'A'..='Z' | '0'..='9' | '-' | '_' => true,
            _ => false,
        })
}

fn is_valid_name(name: &str) -> bool {
    !name.is_empty()
        && name.chars().all(|c| match c {
            'a'..='z' | 'A'..='Z' | '0'..='9' | '-' | '_' | '.' | '+' => true,
            _ => false,
        })
}

impl std::str::FromStr for EntityUri {
    type Err = EntityUriParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::parse(s.to_string())
    }
}

impl serde::Serialize for EntityUri {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_str(&self.value)
    }
}

impl<'de> serde::Deserialize<'de> for EntityUri {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let value = String::deserialize(deserializer)?;

        Self::parse(value).map_err(serde::de::Error::custom)
    }
}

impl schemars::JsonSchema for EntityUri {
    fn schema_name() -> String {
        "EntityUri".to_string()
    }

    fn json_schema(_gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
        schemars::schema::Schema::Object(schemars::schema::SchemaObject {
            instance_type: Some(schemars::schema::InstanceType::String.into()),
            ..Default::default()
        })
    }
}

#[derive(Clone, Debug)]
pub struct EntityUriParseError {
    value: String,
    kind: EntityUriParseErrorKind,
}

impl EntityUriParseError {
    pub fn new(value: impl Into<String>, kind: EntityUriParseErrorKind) -> Self {
        Self {
            value: value.into(),
            kind,
        }
    }
}

impl std::fmt::Display for EntityUriParseError {
    fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(_f, "invalid entity URI: '{}' ({:?})", self.value, self.kind)
    }
}

impl std::error::Error for EntityUriParseError {}

#[derive(Clone, Debug)]
pub enum EntityUriParseErrorKind {
    MissingKindNameSeparator,
    MissingKindNamespaceSeparator,
    InvalidNamespace,
    InvalidType,
    InvalidName,
    InvalidTypeVersion,
}

impl std::fmt::Display for EntityUriParseErrorKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            EntityUriParseErrorKind::MissingKindNameSeparator => {
                write!(f, "missing kind/name separator")
            }
            EntityUriParseErrorKind::MissingKindNamespaceSeparator => {
                write!(f, "missing kind/namespace separator")
            }
            EntityUriParseErrorKind::InvalidNamespace => write!(f, "invalid namespace"),
            EntityUriParseErrorKind::InvalidType => write!(f, "invalid type"),
            EntityUriParseErrorKind::InvalidName => write!(f, "invalid name"),
            EntityUriParseErrorKind::InvalidTypeVersion => write!(f, "invalid type version"),
        }
    }
}

/// Represents either an inline entity definition, or a reference to an entity.
#[derive(
    serde::Serialize, serde::Deserialize, schemars::JsonSchema, PartialEq, Eq, Clone, Debug,
)]
pub enum EntityOrRef<T> {
    #[serde(rename = "ref")]
    Ref(EntityUri),
    #[serde(rename = "item")]
    Item(T),
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_entity_uri_parse() {
        let u = EntityUri::parse("my-ns.com/Ent.v1:lala123".to_string()).unwrap();
        assert_eq!(u.namespace(), "my-ns.com");
        assert_eq!(u.entity_type(), "Ent");
        assert_eq!(u.version(), "1");
        assert_eq!(u.name(), "lala123");
    }
}