archival 0.15.0

The simplest CMS in existence
Documentation
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
use liquid_core::model;
use ordermap::OrderMap;
use serde::{Deserialize, Serialize};
use std::{fmt::Display, hash::Hash};

use crate::util::integer_decode;

use super::DateTime;

#[cfg(feature = "typescript")]
mod typedefs {
    use typescript_type_def::{
        type_expr::{Ident, NativeTypeInfo, TypeExpr, TypeInfo, TypeName},
        TypeDef,
    };

    use crate::fields::MetaValue;

    // These two types are workarounds for the fact that there's a circular type
    // in MetaValue. Below, we use MetaMapTypeDef to then define a meta type
    // that will result in a dependency (which will make sure that types that
    // have MetaValues will also define MetaValue itself, which will not happen
    // with the following two types). The underlying issue is tracked here:
    // https://github.com/dbeckwith/rust-typescript-type-def/issues/18#issuecomment-2078469020
    pub struct MetaArrTypeDef;
    impl TypeDef for MetaArrTypeDef {
        const INFO: TypeInfo = TypeInfo::Native(NativeTypeInfo {
            r#ref: TypeExpr::ident(Ident("MetaValue[]")),
        });
    }
    pub struct MetaTypeDef;
    impl TypeDef for MetaTypeDef {
        const INFO: TypeInfo = TypeInfo::Native(NativeTypeInfo {
            r#ref: TypeExpr::ident(Ident("Record<string, MetaValue>")),
        });
    }

    pub struct MetaMapTypeDef;
    impl TypeDef for MetaMapTypeDef {
        const INFO: TypeInfo = TypeInfo::Native(NativeTypeInfo {
            r#ref: TypeExpr::Name(TypeName {
                path: &[],
                name: Ident("Record"),
                generic_args: &[
                    TypeExpr::Ref(&String::INFO),
                    TypeExpr::Ref(&MetaValue::INFO),
                ],
            }),
        });
    }
}

pub type MetaMap = OrderMap<String, MetaValue>;

#[derive(Debug, Default, Deserialize, Serialize, Clone, PartialEq, Hash)]
#[cfg_attr(feature = "typescript", derive(typescript_type_def::TypeDef))]
pub struct Meta(
    #[cfg_attr(feature = "typescript", type_def(type_of = "typedefs::MetaMapTypeDef"))] pub MetaMap,
);

impl Display for Meta {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self.to_toml())
    }
}

impl Meta {
    pub fn to_toml(&self) -> toml::map::Map<std::string::String, toml::Value> {
        let mut m = toml::map::Map::new();
        for (k, v) in &self.0 {
            m.insert(k.to_string(), v.to_toml());
        }
        m
    }
    pub fn get_value(&self, key: &str) -> Option<&MetaValue> {
        self.0.get(key)
    }

    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    pub fn to_liquid(&self) -> liquid::model::Value {
        let mut m = liquid::Object::new();
        for (k, v) in &self.0 {
            m.insert(k.into(), v.into());
        }
        m.into()
    }
}

impl From<&serde_json::Value> for MetaValue {
    fn from(value: &serde_json::Value) -> Self {
        match value {
            serde_json::Value::Bool(b) => MetaValue::Boolean(*b),
            serde_json::Value::Number(n) => MetaValue::Number(n.as_f64().unwrap()),
            serde_json::Value::String(s) => MetaValue::String(s.into()),
            serde_json::Value::Array(a) => MetaValue::Array(a.iter().map(|i| i.into()).collect()),
            serde_json::Value::Object(o) => MetaValue::Map(o.into()),
            serde_json::Value::Null => {
                todo!("null values unsupported when converting from json to MetaValue")
            }
        }
    }
}

impl From<&serde_json::Map<String, serde_json::Value>> for Meta {
    fn from(value: &serde_json::Map<String, serde_json::Value>) -> Self {
        let mut meta = Self::default();
        for (k, v) in value {
            meta.0.insert(k.to_string(), v.into());
        }
        meta
    }
}

impl From<&MetaValue> for model::Value {
    fn from(value: &MetaValue) -> Self {
        match value {
            MetaValue::String(s) => model::Value::scalar(s.to_string()),
            MetaValue::Number(v) => model::Value::scalar(*v),
            MetaValue::Boolean(v) => model::Value::scalar(*v),
            MetaValue::DateTime(d) => model::Value::scalar(d.as_liquid_datetime()),
            MetaValue::Array(v) => model::Value::array(
                v.iter()
                    .map(model::Value::from)
                    .collect::<Vec<model::Value>>(),
            ),
            MetaValue::Map(m) => model::Value::Object(model::Object::from(m)),
        }
    }
}
impl From<&Meta> for model::Object {
    fn from(value: &Meta) -> Self {
        let mut m = model::Object::new();
        for (k, v) in &value.0 {
            m.insert(k.into(), v.into());
        }
        m
    }
}

impl From<&Meta> for serde_json::Value {
    fn from(value: &Meta) -> Self {
        let mut m = serde_json::Map::<String, serde_json::Value>::new();
        for (k, v) in &value.0 {
            m.insert(k.to_string(), v.into());
        }
        serde_json::Value::Object(m)
    }
}

#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
#[cfg_attr(feature = "typescript", derive(typescript_type_def::TypeDef))]
pub enum MetaValue {
    String(String),
    Number(f64),
    Boolean(bool),
    DateTime(DateTime),
    Array(
        #[cfg_attr(feature = "typescript", type_def(type_of = "typedefs::MetaArrTypeDef"))]
        Vec<MetaValue>,
    ),
    Map(#[cfg_attr(feature = "typescript", type_def(type_of = "typedefs::MetaTypeDef"))] Meta),
}

impl Hash for MetaValue {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        match self {
            MetaValue::Number(n) => integer_decode(*n).hash(state),
            v => v.hash(state),
        }
    }
}

impl MetaValue {
    pub fn to_toml(&self) -> toml::Value {
        match self {
            Self::String(s) => toml::Value::String(s.to_string()),
            Self::Number(v) => toml::Value::Float(*v),
            Self::Boolean(v) => toml::Value::Boolean(*v),
            Self::DateTime(d) => {
                let dt = *d.as_liquid_datetime();
                let date = dt.date();
                let time = dt.time();
                let offset = dt.offset();
                toml::Value::Datetime(toml_datetime::Datetime {
                    date: Some(toml_datetime::Date {
                        year: date.year() as u16,
                        month: date.month() as u8,
                        day: date.day(),
                    }),
                    time: Some(toml_datetime::Time {
                        hour: time.hour(),
                        minute: time.minute(),
                        second: time.second(),
                        nanosecond: time.nanosecond(),
                    }),
                    offset: Some(toml_datetime::Offset::Custom {
                        minutes: offset.whole_minutes(),
                    }),
                })
            }
            Self::Array(v) => toml::Value::Array(v.iter().map(|n| n.to_toml()).collect()),
            Self::Map(m) => toml::Value::Table(m.to_toml()),
        }
    }
}

// JSON

impl From<&MetaValue> for serde_json::Value {
    fn from(value: &MetaValue) -> Self {
        match value {
            MetaValue::String(s) => s.to_string().into(),
            MetaValue::Number(v) => (*v).into(),
            MetaValue::Boolean(v) => (*v).into(),
            MetaValue::DateTime(d) => d.to_string().into(),
            MetaValue::Array(v) => v.iter().collect::<serde_json::Value>(),
            MetaValue::Map(m) => m.into(),
        }
    }
}

// TOML

impl From<&toml::Value> for MetaValue {
    fn from(value: &toml::Value) -> Self {
        match value {
            toml::Value::String(v) => MetaValue::String(v.to_string()),
            toml::Value::Integer(v) => MetaValue::Number(*v as f64),
            toml::Value::Float(v) => MetaValue::Number(*v),
            toml::Value::Array(v) => MetaValue::Array(v.iter().map(|n| n.into()).collect()),
            toml::Value::Boolean(v) => MetaValue::Boolean(*v),
            toml::Value::Table(v) => MetaValue::Map(v.into()),
            toml::Value::Datetime(v) => MetaValue::DateTime(DateTime::from_toml(v).unwrap()),
        }
    }
}

impl From<&toml::map::Map<String, toml::Value>> for Meta {
    fn from(value: &toml::map::Map<String, toml::Value>) -> Self {
        let mut meta = OrderMap::new();
        for (k, v) in value {
            meta.insert(k.to_string(), MetaValue::from(v));
        }
        Self(meta)
    }
}

// Liquid

impl model::ValueView for Meta {
    fn as_scalar(&self) -> Option<model::ScalarCow<'_>> {
        None
    }

    fn is_scalar(&self) -> bool {
        false
    }

    fn as_array(&self) -> Option<&dyn model::ArrayView> {
        None
    }

    fn is_array(&self) -> bool {
        false
    }

    fn as_object(&self) -> Option<&dyn model::ObjectView> {
        Some(self)
    }

    fn is_object(&self) -> bool {
        true
    }

    fn as_state(&self) -> Option<model::State> {
        None
    }

    fn is_state(&self) -> bool {
        false
    }

    fn is_nil(&self) -> bool {
        false
    }

    fn as_debug(&self) -> &dyn std::fmt::Debug {
        &self.0
    }

    fn render(&self) -> model::DisplayCow<'_> {
        todo!()
    }

    fn source(&self) -> model::DisplayCow<'_> {
        todo!()
    }

    fn type_name(&self) -> &'static str {
        "meta"
    }

    fn query_state(&self, _state: model::State) -> bool {
        false
    }

    fn to_kstr(&self) -> model::KStringCow<'_> {
        format!("{:?}", self.0).into()
    }

    fn to_value(&self) -> model::Value {
        let mut m = model::Object::new();
        for (k, v) in &self.0 {
            m.insert(k.into(), v.into());
        }
        model::Value::Object(m)
    }
}

impl model::ObjectView for Meta {
    fn as_value(&self) -> &dyn model::ValueView {
        self
    }

    fn size(&self) -> i64 {
        self.0.len() as i64
    }

    fn keys<'k>(&'k self) -> Box<dyn Iterator<Item = model::KStringCow<'k>> + 'k> {
        Box::new(self.0.keys().map(|k| k.into()))
    }

    fn values<'k>(&'k self) -> Box<dyn Iterator<Item = &'k dyn model::ValueView> + 'k> {
        Box::new(self.keys().map(|k| self.get(&k).unwrap()))
    }

    fn iter<'k>(
        &'k self,
    ) -> Box<dyn Iterator<Item = (model::KStringCow<'k>, &'k dyn model::ValueView)> + 'k> {
        todo!()
    }

    fn contains_key(&self, index: &str) -> bool {
        self.0.contains_key(index)
    }

    fn get<'s>(&'s self, index: &str) -> Option<&'s dyn model::ValueView> {
        if let Some(o) = self.0.get(index) {
            Some(o)
        } else {
            None
        }
    }
}

impl model::ArrayView for MetaValue {
    fn first(&self) -> Option<&dyn model::ValueView> {
        self.get(0)
    }

    fn last(&self) -> Option<&dyn model::ValueView> {
        self.get(-1)
    }

    fn as_value(&self) -> &dyn model::ValueView {
        self
    }

    fn size(&self) -> i64 {
        if let MetaValue::Array(arr) = self {
            arr.len() as i64
        } else {
            0
        }
    }

    fn values<'k>(&'k self) -> Box<dyn Iterator<Item = &'k dyn model::ValueView> + 'k> {
        if let MetaValue::Array(arr) = self {
            Box::new(arr.values())
        } else {
            panic!("values called on non-array MetaValue")
        }
    }

    fn contains_key(&self, index: i64) -> bool {
        if let MetaValue::Array(arr) = self {
            arr.contains_key(index)
        } else {
            false
        }
    }

    fn get(&self, index: i64) -> Option<&dyn model::ValueView> {
        if let MetaValue::Array(arr) = self {
            arr.get(index)
        } else {
            None
        }
    }
}

impl model::ValueView for MetaValue {
    fn as_scalar(&self) -> Option<model::ScalarCow<'_>> {
        match self {
            MetaValue::String(s) => Some(s.to_string().into()),
            MetaValue::Number(v) => Some((*v).into()),
            MetaValue::Boolean(v) => Some((*v).into()),
            MetaValue::DateTime(d) => Some(d.as_liquid_datetime().into()),
            _ => None,
        }
    }

    fn as_array(&self) -> Option<&dyn model::ArrayView> {
        if let MetaValue::Array(v) = self {
            Some(v)
        } else {
            None
        }
    }
    fn as_object(&self) -> Option<&dyn model::ObjectView> {
        if let MetaValue::Map(m) = self {
            Some(m)
        } else {
            None
        }
    }

    fn as_debug(&self) -> &dyn std::fmt::Debug {
        self
    }

    fn render(&self) -> model::DisplayCow<'_> {
        match self {
            MetaValue::String(s) => s.render(),
            MetaValue::Number(v) => v.render(),
            MetaValue::Boolean(v) => v.render(),
            MetaValue::DateTime(d) => d.borrowed_as_datetime().render(),
            _ => todo!("MetaValue render not implemented for non-scalar values"),
        }
    }

    fn source(&self) -> model::DisplayCow<'_> {
        todo!("MetaValue source not implemented")
    }

    fn type_name(&self) -> &'static str {
        match self {
            MetaValue::String(_) => "meta:string",
            MetaValue::Number(_) => "meta:number",
            MetaValue::Boolean(_) => "meta:boolean",
            MetaValue::DateTime(_) => "meta:datetime",
            MetaValue::Array(_) => "meta:array",
            MetaValue::Map(_) => "meta:map",
        }
    }

    fn query_state(&self, _state: model::State) -> bool {
        false
    }

    fn to_kstr(&self) -> model::KStringCow<'_> {
        format!("{:?}", self).into()
    }

    fn to_value(&self) -> model::Value {
        self.into()
    }
}