zenith-core 0.0.5

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
//! Validation tests for the `pattern` node.
//!
//! Covers id-uniqueness, motif template isolation, complete token-ref
//! collection on all visual props (including border/stroke-outer/blur), and
//! every pattern-specific semantic diagnostic.

mod common;

use common::*;

/// A pattern whose id duplicates another node's id fires `id.duplicate` — this
/// proves the pattern's own id participates in id-uniqueness.
#[test]
fn pattern_duplicate_id_fires_id_duplicate() {
    let src = r##"zenith version=1 {
  project id="proj.dup" name="Dup"
  tokens format="zenith-token-v1" {
    token id="color.dot" type="color" value="#334155"
  }
  styles {
  }
  document id="doc.dup" title="Dup" {
    page id="page.dup" w=(px)800 h=(px)600 {
      rect id="node.dup" x=(px)0 y=(px)0 w=(px)10 h=(px)10 fill=(token)"color.dot"
      pattern id="node.dup" kind="grid" x=(px)0 y=(px)0 w=(px)800 h=(px)600 fill=(token)"color.dot" {
        ellipse id="e.dot" w=(px)8 h=(px)8 fill=(token)"color.dot"
      }
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse must succeed");
    let report = validate(&doc);
    assert!(
        has_code(&report, "id.duplicate"),
        "pattern id duplicate must fire id.duplicate; got: {:?}",
        codes(&report)
    );
}

/// A pattern node alone validates without crashing AND its motif's id is NOT
/// collected: a motif whose id collides with a real sibling node must NOT
/// produce `id.duplicate` (the motif is a template, invisible to id-collection).
#[test]
fn pattern_motif_id_not_collected() {
    let src = r##"zenith version=1 {
  project id="proj.motif" name="Motif"
  tokens format="zenith-token-v1" {
    token id="color.dot" type="color" value="#334155"
  }
  styles {
  }
  document id="doc.motif" title="Motif" {
    page id="page.motif" w=(px)800 h=(px)600 {
      rect id="shared.id" x=(px)0 y=(px)0 w=(px)10 h=(px)10 fill=(token)"color.dot"
      pattern id="p.dots" kind="grid" x=(px)0 y=(px)0 w=(px)800 h=(px)600 spacing=(px)20 fill=(token)"color.dot" {
        ellipse id="shared.id" w=(px)8 h=(px)8 fill=(token)"color.dot"
      }
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse must succeed");
    let report = validate(&doc);
    // The motif shares an id with a real node, but the motif is a TEMPLATE and is
    // never id-collected — so NO id.duplicate is produced.
    assert!(
        !has_code(&report, "id.duplicate"),
        "motif id must NOT be collected (no id.duplicate); got: {:?}",
        codes(&report)
    );
    assert!(
        !report.has_errors(),
        "a clean pattern + motif must not error; got: {:?}",
        codes(&report)
    );
}

/// A valid grid pattern with all required fields and an in-range jitter
/// produces no pattern-specific diagnostics.
#[test]
fn pattern_valid_grid_no_diagnostics() {
    let src = r##"zenith version=1 {
  project id="proj.vg" name="ValidGrid"
  tokens format="zenith-token-v1" {
    token id="color.bg" type="color" value="#ffffff"
  }
  styles {
  }
  document id="doc.vg" title="ValidGrid" {
    page id="page.vg" w=(px)800 h=(px)600 {
      pattern id="p.grid" kind="grid" x=(px)0 y=(px)0 w=(px)800 h=(px)600 spacing=(px)24 jitter=0.2 fill=(token)"color.bg" {
        ellipse id="e.motif" w=(px)8 h=(px)8
      }
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse must succeed");
    let report = validate(&doc);
    let pattern_diags: Vec<&str> = codes(&report)
        .into_iter()
        .filter(|c| c.starts_with("pattern."))
        .collect();
    assert!(
        pattern_diags.is_empty(),
        "valid grid pattern must produce no pattern.* diagnostics; got: {:?}",
        pattern_diags
    );
}

/// A valid scatter pattern with count > 0 produces no pattern-specific
/// diagnostics.
#[test]
fn pattern_valid_scatter_no_diagnostics() {
    let src = r##"zenith version=1 {
  project id="proj.vs" name="ValidScatter"
  tokens format="zenith-token-v1" {
    token id="color.dot" type="color" value="#334155"
  }
  styles {
  }
  document id="doc.vs" title="ValidScatter" {
    page id="page.vs" w=(px)800 h=(px)600 {
      pattern id="p.scatter" kind="scatter" x=(px)0 y=(px)0 w=(px)800 h=(px)600 count=50 fill=(token)"color.dot" {
        ellipse id="e.motif" w=(px)6 h=(px)6
      }
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse must succeed");
    let report = validate(&doc);
    let pattern_diags: Vec<&str> = codes(&report)
        .into_iter()
        .filter(|c| c.starts_with("pattern."))
        .collect();
    assert!(
        pattern_diags.is_empty(),
        "valid scatter pattern must produce no pattern.* diagnostics; got: {:?}",
        pattern_diags
    );
}

/// An unrecognized kind string fires `pattern.unknown_kind` and does NOT also
/// fire kind-specific requirement errors (e.g. grid_missing_spacing).
#[test]
fn pattern_unknown_kind_fires_diagnostic() {
    let src = r##"zenith version=1 {
  project id="proj.uk" name="UnknownKind"
  tokens format="zenith-token-v1" {
  }
  styles {
  }
  document id="doc.uk" title="UnknownKind" {
    page id="page.uk" w=(px)800 h=(px)600 {
      pattern id="p.bad" kind="hexagonal" x=(px)0 y=(px)0 w=(px)800 h=(px)600 {
        ellipse id="e.m" w=(px)8 h=(px)8
      }
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse must succeed");
    let report = validate(&doc);
    assert!(
        has_code(&report, "pattern.unknown_kind"),
        "unknown kind must fire pattern.unknown_kind; got: {:?}",
        codes(&report)
    );
    assert!(
        !has_code(&report, "pattern.grid_missing_spacing"),
        "unknown kind must NOT fire grid_missing_spacing; got: {:?}",
        codes(&report)
    );
    assert!(
        !has_code(&report, "pattern.scatter_missing_count"),
        "unknown kind must NOT fire scatter_missing_count; got: {:?}",
        codes(&report)
    );
}

/// A grid pattern without a spacing value fires `pattern.grid_missing_spacing`.
#[test]
fn pattern_grid_missing_spacing() {
    let src = r##"zenith version=1 {
  project id="proj.gms" name="GridNoSpacing"
  tokens format="zenith-token-v1" {
  }
  styles {
  }
  document id="doc.gms" title="GridNoSpacing" {
    page id="page.gms" w=(px)800 h=(px)600 {
      pattern id="p.grid" kind="grid" x=(px)0 y=(px)0 w=(px)800 h=(px)600 {
        ellipse id="e.m" w=(px)8 h=(px)8
      }
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse must succeed");
    let report = validate(&doc);
    assert!(
        has_code(&report, "pattern.grid_missing_spacing"),
        "grid without spacing must fire pattern.grid_missing_spacing; got: {:?}",
        codes(&report)
    );
}

/// A scatter pattern without a count value fires `pattern.scatter_missing_count`.
#[test]
fn pattern_scatter_missing_count() {
    let src = r##"zenith version=1 {
  project id="proj.smc" name="ScatterNoCount"
  tokens format="zenith-token-v1" {
  }
  styles {
  }
  document id="doc.smc" title="ScatterNoCount" {
    page id="page.smc" w=(px)800 h=(px)600 {
      pattern id="p.scatter" kind="scatter" x=(px)0 y=(px)0 w=(px)800 h=(px)600 {
        ellipse id="e.m" w=(px)8 h=(px)8
      }
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse must succeed");
    let report = validate(&doc);
    assert!(
        has_code(&report, "pattern.scatter_missing_count"),
        "scatter without count must fire pattern.scatter_missing_count; got: {:?}",
        codes(&report)
    );
}

/// A pattern with count=0 fires `pattern.invalid_count`.
#[test]
fn pattern_invalid_count_zero() {
    let src = r##"zenith version=1 {
  project id="proj.ic" name="InvalidCount"
  tokens format="zenith-token-v1" {
  }
  styles {
  }
  document id="doc.ic" title="InvalidCount" {
    page id="page.ic" w=(px)800 h=(px)600 {
      pattern id="p.scatter" kind="scatter" x=(px)0 y=(px)0 w=(px)800 h=(px)600 count=0 {
        ellipse id="e.m" w=(px)8 h=(px)8
      }
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse must succeed");
    let report = validate(&doc);
    assert!(
        has_code(&report, "pattern.invalid_count"),
        "count=0 must fire pattern.invalid_count; got: {:?}",
        codes(&report)
    );
}

/// A pattern with count=-5 fires `pattern.invalid_count`.
#[test]
fn pattern_invalid_count_negative() {
    let src = r##"zenith version=1 {
  project id="proj.icn" name="InvalidCountNeg"
  tokens format="zenith-token-v1" {
  }
  styles {
  }
  document id="doc.icn" title="InvalidCountNeg" {
    page id="page.icn" w=(px)800 h=(px)600 {
      pattern id="p.scatter" kind="scatter" x=(px)0 y=(px)0 w=(px)800 h=(px)600 count=-5 {
        ellipse id="e.m" w=(px)8 h=(px)8
      }
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse must succeed");
    let report = validate(&doc);
    assert!(
        has_code(&report, "pattern.invalid_count"),
        "count=-5 must fire pattern.invalid_count; got: {:?}",
        codes(&report)
    );
}

/// A pattern with spacing=0 fires `pattern.invalid_spacing`.
#[test]
fn pattern_invalid_spacing_zero() {
    let src = r##"zenith version=1 {
  project id="proj.is" name="InvalidSpacing"
  tokens format="zenith-token-v1" {
  }
  styles {
  }
  document id="doc.is" title="InvalidSpacing" {
    page id="page.is" w=(px)800 h=(px)600 {
      pattern id="p.grid" kind="grid" x=(px)0 y=(px)0 w=(px)800 h=(px)600 spacing=(px)0 {
        ellipse id="e.m" w=(px)8 h=(px)8
      }
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse must succeed");
    let report = validate(&doc);
    assert!(
        has_code(&report, "pattern.invalid_spacing"),
        "spacing=0 must fire pattern.invalid_spacing; got: {:?}",
        codes(&report)
    );
}

/// A pattern with jitter=1.5 fires `pattern.jitter_out_of_range` as a warning
/// (not an error).
#[test]
fn pattern_jitter_out_of_range_warning() {
    let src = r##"zenith version=1 {
  project id="proj.jor" name="JitterOOR"
  tokens format="zenith-token-v1" {
  }
  styles {
  }
  document id="doc.jor" title="JitterOOR" {
    page id="page.jor" w=(px)800 h=(px)600 {
      pattern id="p.grid" kind="grid" x=(px)0 y=(px)0 w=(px)800 h=(px)600 spacing=(px)20 jitter=1.5 {
        ellipse id="e.m" w=(px)8 h=(px)8
      }
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse must succeed");
    let report = validate(&doc);
    assert!(
        has_code(&report, "pattern.jitter_out_of_range"),
        "jitter=1.5 must fire pattern.jitter_out_of_range; got: {:?}",
        codes(&report)
    );
    // Must be a warning, not an error.
    let is_warning = report
        .diagnostics
        .iter()
        .any(|d| d.code == "pattern.jitter_out_of_range" && d.severity == Severity::Warning);
    assert!(
        is_warning,
        "pattern.jitter_out_of_range must be a Warning, not an Error; got: {:?}",
        codes(&report)
    );
    assert!(
        !report.has_errors(),
        "jitter out of range must not produce any errors; got: {:?}",
        codes(&report)
    );
}

/// A token used ONLY on a pattern's `border_top` must NOT be flagged as
/// `token.unused`. This proves that the complete visual-prop token-ref
/// collection covers the border/stroke-outer family.
#[test]
fn pattern_border_token_not_flagged_unused() {
    let src = r##"zenith version=1 {
  project id="proj.bt" name="BorderToken"
  tokens format="zenith-token-v1" {
    token id="color.border" type="color" value="#0000ff"
  }
  styles {
  }
  document id="doc.bt" title="BorderToken" {
    page id="page.bt" w=(px)800 h=(px)600 {
      pattern id="p.grid" kind="grid" x=(px)0 y=(px)0 w=(px)800 h=(px)600 spacing=(px)20 border-top=(token)"color.border" {
        ellipse id="e.m" w=(px)8 h=(px)8
      }
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse must succeed");
    let report = validate(&doc);
    assert!(
        !has_code(&report, "token.unused"),
        "token used only on pattern border-top must NOT be flagged token.unused; got: {:?}",
        codes(&report)
    );
}

/// A token used ONLY inside a pattern's motif must NOT be flagged
/// `token.unused`. The motif renders (replicated per instance), so the motif
/// subtree is walked for token references even though its ids are not collected.
#[test]
fn pattern_motif_token_not_flagged_unused() {
    let src = r##"zenith version=1 {
  project id="proj.mt" name="MotifToken"
  tokens format="zenith-token-v1" {
    token id="color.dot" type="color" value="#fbbf24"
  }
  styles {
  }
  document id="doc.mt" title="MotifToken" {
    page id="page.mt" w=(px)800 h=(px)600 {
      pattern id="p.grid" kind="grid" x=(px)0 y=(px)0 w=(px)800 h=(px)600 spacing=(px)20 {
        ellipse id="e.m" w=(px)8 h=(px)8 fill=(token)"color.dot"
      }
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse must succeed");
    let report = validate(&doc);
    assert!(
        !has_code(&report, "token.unused"),
        "token used only inside a pattern motif must NOT be flagged token.unused; got: {:?}",
        codes(&report)
    );
}