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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
mod entity_type;
mod kind;
mod uri;

pub use self::{entity_type::*, kind::*, uri::*};

use std::collections::HashMap;

use anyhow::Context;
use schemars::JsonSchema;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use time::OffsetDateTime;
use uuid::Uuid;

/// Common entity metadata.
///
/// This data is not generic, and is the same for all entity kinds.
#[derive(Serialize, Deserialize, JsonSchema, PartialEq, Eq, Clone, Debug)]
pub struct EntityMeta {
    /// Optional unique id.
    pub uid: Option<Uuid>,

    /// Name of the entity.
    ///
    /// This is only unique within the scope of the entity.
    pub name: String,

    /// Long description.
    ///
    /// Should be either plain text or markdown.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// Labels are used to organize entities.
    /// They are a set of simple key/value pairs.
    #[serde(default)]
    #[serde(skip_serializing_if = "HashMap::is_empty")]
    pub labels: HashMap<String, String>,

    /// Annotations are used to attach arbitrary metadata to entities.
    /// They can contain arbitrary (json-encodable) data.
    #[serde(default)]
    #[serde(skip_serializing_if = "HashMap::is_empty")]
    pub annotations: HashMap<String, serde_json::Value>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub parent: Option<EntityUri>,
}

impl EntityMeta {
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            uid: None,
            name: name.into(),
            description: None,
            labels: Default::default(),
            annotations: Default::default(),
            parent: None,
        }
    }

    pub fn with_uid(mut self, uid: Uuid) -> Self {
        self.uid = Some(uid);
        self
    }

    pub fn with_annotations<I, K, V>(mut self, annotations: I) -> Self
    where
        I: IntoIterator<Item = (K, V)>,
        K: Into<String>,
        V: Into<serde_json::Value>,
    {
        self.annotations = annotations
            .into_iter()
            .map(|(k, v)| (k.into(), v.into()))
            .collect();
        self
    }
}

/// An entity with associated data.
#[derive(Serialize, Deserialize, JsonSchema, PartialEq, Eq, Clone, Debug)]
pub struct Entity<D, C = serde_json::Value> {
    /// Common entity metadata.
    pub meta: EntityMeta,
    /// Specification of the entity.
    pub spec: D,
    /// Inline child entity specs.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub children: Option<Vec<C>>,
}

impl<D> Entity<D> {
    pub fn new_with_name(name: impl Into<String>, spec: D) -> Self {
        Self {
            meta: EntityMeta::new(name),
            spec,
            children: None,
        }
    }

    pub fn try_map_spec<F, O, E>(self, f: F) -> Result<Entity<O>, E>
    where
        F: FnOnce(D) -> Result<O, E>,
    {
        Ok(Entity {
            meta: self.meta,
            spec: f(self.spec)?,
            children: self.children,
        })
    }

    pub fn uid(&self) -> Option<Uuid> {
        self.meta.uid
    }
}

pub type JsonEntity = Entity<serde_json::Value, serde_json::Value>;

impl<D, C> Entity<D, C>
where
    D: EntityDescriptorConst,
{
    pub fn uri(&self) -> String {
        format!("{}:{}", D::KIND, self.meta.name)
    }

    pub fn build_uri(&self) -> EntityUri {
        // NOTE: using unwrap here because an invalid kind in Self::KIND is a
        // user error.
        EntityUri::parse(self.uri()).unwrap()
    }
}

impl<D, C> Entity<D, C>
where
    D: EntityDescriptorConst + serde::Serialize,
    C: serde::Serialize,
{
    /// Convert this type to yaml, injecting the kind into the output.
    // TODO: make this redundant with a custom Serialize impl!
    pub fn to_json_map(&self) -> Result<serde_json::Value, serde_json::Error> {
        // Constructing a custom object to properly order the fields.
        // (kind, then meta, then spec)
        let mut map = serde_json::Value::Object(Default::default());
        map["kind"] = D::KIND.into();
        map["meta"] = serde_json::to_value(&self.meta)?;
        map["spec"] = serde_json::to_value(&self.spec)?;

        Ok(map)
    }

    pub fn to_json(&self) -> Result<String, serde_json::Error> {
        let map = self.to_json_map()?;
        serde_json::to_string_pretty(&map)
    }

    /// Convert this type to yaml, injecting the kind into the output.
    // TODO: make this redundant with a custom Serialize impl!
    pub fn to_yaml_map(&self) -> Result<serde_yaml::Mapping, serde_yaml::Error> {
        // Constructing a custom object to properly order the fields.
        // (kind, then meta, then spec)
        let mut map = serde_yaml::Mapping::new();
        map.insert("kind".into(), D::KIND.into());
        map.insert("meta".into(), serde_yaml::to_value(&self.meta)?);
        map.insert("spec".into(), serde_yaml::to_value(&self.spec)?);

        Ok(map)
    }

    /// Convert this type to yaml, injecting the kind into the output.
    // TODO: make this redundant with a custom Serialize impl!
    pub fn to_yaml(&self) -> Result<String, serde_yaml::Error> {
        let map = self.to_yaml_map()?;
        serde_yaml::to_string(&map)
    }

    /// Converts this type into a generic entity
    pub fn to_generic(&self) -> Result<GenericEntity, serde_json::Error> {
        // TODO: @Christoph - the children parser needs to be implemented
        assert!(self.children.is_none());

        Ok(GenericEntity {
            kind: D::KIND.to_string(),
            meta: self.meta.clone(),
            spec: serde_json::to_value(&self.spec)?,
            children: None,
        })
    }
}

/// Generic, untyped entity.
#[derive(Serialize, Deserialize, JsonSchema, PartialEq, Eq, Clone, Debug)]
pub struct GenericEntity {
    pub kind: String,

    /// Common entity metadata.
    pub meta: EntityMeta,
    /// Specification of the entity.
    pub spec: serde_json::Value,
    /// Inline child entity specs.
    pub children: Option<Vec<GenericEntity>>,
}

impl GenericEntity {
    pub fn build_uri_str(&self) -> String {
        format!("{}:{}", self.kind, self.meta.name)
    }

    pub fn build_uri(&self) -> Result<EntityUri, EntityUriParseError> {
        EntityUri::new_kind_name(&self.kind, &self.meta.name)
    }
}

/// An entity with associated data, including state.
#[derive(Serialize, Deserialize, JsonSchema, PartialEq, Eq, Clone, Debug)]
pub struct FullEntity<D, S = (), C = serde_json::Value> {
    /// Common entity metadata.
    pub meta: EntityMeta,
    /// Specification of the entity.
    pub spec: D,
    /// Inline child entity specs.
    pub children: Option<Vec<C>>,
    pub state: EntityState<S>,
}

impl<D, S, C> FullEntity<D, S, C> {
    pub fn uid(&self) -> Uuid {
        self.meta.uid.unwrap_or_default()
    }

    pub fn with_main_state(mut self, state: S) -> Self {
        let now = OffsetDateTime::now_utc();
        let state = if let Some(mut s) = self.state.main.take() {
            s.updated_at = now;
            s.state_version += 1;
            s.data = state;
            s
        } else {
            EntityStateComponent {
                state_version: 1,
                updated_at: now,
                data: state,
            }
        };
        self.state.main = Some(state);
        self
    }
}

/// State of an entity.
///
/// Contains a `main` state, which will be managed by the owning service that
/// manages the entity.
///
/// Additional services may inject their own state, which will be found in
/// [`Self::components`].
#[derive(Serialize, Deserialize, JsonSchema, PartialEq, Eq, Clone, Debug)]
pub struct EntityState<S = ()> {
    /// Version of the entity.
    /// All modifications to metadata or spec will increment this version.
    pub entity_version: u64,

    /// UUID of the parent entity.
    /// This is only set if the entity is a child of another entity.
    pub parent_uid: Option<Uuid>,

    /// Creation timestamp.
    #[serde(with = "time::serde::timestamp")]
    #[schemars(with = "u64")]
    pub created_at: OffsetDateTime,
    /// Last update timestamp.
    /// Will be set on each metadata or spec change, but not on state changes.
    #[serde(with = "time::serde::timestamp")]
    #[schemars(with = "u64")]
    pub updated_at: OffsetDateTime,

    /// The primary state of the entity, managed by the owning service.
    pub main: Option<EntityStateComponent<S>>,

    /// Additional entity states, managed by services other than the entity owners.
    pub components: HashMap<String, EntityStateComponent>,
}

/// Single component of an entities state.
#[derive(Serialize, Deserialize, JsonSchema, PartialEq, Eq, Clone, Debug)]
pub struct EntityStateComponent<T = serde_json::Value> {
    /// Version of this state.
    /// Will be incremented on each change.
    pub state_version: u64,
    /// Update timestamp.
    #[serde(with = "time::serde::timestamp")]
    #[schemars(with = "u64")]
    pub updated_at: OffsetDateTime,
    /// The actual state data.
    pub data: T,
}

/// A marker trait for entity types.
///
/// Should be implementes on the struct representing the entities spec.
pub trait EntityDescriptorConst {
    const NAMESPACE: &'static str;
    const NAME: &'static str;
    const VERSION: &'static str;
    const KIND: &'static str;

    /// Entity specification.
    type Spec: Serialize + DeserializeOwned + JsonSchema + Clone + PartialEq + Eq + std::fmt::Debug;
    /// The main entity state.
    type State: Serialize + DeserializeOwned + JsonSchema + Clone + PartialEq + Eq + std::fmt::Debug;

    fn json_schema() -> schemars::schema::RootSchema {
        schemars::schema_for!(Entity<Self::Spec>)
    }

    fn build_uri_str(name: &str) -> String {
        format!("{}:{}", Self::KIND, name)
    }

    fn build_uri(name: &str) -> Result<EntityUri, EntityUriParseError> {
        EntityUri::new_kind_name(Self::KIND, name)
    }

    /// Build the name that is used for the EntityTypeSpec representing this type.
    fn type_name() -> String {
        // TODO: this should be an additional const...
        format!("{}-{}-v{}", Self::NAMESPACE, Self::NAME, Self::VERSION)
    }

    fn build_type_descriptor() -> Entity<EntityTypeSpec>
    where
        Self: JsonSchema + Sized,
    {
        EntityTypeSpec::build_for_type::<Self>()
    }
}

/// Deserialize a typed entity from YAML.
pub fn deserialize_entity_yaml_typed<T>(input: &str) -> Result<Entity<T>, anyhow::Error>
where
    T: EntityDescriptorConst + DeserializeOwned,
{
    let raw: serde_yaml::Value = serde_yaml::from_str(input).context("invalid YAML")?;
    let kind = raw
        .get("kind")
        .context("missing 'kind' field in yaml")?
        .as_str()
        .context("'kind' field is not a string")?;

    if kind != T::KIND {
        anyhow::bail!("expected kind '{}' but got '{}'", T::KIND, kind);
    }

    let out = serde_yaml::from_value(raw).context("could not deserialize to entity data")?;
    Ok(out)
}