zenith-core 0.0.7

Zenith core: KDL parser adapter, semantic AST, canonical formatter, tokens, validation, and diagnostics.
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
//! Integration tests for the canonical writer: roundtrip.
//!
//! Idempotency, AST round-trip equality, number/literal formatting, canonical
//! property ordering, booleans, and `doc-id` — the writer's document-level core.
//!
//! Moved verbatim from the former in-`src` `format/writer/tests.rs`; the body of
//! every test is unchanged — only import paths were rewritten to the public
//! `zenith_core` surface. Span-stripping helpers live in `common`.

mod common;

use common::*;
use zenith_core::format::format_document;

/// A minimal `.zen` document used as the idempotency fixture.
const MINIMAL: &str = r##"zenith version=1 {
  project id="proj.test" name="Test Project"
  tokens format="zenith-token-v1" {
    token id="color.bg" type="color" value="#f8fafc"
    token id="size.title" type="dimension" value=(pt)48
    token id="font.weight.bold" type="fontWeight" value=700
    token id="lh.body" type="number" value=1.45
  }
  styles {
  }
  document id="doc.test" title="Test Doc" {
    page id="page.one" name="One" w=(px)640 h=(px)360 background=(token)"color.bg" {
      rect id="bg.rect" x=(px)0 y=(px)0 w=(px)640 h=(px)360 fill=(token)"color.bg"
      text id="label" x=(px)10 y=(px)10 w=(px)200 h=(px)50 align="center" fill=(token)"color.text" {
        span "Hello Zenith"
      }
    }
  }
}
"##;

/// A `.zen` document with a `code` node whose content stresses every escape
/// path: leading spaces, a blank line, a tab, an embedded quote, and a
/// literal backslash.
const CODE_DOC: &str = r##"zenith version=1 {
  project id="proj.code" name="Code Project"
  tokens format="zenith-token-v1" {
  }
  styles {
  }
  document id="doc.code" title="Code Doc" {
    page id="page.one" w=(px)640 h=(px)360 {
      code id="snippet" x=(px)96 y=(px)320 w=(px)560 h=(px)180 overflow="clip" language="rust" line-numbers=#false tab-width=4 {
        content "fn main() {\n    let path = \"c:\\\\tmp\";\n\n\tprintln!(\"hi\");\n}"
      }
    }
  }
}
"##;

/// **Idempotency test**: format once → `s1`; format again → `s2`; assert equal.
#[test]
fn test_idempotency() {
    let adapter = KdlAdapter;
    let doc1 = adapter
        .parse(MINIMAL.as_bytes())
        .expect("parse 1 must succeed");
    let s1 = format_document(&doc1).expect("format 1 must succeed");

    let doc2 = adapter.parse(&s1).expect("parse 2 must succeed");
    let s2 = format_document(&doc2).expect("format 2 must succeed");

    assert_eq!(
        String::from_utf8(s1.clone()).unwrap(),
        String::from_utf8(s2).unwrap(),
        "format must be idempotent"
    );
}

/// **Round-trip AST equality**: parse → format → parse must yield the same AST
/// (excluding source spans, which reflect byte positions in the original source).
#[test]
fn test_round_trip_ast_equality() {
    let adapter = KdlAdapter;
    let doc_orig = adapter.parse(MINIMAL.as_bytes()).expect("original parse");
    let formatted = format_document(&doc_orig).expect("format");
    let doc_reparsed = adapter.parse(&formatted).expect("re-parse after format");

    // Compare with spans stripped — spans are byte-position metadata that
    // legitimately differ between the original source and the reformatted
    // canonical form; they are not part of the document semantics.
    let orig_stripped = strip_spans(doc_orig);
    let reparsed_stripped = strip_spans(doc_reparsed);
    assert_eq!(
        orig_stripped, reparsed_stripped,
        "re-parsed AST must equal original (spans excluded)"
    );
}

/// **`baseline-grid` round-trip**: a page's `baseline-grid=(px)14` must survive
/// parse → format → parse, mirroring the `bleed` dimension round-trip.
#[test]
fn test_baseline_grid_round_trips() {
    let src = r##"zenith version=1 {
  project id="proj.bg" name="BG"
  tokens format="zenith-token-v1" {
  }
  styles {
  }
  document id="doc.bg" title="BG" {
    page id="page.one" w=(px)640 h=(px)360 baseline-grid=(px)14 {
      rect id="r" x=(px)0 y=(px)0 w=(px)10 h=(px)10 fill=(token)"c"
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse");
    let page = &doc.body.pages[0];
    assert!(
        page.baseline_grid.is_some(),
        "baseline-grid must parse onto the page"
    );

    let formatted = format_document(&doc).expect("format");
    let formatted_str = String::from_utf8(formatted.clone()).expect("utf8");
    assert!(
        formatted_str.contains("baseline-grid=(px)14"),
        "formatted output must contain baseline-grid; got:\n{formatted_str}"
    );

    let reparsed = adapter.parse(&formatted).expect("re-parse");
    assert_eq!(
        strip_spans(doc).body.pages[0].baseline_grid,
        strip_spans(reparsed).body.pages[0].baseline_grid,
        "baseline-grid must round-trip identically"
    );
}

/// **`font-size-min` round-trip**: a text node's `font-size-min=(token)"…"`
/// (the `overflow="autofit"` floor) must survive parse → format → parse,
/// mirroring `font-size`.
#[test]
fn test_font_size_min_round_trips() {
    let src = r##"zenith version=1 {
  project id="proj.fsm" name="FSM"
  tokens format="zenith-token-v1" {
    token id="size.title.min" type="dimension" value=(px)12
  }
  styles {
  }
  document id="doc.fsm" title="FSM" {
    page id="page.one" w=(px)640 h=(px)360 {
      text id="t" x=(px)0 y=(px)0 w=(px)200 h=(px)40 overflow="autofit" font-size-min=(token)"size.title.min" {
        span "Hi"
      }
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse");

    let formatted = format_document(&doc).expect("format");
    let formatted_str = String::from_utf8(formatted.clone()).expect("utf8");
    assert!(
        formatted_str.contains(r#"font-size-min=(token)"size.title.min""#),
        "formatted output must contain font-size-min; got:\n{formatted_str}"
    );

    let reparsed = adapter.parse(&formatted).expect("re-parse");
    assert_eq!(
        strip_spans(doc).body.pages[0].children,
        strip_spans(reparsed).body.pages[0].children,
        "font-size-min must round-trip identically"
    );
}

/// **Code content verbatim round-trip**: parse → format → parse must yield a
/// BYTE-IDENTICAL content blob, and the formatter must be idempotent.
#[test]
fn test_code_content_verbatim_round_trip() {
    let adapter = KdlAdapter;
    let doc1 = adapter.parse(CODE_DOC.as_bytes()).expect("parse 1");

    // Decoded content captured from the first parse.
    let original = match &doc1.body.pages[0].children[0] {
        Node::Code(c) => c.content.clone(),
        other => panic!("expected Code node, got {other:?}"),
    };
    // Sanity: the fixture really exercises every escape class.
    assert!(original.contains('\n') && original.contains('\t'));
    assert!(original.contains('"') && original.contains('\\'));

    let s1 = format_document(&doc1).expect("format 1");
    let doc2 = adapter.parse(&s1).expect("parse 2");
    let reparsed = match &doc2.body.pages[0].children[0] {
        Node::Code(c) => c.content.clone(),
        other => panic!("expected Code node, got {other:?}"),
    };
    assert_eq!(
        original, reparsed,
        "code content must round-trip byte-identically"
    );

    // Idempotency: format(format(doc)) == format(doc).
    let s2 = format_document(&doc2).expect("format 2");
    assert_eq!(
        String::from_utf8(s1).unwrap(),
        String::from_utf8(s2).unwrap(),
        "code formatting must be idempotent"
    );
}

/// **Number formatting**: token `(pt)48` must round-trip as `(pt)48`.
#[test]
fn test_pt_48_round_trips() {
    let src = r##"zenith version=1 {
  project id="proj.t" name="T"
  tokens format="zenith-token-v1" {
    token id="size.title" type="dimension" value=(pt)48
  }
  styles {
  }
  document id="doc.t" title="T" {
    page id="p" w=(px)100 h=(px)100 {
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse");
    let out = format_document(&doc).expect("format");
    let text = String::from_utf8(out).unwrap();
    assert!(
        text.contains("value=(pt)48"),
        "expected `value=(pt)48` in output, got:\n{text}"
    );
    assert!(
        !text.contains("(pt)48.0"),
        "must not contain (pt)48.0 in output"
    );
}

/// **Literal visual dimension round-trip**: a `stroke-width=(px)2` literal
/// must format as `(px)2` (not `(px)2.0`, not `"2"`) and re-parse back to a
/// `Dimension(2.0, Px)`.
#[test]
fn test_literal_dimension_round_trips() {
    use zenith_core::{Dimension, Node, PropertyValue, Unit};
    let src = r##"zenith version=1 {
  project id="proj.ld" name="LD"
  tokens format="zenith-token-v1" {
  }
  styles {
  }
  document id="doc.ld" title="LD" {
    page id="p" w=(px)100 h=(px)100 {
      rect id="r" x=(px)0 y=(px)0 w=(px)10 h=(px)10 stroke-width=(px)2
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse");
    let out = format_document(&doc).expect("format");
    let text = String::from_utf8(out).unwrap();
    assert!(
        text.contains("stroke-width=(px)2"),
        "expected `stroke-width=(px)2`, got:\n{text}"
    );
    assert!(
        !text.contains("(px)2.0"),
        "must not emit (px)2.0; got:\n{text}"
    );
    assert!(
        !text.contains("stroke-width=\"2\""),
        "must not emit a quoted literal; got:\n{text}"
    );

    // Re-parse the formatted output → still a Dimension(2.0, Px).
    let doc2 = adapter.parse(text.as_bytes()).expect("re-parse");
    match &doc2.body.pages[0].children[0] {
        Node::Rect(r) => assert_eq!(
            r.stroke_width,
            Some(PropertyValue::Dimension(Dimension {
                value: 2.0,
                unit: Unit::Px,
            }))
        ),
        other => panic!("expected Rect, got {other:?}"),
    }
}

/// **Canonical property order**: a rect with `fill` before `x` in source
/// must be formatted with `x` before `fill`.
#[test]
fn test_canonical_property_order_rect() {
    // Source has fill before x — non-canonical order.
    let src = r##"zenith version=1 {
  project id="proj.order" name="Order"
  tokens format="zenith-token-v1" {
    token id="color.bg" type="color" value="#ffffff"
  }
  styles {
  }
  document id="doc.order" title="Order" {
    page id="p" w=(px)100 h=(px)100 {
      rect id="r" fill=(token)"color.bg" x=(px)10 y=(px)20 w=(px)50 h=(px)50
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse");
    let out = format_document(&doc).expect("format");
    let text = String::from_utf8(out).unwrap();

    // Find positions of `x=` and `fill=` in the rect line.
    let rect_line = text
        .lines()
        .find(|l| l.trim_start().starts_with("rect"))
        .expect("must find rect line");
    let pos_x = rect_line.find(" x=").expect("must find x= on rect line");
    let pos_fill = rect_line
        .find(" fill=")
        .expect("must find fill= on rect line");
    assert!(
        pos_x < pos_fill,
        "x= must appear before fill= in canonical output; rect line: {rect_line:?}"
    );
}

/// **Booleans**: `visible=#false` must emit with `#false`, not `false` or `"false"`.
#[test]
fn test_boolean_format() {
    let src = r##"zenith version=1 {
  project id="proj.bool" name="Bool"
  tokens format="zenith-token-v1" {
  }
  styles {
  }
  document id="doc.bool" title="Bool" {
    page id="p" w=(px)100 h=(px)100 {
      rect id="r" x=(px)0 y=(px)0 w=(px)10 h=(px)10 visible=#false
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse");
    let out = format_document(&doc).expect("format");
    let text = String::from_utf8(out).unwrap();
    assert!(
        text.contains("visible=#false"),
        "expected `visible=#false`, got:\n{text}"
    );
}

/// **`doc-id` round-trip**: a root `zenith` node carrying `doc-id="my-doc-123"`
/// must parse onto `doc.doc_id`, be re-emitted verbatim by the formatter, and
/// survive a parse → format → parse cycle with byte-identical output.
#[test]
fn test_doc_id_round_trips() {
    let src = r##"zenith version=1 doc-id="my-doc-123" {
  project id="proj.did" name="DocId"
  tokens format="zenith-token-v1" {
    token id="color.bg" type="color" value="#ffffff"
  }
  styles {
  }
  document id="doc.did" title="DocId" {
    page id="page.one" w=(px)640 h=(px)360 {
      rect id="r" x=(px)0 y=(px)0 w=(px)640 h=(px)360 fill=(token)"color.bg"
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse must succeed");

    assert_eq!(
        doc.doc_id.as_deref(),
        Some("my-doc-123"),
        "doc-id must parse onto doc.doc_id"
    );

    let formatted = format_document(&doc).expect("format must succeed");
    let formatted_str = String::from_utf8(formatted.clone()).expect("utf8");

    assert!(
        formatted_str.contains("doc-id=\"my-doc-123\""),
        "formatted output must contain doc-id=\"my-doc-123\"; got:\n{formatted_str}"
    );

    let reparsed = adapter.parse(&formatted).expect("re-parse after format");
    assert_eq!(
        doc.doc_id, reparsed.doc_id,
        "doc_id must round-trip identically"
    );

    // Idempotency: format twice → byte-identical output.
    let formatted2 = format_document(&reparsed).expect("format 2 must succeed");
    assert_eq!(
        formatted, formatted2,
        "doc-id formatting must be idempotent"
    );
}

/// **`doc-id` absent is `None`**: a root `zenith` node without a `doc-id`
/// attribute must produce `doc.doc_id == None` after parsing.
#[test]
fn test_doc_id_absent_is_none() {
    let src = r##"zenith version=1 {
  project id="proj.nodid" name="NoDid"
  tokens format="zenith-token-v1" {
    token id="color.bg" type="color" value="#ffffff"
  }
  styles {
  }
  document id="doc.nodid" title="NoDid" {
    page id="page.one" w=(px)640 h=(px)360 {
      rect id="r" x=(px)0 y=(px)0 w=(px)640 h=(px)360 fill=(token)"color.bg"
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse must succeed");

    assert!(
        doc.doc_id.is_none(),
        "doc_id must be None when doc-id is absent from the zenith node"
    );
}

/// A document with NO assets block → empty AssetBlock (not an error).
#[test]
fn test_absent_assets_block_is_empty() {
    let adapter = KdlAdapter;
    let doc = adapter
        .parse(MINIMAL.as_bytes())
        .expect("parse must succeed");
    assert!(
        doc.assets.assets.is_empty(),
        "absent assets block must yield an empty AssetBlock"
    );
}