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
/// 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,

    kind_name_start: usize,
    kind_version_start: usize,

    path_start: usize,
    name_start: usize,
}

impl EntityUri {
    pub fn parse(_s: impl Into<String>) -> Result<Self, EntityUriParseError> {
        todo!()
    }
}

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

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

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 enum EntityUriParseError {}

impl std::fmt::Display for EntityUriParseError {
    fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            _ => {
                todo!()
            }
        }
    }
}

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

/// 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),
}