appcore-filemaker 0.1.0-beta.1

Deterministic declarative document, canvas, and dataset compiler for AppCore
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
// =============================================================================
//        #######
//     ###       ###     F: compiler.rs
//    ##   ## ##   ##    P: AppCore-Runtime
//         ## ##
//                       C: 2026/08/30 05:00:00 by dnettoRaw
//    ##   ## ##   ##    U: 2026/08/30 05:00:00 by dnettoRaw
//      ###########      S: 1.0.2-rc
// =============================================================================

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;
    use std::sync::Arc;

    use appcore_filemaker::*;

    #[test]
    fn expands_sandboxed_include_and_component() {
        let root = br"filemaker: '1.0'
model: document
id: root
includes:
  - path: fragment.yaml
    namespace: extra
components:
  label:
    props: { value: Default }
    elements:
      - { id: text, type: text, text: '{{value}}' }
elements:
  - { id: greeting, type: group, component: label, props: { value: Hello } }
";
        let fragment = br"filemaker: '1.0'
model: document
id: fragment
elements:
  - { id: box, type: rect, width: 10mm, height: 10mm }
";
        let mut resolver = MemoryResolver::default();
        resolver
            .insert("fragment.yaml", "application/yaml", fragment.to_vec())
            .unwrap();
        let compiler = Compiler::builder()
            .template_resolver(Arc::new(resolver))
            .build()
            .unwrap();
        let ir = compiler.compile_template_yaml(root).unwrap();
        assert_eq!(ir.elements[0].children[0].text.as_deref(), Some("Hello"));
        assert_eq!(ir.elements[1].id.as_str(), "extra/box");
    }

    #[test]
    fn patch_transaction_rolls_back_after_locked_failure() {
        let yaml = br"filemaker: '1.0'
model: document
id: root
elements:
  - { id: open, type: text, text: before }
  - { id: locked, type: text, text: fixed, locked: true }
";
        let compiler = Compiler::builder().build().unwrap();
        let template = compiler.compile_template_yaml(yaml).unwrap();
        let mut document = compiler
            .bind(&template, &DataValue::Object(BTreeMap::new()), &[])
            .unwrap();
        let original = document.clone();
        let patch = Patch {
            sequence: 7,
            operations: vec![
                appcore_filemaker::PatchOperation::SetText {
                    id: appcore_filemaker::ElementId::new("open").unwrap(),
                    text: "changed".to_owned(),
                },
                appcore_filemaker::PatchOperation::SetHidden {
                    id: appcore_filemaker::ElementId::new("locked").unwrap(),
                    hidden: true,
                },
            ],
        };
        let error = PatchTransaction::new(&mut document, 10)
            .apply(&patch)
            .unwrap_err();
        assert_eq!(error.code(), ErrorCode::PatchLocked);
        assert_eq!(document, original);
    }

    #[test]
    fn bind_bounds_patch_operations_across_the_complete_batch() {
        let limits = ResourceLimits {
            max_patch_operations: 1,
            ..ResourceLimits::default()
        };
        let compiler = Compiler::builder().limits(limits).build().unwrap();
        let template = compiler
            .compile_template_yaml(
                br"filemaker: '1.0'
model: canvas
id: bounded-patches
page: { width: 20pt, height: 20pt }
elements: [{ id: node, type: rect, width: 5pt, height: 5pt }]
",
            )
            .unwrap();
        let patches = [1, 2].map(|sequence| Patch {
            sequence,
            operations: vec![PatchOperation::SetHidden {
                id: ElementId::new("node").unwrap(),
                hidden: sequence == 1,
            }],
        });
        let error = compiler
            .bind(&template, &DataValue::Object(BTreeMap::new()), &patches)
            .unwrap_err();
        assert_eq!(error.code(), ErrorCode::LimitExceeded);
    }

    #[test]
    fn destructive_patch_cannot_bypass_a_locked_descendant() {
        let compiler = Compiler::builder().build().unwrap();
        let template = compiler
            .compile_template_yaml(
                br"filemaker: '1.0'
model: canvas
id: locked-subtree
page: { width: 20pt, height: 20pt }
elements:
  - id: parent
    type: group
    children:
      - { id: child, type: rect, width: 5pt, height: 5pt, locked: true }
",
            )
            .unwrap();
        let original = compiler
            .bind(&template, &DataValue::Object(BTreeMap::new()), &[])
            .unwrap();
        for operation in [
            PatchOperation::Remove {
                id: ElementId::new("parent").unwrap(),
            },
            PatchOperation::Replace {
                id: ElementId::new("parent").unwrap(),
                element: original.elements[0].clone(),
            },
        ] {
            let mut document = original.clone();
            let error = PatchTransaction::new(&mut document, 1)
                .apply(&Patch {
                    sequence: 1,
                    operations: vec![operation],
                })
                .unwrap_err();
            assert_eq!(error.code(), ErrorCode::PatchLocked);
            assert_eq!(document, original);
        }
    }

    #[test]
    fn move_and_resize_are_explicit_runtime_geometry_overrides() {
        let yaml = br"filemaker: '1.0'
model: canvas
id: override
collision: false
page: { width: 100pt, height: 100pt }
guides: { edge: 20pt }
elements:
  - id: box
    type: rect
    width: 20pt
    height: 10pt
    align_x: center
    anchors: { top: 'guide:edge' }
    constraints: { min_width: 15pt, max_width: 25pt, aspect_ratio: 2000000 }
";
        let limits = ResourceLimits::default();
        let compiler = Compiler::builder().build().unwrap();
        let template = compiler.compile_template_yaml(yaml).unwrap();
        let mut document = compiler
            .bind(&template, &DataValue::Object(BTreeMap::new()), &[])
            .unwrap();
        PatchTransaction::new(&mut document, 2)
            .apply(&Patch {
                sequence: 1,
                operations: vec![
                    PatchOperation::Move {
                        id: ElementId::new("box").unwrap(),
                        x: "5pt".parse().unwrap(),
                        y: "6pt".parse().unwrap(),
                    },
                    PatchOperation::Resize {
                        id: ElementId::new("box").unwrap(),
                        width: "30pt".parse().unwrap(),
                        height: "40pt".parse().unwrap(),
                    },
                ],
            })
            .unwrap();
        let scene = LayoutEngine::new(&limits, &FontManager::default(), LayoutOptions::default())
            .unwrap()
            .resolve(&document)
            .unwrap();
        assert_eq!(
            scene.pages[0].elements[0].bounds.layout,
            Rect::new(
                Unit::points(5).unwrap(),
                Unit::points(6).unwrap(),
                Unit::points(30).unwrap(),
                Unit::points(40).unwrap(),
            )
            .unwrap()
        );
    }

    #[test]
    fn applies_explicit_theme_tokens_and_cascade_order() {
        let yaml = br"filemaker: '1.0'
model: canvas
id: themed
page: { width: 20pt, height: 20pt }
theme: dark
themes:
  base:
    tokens: { foreground: '#111111', accent: '#224466' }
    style: { fill: '$foreground' }
  dark:
    extends: base
    tokens: { foreground: '#eeeeee' }
    style: { stroke: '$accent' }
style: { opacity: 900000 }
styles:
  highlighted: { fill: '$accent' }
elements:
  - id: box
    type: rect
    width: 10pt
    height: 10pt
    styles: [highlighted]
    style: { opacity: 800000 }
";
        let template = Compiler::builder()
            .build()
            .unwrap()
            .compile_template_yaml(yaml)
            .unwrap();
        let style = &template.elements[0].style;
        assert_eq!(
            style.fill,
            Some(appcore_filemaker::Color::Rgb {
                r: 34,
                g: 68,
                b: 102
            })
        );
        assert_eq!(
            style.stroke,
            Some(appcore_filemaker::Color::Rgb {
                r: 34,
                g: 68,
                b: 102
            })
        );
        assert_eq!(style.opacity, Some(800_000));
    }

    #[test]
    fn compiles_functional_and_typed_colors_without_losing_color_space() {
        let yaml = br"filemaker: '1.0'
model: canvas
id: colors
page: { width: 20pt, height: 20pt }
themes:
  print:
    tokens: { ink: 'cmyk(1000000, 250000, 0, 100000)' }
theme: print
elements:
  - id: box
    type: rect
    width: 10pt
    height: 10pt
    style:
      fill: { space: gray, value: 128 }
      stroke: '$ink'
      color: 'rgba(1, 2, 3, 4)'
";
        let template = Compiler::builder()
            .build()
            .unwrap()
            .compile_template_yaml(yaml)
            .unwrap();
        let style = &template.elements[0].style;
        assert_eq!(style.fill, Some(Color::Gray { value: 128 }));
        assert_eq!(
            style.stroke,
            Some(Color::Cmyk {
                c: 1_000_000,
                m: 250_000,
                y: 0,
                k: 100_000,
            })
        );
        assert_eq!(
            style.color,
            Some(Color::Rgba {
                r: 1,
                g: 2,
                b: 3,
                a: 4,
            })
        );
    }

    #[test]
    fn rejects_out_of_range_functional_and_typed_colors() {
        for yaml in [
            br"filemaker: '1.0'
model: canvas
id: invalid-rgb
page: { width: 20pt, height: 20pt }
elements:
  - { id: box, type: rect, width: 10pt, height: 10pt, style: { fill: 'rgb(256, 0, 0)' } }
"
            .as_slice(),
            br"filemaker: '1.0'
model: canvas
id: invalid-cmyk
page: { width: 20pt, height: 20pt }
elements:
  - id: box
    type: rect
    width: 10pt
    height: 10pt
    style: { fill: { space: cmyk, c: 1000001, m: 0, y: 0, k: 0 } }
"
            .as_slice(),
        ] {
            let error = Compiler::builder()
                .build()
                .unwrap()
                .compile_template_yaml(yaml)
                .unwrap_err();
            assert_eq!(error.code(), ErrorCode::SchemaField);
        }
    }

    #[test]
    fn style_cascade_reaches_data_runtime_and_paint_only_export_layers() {
        let yaml = br"filemaker: '1.0'
model: canvas
id: full-cascade
collision: false
page: { width: 20pt, height: 20pt }
themes:
  base:
    tokens: { alert: 'gray(128)' }
    style: { fill: red, stroke: black }
theme: base
style: { opacity: 900000 }
components:
  card:
    elements:
      - id: body
        type: rect
        width: 10pt
        height: 10pt
        style: { fill: blue }
        style_rules:
          - { when: 'data.highlight == true', style: { fill: '$alert' } }
elements:
  - { id: card, type: group, component: card, width: 20pt, height: 20pt }
";
        let limits = ResourceLimits::default();
        let compiler = Compiler::builder().build().unwrap();
        let template = compiler.compile_template_yaml(yaml).unwrap();
        let compiled = &template.elements[0].children[0];
        assert_eq!(compiled.style.fill, Some(Color::parse("blue").unwrap()));
        assert_eq!(compiled.style_rules.len(), 1);

        let data = DataValue::Object(BTreeMap::from([(
            "highlight".to_owned(),
            DataValue::Boolean(true),
        )]));
        let mut document = compiler.bind(&template, &data, &[]).unwrap();
        let body_id = ElementId::new("card/body").unwrap();
        let data_style = &document.elements[0].children[0].style;
        assert_eq!(data_style.fill, Some(Color::Gray { value: 128 }));
        assert!(document.elements[0].children[0].style_rules.is_empty());

        PatchTransaction::new(&mut document, 1)
            .apply(&Patch {
                sequence: 1,
                operations: vec![PatchOperation::SetStyle {
                    id: body_id,
                    style: Style {
                        fill: Some(Color::parse("green").unwrap()),
                        ..Style::default()
                    },
                }],
            })
            .unwrap();
        let scene = LayoutEngine::new(&limits, &FontManager::default(), LayoutOptions::default())
            .unwrap()
            .resolve(&document)
            .unwrap();
        let body = scene
            .pages
            .iter()
            .flat_map(|page| &page.elements)
            .find(|element| element.id.as_str() == "card/body")
            .unwrap();
        assert_eq!(body.style.fill, Some(Color::parse("green").unwrap()));

        let mut svg = Vec::new();
        export(
            &scene,
            &ExportRequest {
                style_override: Some(ExportStyleOverride {
                    fill: Some(Color::Rgb { r: 9, g: 8, b: 7 }),
                    ..ExportStyleOverride::default()
                }),
                ..ExportRequest::default()
            },
            &ExportContext {
                limits: &limits,
                fonts: &FontManager::default(),
                assets: None,
            },
            &mut svg,
        )
        .unwrap();
        assert!(String::from_utf8(svg).unwrap().contains("fill=\"#090807\""));
        assert_eq!(body.style.fill, Some(Color::parse("green").unwrap()));
    }

    #[test]
    fn invalid_runtime_style_rolls_back_the_complete_patch() {
        let compiler = Compiler::builder().build().unwrap();
        let template = compiler
            .compile_template_yaml(
                br"filemaker: '1.0'
model: canvas
id: rollback-style
page: { width: 20pt, height: 20pt }
elements:
  - { id: box, type: rect, width: 10pt, height: 10pt }
",
            )
            .unwrap();
        let mut document = compiler
            .bind(&template, &DataValue::Object(BTreeMap::new()), &[])
            .unwrap();
        let original = document.clone();
        let error = PatchTransaction::new(&mut document, 2)
            .apply(&Patch {
                sequence: 2,
                operations: vec![
                    PatchOperation::SetHidden {
                        id: ElementId::new("box").unwrap(),
                        hidden: true,
                    },
                    PatchOperation::SetStyle {
                        id: ElementId::new("box").unwrap(),
                        style: Style {
                            opacity: Some(1_000_001),
                            ..Style::default()
                        },
                    },
                ],
            })
            .unwrap_err();
        assert_eq!(error.code(), ErrorCode::SchemaField);
        assert_eq!(document, original);
    }

    #[test]
    fn rejects_theme_inheritance_cycles() {
        let yaml = br"filemaker: '1.0'
model: canvas
id: themed
page: { width: 20pt, height: 20pt }
theme: a
themes:
  a: { extends: b }
  b: { extends: a }
";
        let error = Compiler::builder()
            .build()
            .unwrap()
            .compile_template_yaml(yaml)
            .unwrap_err();
        assert_eq!(error.code(), ErrorCode::DataCycle);
    }
}