tilo-resource 0.5.0

The Tilo resource layer: typed, hierarchical app resources.
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
//! An enum whose variants carry fields is a model type: stored as a map
//! naming its variant, its fields stored by variant, merged field by field,
//! and migrated from the shapes older versions stored.

use automerge::ReadDoc;
use tilo_resource::evolution;
use tilo_resource::{Invoke, Model, ModelError, Replica, Resource, VARIANT_KEY, resource};

#[derive(Model, Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum Matcher {
    Host { name: String, port: u16 },
    Path(String),
    Anything,
}

/// Two variants naming a field alike, with types that do not fit each other:
/// a box `width` units wide, and text wrapping at `width` units or not at
/// all.
#[derive(Model, Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum Figure {
    Rect { width: f64 },
    Text { width: Option<f64> },
}

/// Two variants naming a field `value` stored as a text and as a number.
#[derive(Model, Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum Either {
    Text { value: String },
    Count { value: u64 },
}

/// Variant and field names holding the storage key's delimiters.
#[derive(Model, Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum Escaped {
    #[model(rename = "left/right")]
    Slash {
        #[model(rename = "value/one")]
        value: String,
    },
    #[model(rename = "left%2Fright")]
    Percent {
        #[model(rename = "value/one")]
        value: u64,
    },
}

/// A unit enum whose JSON form serde spells in kebab case: what a value reads
/// and what a caller writes are that spelling, not the document's key.
#[derive(Model, Default, Clone, Copy, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum State {
    #[default]
    NotStarted,
    InProgress,
}

#[derive(Model, Default, Clone, Debug, PartialEq)]
pub struct Rules {
    pub matcher: Option<Matcher>,
    pub all: Vec<Matcher>,
}

#[derive(Model, Default, Clone, Debug, PartialEq)]
pub struct Card {
    pub figure: Option<Figure>,
    pub either: Option<Either>,
}

#[resource(kind = "test.card", catalogue = "tests/locales")]
impl Card {
    /// Draw a box `width` units wide.
    #[action]
    pub fn draw_rect(&mut self, width: f64) {
        self.figure = Some(Figure::Rect { width });
    }

    /// Draw text wrapping at `width` units, or not wrapping.
    #[action]
    pub fn draw_text(&mut self, width: Option<f64>) {
        self.figure = Some(Figure::Text { width });
    }

    /// Make the box `width` units wide, when the figure is a box.
    #[action]
    pub fn widen(&mut self, width: f64) {
        if let Some(Figure::Rect { width: drawn }) = &mut self.figure {
            *drawn = width;
        }
    }

    /// Wrap the text at `width` units, when the figure is text.
    #[action]
    pub fn set_wrap(&mut self, width: f64) {
        if let Some(Figure::Text { width: drawn }) = &mut self.figure {
            *drawn = Some(width);
        }
    }

    /// Make the text, saying `value`.
    #[action]
    pub fn text(&mut self, value: String) {
        self.either = Some(Either::Text { value });
    }

    /// Make the count, counting `value`.
    #[action]
    pub fn count(&mut self, value: u64) {
        self.either = Some(Either::Count { value });
    }
}

#[resource(kind = "test.rules", version = 3, migrate = Rules::upgrade, catalogue = "tests/locales")]
impl Rules {
    /// Match a host.
    #[action]
    pub fn match_host(&mut self, name: String, port: u16) {
        self.matcher = Some(Matcher::Host { name, port });
    }

    /// Match a path.
    #[action]
    pub fn match_path(&mut self, path: String) {
        self.matcher = Some(Matcher::Path(path));
    }

    /// Rename the host matched.
    #[action]
    pub fn rename_host(&mut self, to: String) {
        if let Some(Matcher::Host { name, .. }) = &mut self.matcher {
            *name = to;
        }
    }

    /// Change the port matched.
    #[action]
    pub fn set_port(&mut self, to: u16) {
        if let Some(Matcher::Host { port, .. }) = &mut self.matcher {
            *port = to;
        }
    }

    fn upgrade(doc: &mut tilo_resource::Doc, from: u32) -> Result<(), ModelError> {
        match from {
            // Each value was one JSON string.
            1 => {
                evolution::retype_json::<Option<Matcher>>(doc, &automerge::ROOT, "matcher")?;
                evolution::retype_json::<Vec<Matcher>>(doc, &automerge::ROOT, "all")?;
            }
            // A variant's fields were flat, beside its name.
            2 => {
                evolution::retype_variant_fields::<Option<Matcher>>(
                    doc,
                    &automerge::ROOT,
                    "matcher",
                )?;
                evolution::retype_variant_fields::<Vec<Matcher>>(doc, &automerge::ROOT, "all")?;
            }
            _ => return Err(ModelError::invalid(format!("no migration from {from}"))),
        }
        Ok(())
    }
}

tilo_test_env::scenario! {
    fn a_data_enum_stores_merges_and_migrates_by_variant;

    phase a_data_enum_round_trips_through_the_document_and_json;
    {
        let mut replica = Replica::<Rules>::new().in_app("so.tilo.model", "Model");
        let host = Matcher::Host {
            name: "example.org".into(),
            port: 443,
        };
        replica
            .apply(Rules::MATCH_HOST.with(("example.org".to_owned(), 443_u16)))
            .unwrap();
        assert_eq!(replica.state().unwrap().matcher, Some(host.clone()));
        for value in [host, Matcher::Path("/a".into()), Matcher::Anything] {
            assert_eq!(Matcher::from_json(&value.to_json()).unwrap(), value);
        }
        assert_eq!(
            Matcher::Path("/a".into()).to_json(),
            serde_json::json!({ VARIANT_KEY: "Path", "0": "/a" })
        );
    }

    phase a_unit_enums_json_form_is_what_serde_spells;
    {
        let entry = State::InProgress.to_json();
        assert_eq!(entry, serde_json::json!("in-progress"));
        assert_eq!(State::from_json(&entry).unwrap(), State::InProgress);
        assert_eq!(
            State::value_schema(),
            serde_json::json!({"type": "string", "enum": ["not-started", "in-progress"]})
        );
        let mut types = tilo_resource::surface::TypeSet::default();
        let field = <State as Model>::field_type(&mut types);
        assert_eq!(field, tilo_resource::FieldType::Named("State".into()));
    }

    phase concurrent_edits_of_one_variant_merge_field_by_field;
    {
        let mut left = Replica::<Rules>::new().in_app("so.tilo.model", "Model");
        left.apply(Rules::MATCH_HOST.with(("example.org".to_owned(), 80_u16)))
            .unwrap();
        let mut right = Replica::<Rules>::from_doc(left.doc_mut().fork()).in_app("so.tilo.model", "Model");
        left.apply(Rules::RENAME_HOST.with("tilo.example".to_owned()))
            .unwrap();
        right.apply(Rules::SET_PORT.with(8080_u16)).unwrap();
        tilo_automerge::ApplyIncoming::merge_incoming(left.doc_mut(), right.doc_mut()).unwrap();
        let merged = Rules::read(left.doc()).unwrap();
        assert_eq!(
            merged.matcher,
            Some(Matcher::Host {
                name: "tilo.example".into(),
                port: 8080,
            }),
            "each side's field survives"
        );
    }

    phase changing_the_variant_removes_the_fields_of_the_old_one;
    {
        let mut replica = Replica::<Rules>::new().in_app("so.tilo.model", "Model");
        replica
            .apply(Rules::MATCH_HOST.with(("example.org".to_owned(), 80_u16)))
            .unwrap();
        replica
            .apply(Rules::MATCH_PATH.with("/b".to_owned()))
            .unwrap();
        let doc = replica.doc();
        let (_, matcher) = doc.get(automerge::ROOT, "matcher").unwrap().unwrap();
        let mut keys: Vec<String> = doc.keys(&matcher).collect();
        keys.sort();
        assert_eq!(
            keys,
            vec!["$field/path/0".to_owned(), VARIANT_KEY.to_owned()]
        );
        assert_eq!(
            replica.state().unwrap().matcher,
            Some(Matcher::Path("/b".into()))
        );
    }

    phase a_json_field_of_the_old_shape_migrates_to_the_data_enum;
    {
        let mut doc = old_document();
        assert!(
            evolution::read_checked::<Rules>(&doc).is_err(),
            "unmigrated"
        );
        assert!(evolution::migrate::<Rules>(&mut doc).unwrap());
        let rules = evolution::read_checked::<Rules>(&doc).unwrap();
        assert_eq!(
            rules.matcher,
            Some(Matcher::Host {
                name: "old.example".into(),
                port: 22,
            })
        );
        assert_eq!(
            rules.all,
            vec![Matcher::Anything, Matcher::Path("/x".into())]
        );
    }

    phase the_active_variant_never_reads_another_variants_field_of_the_same_name;
    // One person edits a field of the active variant while another changes the
    // variant to one naming a field alike. However the two merge, the variant
    // reads its own field: never the other's value in its place (which an
    // `Option` would read without complaint), and never a value of another
    // type that leaves the document unreadable.
    {
        let merged = |first: Invoke<Card>, ours: Invoke<Card>, theirs: Invoke<Card>| {
            let mut left = Replica::<Card>::new().in_app("so.tilo.model", "Model");
            left.apply(first).unwrap();
            let mut right = Replica::<Card>::from_doc(left.doc_mut().fork()).in_app("so.tilo.model", "Model");
            left.apply(ours).unwrap();
            right.apply(theirs).unwrap();
            tilo_automerge::ApplyIncoming::merge_incoming(left.doc_mut(), right.doc_mut()).unwrap();
            Card::read(left.doc()).unwrap()
        };

        let card = merged(
            Card::DRAW_RECT.with(120.0),
            Card::WIDEN.with(80.0),
            Card::DRAW_TEXT.with(None),
        );
        assert_eq!(
            card.figure,
            Some(Figure::Text { width: None }),
            "the text's own width, not the box's 80 read as where it wraps"
        );

        let card = merged(
            Card::DRAW_TEXT.with(Some(40.0)),
            Card::SET_WRAP.with(60.0),
            Card::DRAW_RECT.with(10.0),
        );
        assert_eq!(
            card.figure,
            Some(Figure::Rect { width: 10.0 }),
            "the box's own width, not the text's 60"
        );

        let card = merged(
            Card::TEXT.with("hello".to_owned()),
            Card::TEXT.with("world".to_owned()),
            Card::COUNT.with(7_u64),
        );
        assert_eq!(
            card.either,
            Some(Either::Count { value: 7 }),
            "the count reads its own number, not the text's"
        );
    }

    phase a_variants_fields_are_stored_under_keys_of_its_own;
    // A variant's fields are stored under keys of its own, escaped so that no
    // two variants' fields share one.
    {
        let mut doc = tilo_resource::Doc::new();
        let keys = |doc: &tilo_resource::Doc| {
            let (_, stored) = doc.get(automerge::ROOT, "value").unwrap().unwrap();
            let mut keys: Vec<String> = doc.keys(&stored).collect();
            keys.sort();
            keys
        };
        let slash = Escaped::Slash { value: "a".into() };
        tilo_resource::write_key(&mut doc, &automerge::ROOT, "value", &slash).unwrap();
        assert_eq!(
            keys(&doc),
            vec![
                "$field/left%2Fright/value%2Fone".to_owned(),
                VARIANT_KEY.to_owned()
            ]
        );
        assert_eq!(
            tilo_resource::read_key::<Escaped>(&doc, &automerge::ROOT, "value").unwrap(),
            slash
        );
        let percent = Escaped::Percent { value: 3 };
        tilo_resource::write_key(&mut doc, &automerge::ROOT, "value", &percent).unwrap();
        assert_eq!(
            keys(&doc),
            vec![
                "$field/left%252Fright/value%2Fone".to_owned(),
                VARIANT_KEY.to_owned()
            ]
        );
        assert_eq!(
            tilo_resource::read_key::<Escaped>(&doc, &automerge::ROOT, "value").unwrap(),
            percent
        );
    }

    phase a_flat_variant_reads_as_written_and_migrates_to_fields_by_variant;
    // A document of the flat layout reads as it was written, and its migration
    // stores each field by its variant, reading what the document holds.
    {
        let read = |doc: &tilo_resource::Doc| {
            (
                tilo_resource::read_key::<Option<Matcher>>(doc, &automerge::ROOT, "matcher").unwrap(),
                tilo_resource::read_key::<Vec<Matcher>>(doc, &automerge::ROOT, "all").unwrap(),
            )
        };
        let held = (
            Some(Matcher::Host {
                name: "flat.example".into(),
                port: 2222,
            }),
            vec![Matcher::Anything, Matcher::Path("/y".into())],
        );
        let mut doc = version_two_document();
        assert_eq!(read(&doc), held, "the flat fields are the variant's");
        assert!(
            evolution::read_checked::<Rules>(&doc).is_err(),
            "unmigrated"
        );

        assert!(evolution::migrate::<Rules>(&mut doc).unwrap());
        assert_eq!(evolution::stored_version(&doc), 3);
        assert_eq!(read(&doc), held, "the value is what it was");
        let rules = evolution::read_checked::<Rules>(&doc).unwrap();
        assert_eq!((rules.matcher, rules.all), held);
        let (_, stored) = doc.get(automerge::ROOT, "matcher").unwrap().unwrap();
        let mut keys: Vec<String> = doc.keys(&stored).collect();
        keys.sort();
        assert_eq!(
            keys,
            vec![
                "$field/host/name".to_owned(),
                "$field/host/port".to_owned(),
                VARIANT_KEY.to_owned(),
            ],
            "stored by variant, the flat keys gone"
        );
    }
}

/// The old shape: each field one JSON string, as a field of JSON text stored it.
fn old_document() -> tilo_resource::Doc {
    let mut doc = tilo_resource::Doc::new();
    let matcher = serde_json::to_string(&Some(Matcher::Host {
        name: "old.example".into(),
        port: 22,
    }))
    .unwrap();
    let all = serde_json::to_string(&vec![Matcher::Anything, Matcher::Path("/x".into())]).unwrap();
    tilo_resource::write_key(
        &mut doc,
        &automerge::ROOT,
        "matcher",
        &tilo_resource::Atom(matcher),
    )
    .unwrap();
    tilo_resource::write_key(&mut doc, &automerge::ROOT, "all", &tilo_resource::Atom(all)).unwrap();
    doc.commit();
    doc
}

/// A document of the layout version 2 wrote: a variant's name under
/// `$variant`, and its fields flat beside it.
fn version_two_document() -> tilo_resource::Doc {
    let mut doc = tilo_resource::Doc::new();
    evolution::fixture(
        &mut doc,
        &automerge::ROOT,
        &serde_json::json!({
            "matcher": { VARIANT_KEY: "host", "name": {"$text": "flat.example"}, "port": 2222 },
            "all": [{ VARIANT_KEY: "anything" }, { VARIANT_KEY: "path", "0": {"$text": "/y"} }],
            (evolution::VERSION_KEY): 2,
        }),
    )
    .unwrap();
    doc.commit();
    doc
}