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
//! Per-kind checks for `shape`, `connector`, and `unknown` nodes.
//!
//! `check_unknown` emits the forward-compat warning and registers the optional
//! id; the dispatcher in [`super::super::nodes::walk_node`] performs the child
//! recursion so traversal order is unchanged.

use std::collections::BTreeSet;

use crate::ast::node::{ConnectorNode, ShapeNode, UnknownNode};
use crate::diagnostics::Diagnostic;

use super::shared::{
    AnchorParentCtx, AnchorProps, TokenEnv, check_anchor, check_optional_dim, check_spans,
    check_style_ref,
};
use super::suggest::check_unknown_props;
use crate::validate::check::nodes::WalkCtx;
use crate::validate::check::register_id;
use crate::validate::check::visual::{VisualExpect, check_visual_prop};

pub(in crate::validate::check) fn check_shape(
    s: &ShapeNode,
    ctx: WalkCtx,
    seen_ids: &mut BTreeSet<String>,
    referenced_token_ids: &mut BTreeSet<String>,
    geom_required: bool,
    parent_ctx: AnchorParentCtx,
    diagnostics: &mut Vec<Diagnostic>,
) {
    let WalkCtx {
        resolved_tokens,
        declared_style_ids,
        zone_ids,
        ..
    } = ctx;
    register_id(&s.id, seen_ids, diagnostics);
    check_style_ref(
        &s.id,
        s.style.as_deref(),
        declared_style_ids,
        s.source_span,
        diagnostics,
    );
    check_style_ref(
        &s.id,
        s.text_style.as_deref(),
        declared_style_ids,
        s.source_span,
        diagnostics,
    );

    // A recognized anchor supplies both x and y.
    let anchor_active = check_anchor(
        &s.id,
        AnchorProps {
            anchor: s.anchor.as_deref(),
            anchor_zone: s.anchor_zone.as_deref(),
            anchor_sibling: s.anchor_sibling.as_deref(),
            anchor_parent: s.anchor_parent == Some(true),
            anchor_edge: s.anchor_edge.as_deref(),
            anchor_gap: s.anchor_gap.as_ref(),
        },
        parent_ctx,
        zone_ids,
        s.source_span,
        diagnostics,
    );
    let xy_required = geom_required && !anchor_active;

    // Required geometry: x, y, w, h must all be present.
    {
        let mut tokens = TokenEnv {
            referenced: referenced_token_ids,
            resolved: resolved_tokens,
        };
        check_optional_dim(
            &s.id,
            "x",
            s.x.as_ref(),
            xy_required,
            s.source_span,
            &mut tokens,
            diagnostics,
        );
        check_optional_dim(
            &s.id,
            "y",
            s.y.as_ref(),
            xy_required,
            s.source_span,
            &mut tokens,
            diagnostics,
        );
        check_optional_dim(
            &s.id,
            "w",
            s.w.as_ref(),
            geom_required,
            s.source_span,
            &mut tokens,
            diagnostics,
        );
        check_optional_dim(
            &s.id,
            "h",
            s.h.as_ref(),
            geom_required,
            s.source_span,
            &mut tokens,
            diagnostics,
        );
    }

    // Visual properties — all token-required.
    check_visual_prop(
        &s.id,
        "fill",
        s.fill.as_ref(),
        VisualExpect::Color,
        referenced_token_ids,
        resolved_tokens,
        diagnostics,
    );
    check_visual_prop(
        &s.id,
        "stroke",
        s.stroke.as_ref(),
        VisualExpect::Color,
        referenced_token_ids,
        resolved_tokens,
        diagnostics,
    );
    check_visual_prop(
        &s.id,
        "stroke-width",
        s.stroke_width.as_ref(),
        VisualExpect::Dimension,
        referenced_token_ids,
        resolved_tokens,
        diagnostics,
    );
    check_visual_prop(
        &s.id,
        "radius",
        s.radius.as_ref(),
        VisualExpect::Dimension,
        referenced_token_ids,
        resolved_tokens,
        diagnostics,
    );
    check_visual_prop(
        &s.id,
        "padding",
        s.padding.as_ref(),
        VisualExpect::Dimension,
        referenced_token_ids,
        resolved_tokens,
        diagnostics,
    );

    // Enum-value checks (Warnings on unrecognized values, not errors).
    if let Some(k) = s.kind.as_deref()
        && !matches!(k, "process" | "decision" | "terminator" | "ellipse")
    {
        diagnostics.push(Diagnostic::warning(
            "shape.unknown_kind",
            format!(
                "shape '{}': kind '{k}' is not one of \
                 process/decision/terminator/ellipse",
                s.id
            ),
            s.source_span,
            Some(s.id.clone()),
        ));
    }
    if let Some(sa) = s.stroke_alignment.as_deref()
        && !matches!(sa, "inside" | "center" | "outside")
    {
        diagnostics.push(Diagnostic::warning(
            "shape.invalid_stroke_alignment",
            format!(
                "shape '{}': stroke-alignment '{sa}' is not one of \
                 inside/center/outside",
                s.id
            ),
            s.source_span,
            Some(s.id.clone()),
        ));
    }
    if let Some(ha) = s.h_align.as_deref()
        && !matches!(ha, "start" | "center" | "end")
    {
        diagnostics.push(Diagnostic::warning(
            "shape.invalid_h_align",
            format!(
                "shape '{}': h-align '{ha}' is not one of start/center/end",
                s.id
            ),
            s.source_span,
            Some(s.id.clone()),
        ));
    }
    if let Some(va) = s.v_align.as_deref()
        && !matches!(va, "top" | "middle" | "bottom")
    {
        diagnostics.push(Diagnostic::warning(
            "shape.invalid_v_align",
            format!(
                "shape '{}': v-align '{va}' is not one of top/middle/bottom",
                s.id
            ),
            s.source_span,
            Some(s.id.clone()),
        ));
    }

    // Per-span visual properties (mirrors Node::Text). Registers token
    // refs so they are not falsely flagged as unused, and type-checks
    // fill/font-weight on each span.
    check_spans(
        &s.id,
        &s.spans,
        referenced_token_ids,
        resolved_tokens,
        diagnostics,
    );

    // Unknown properties.
    check_unknown_props("shape", &s.id, &s.unknown_props, s.source_span, diagnostics);
}

pub(in crate::validate::check) fn check_connector(
    c: &ConnectorNode,
    ctx: WalkCtx,
    seen_ids: &mut BTreeSet<String>,
    referenced_token_ids: &mut BTreeSet<String>,
    diagnostics: &mut Vec<Diagnostic>,
) {
    let WalkCtx {
        resolved_tokens,
        declared_style_ids,
        all_node_ids,
        ..
    } = ctx;
    register_id(&c.id, seen_ids, diagnostics);
    check_style_ref(
        &c.id,
        c.style.as_deref(),
        declared_style_ids,
        c.source_span,
        diagnostics,
    );

    // Stroke visual properties — token-required (connector has no fill).
    check_visual_prop(
        &c.id,
        "stroke",
        c.stroke.as_ref(),
        VisualExpect::Color,
        referenced_token_ids,
        resolved_tokens,
        diagnostics,
    );
    check_visual_prop(
        &c.id,
        "stroke-width",
        c.stroke_width.as_ref(),
        VisualExpect::Dimension,
        referenced_token_ids,
        resolved_tokens,
        diagnostics,
    );

    // Target existence: `from`/`to`, when present, must name an id in the
    // document. A reference to a missing id is advisory (the connector
    // simply renders nothing at compile time). Mirrors
    // `text-exclusion.unresolved_ref`.
    if let Some(target) = &c.from
        && !all_node_ids.contains(target)
    {
        diagnostics.push(Diagnostic::warning(
            "connector.unknown_target",
            format!(
                "connector '{}': from '{}' matches no node id in the document",
                c.id, target
            ),
            c.source_span,
            Some(c.id.clone()),
        ));
    }
    if let Some(target) = &c.to
        && !all_node_ids.contains(target)
    {
        diagnostics.push(Diagnostic::warning(
            "connector.unknown_target",
            format!(
                "connector '{}': to '{}' matches no node id in the document",
                c.id, target
            ),
            c.source_span,
            Some(c.id.clone()),
        ));
    }

    // Missing endpoints: a connector with no `from`/`to` can't route.
    if c.from.is_none() || c.to.is_none() {
        diagnostics.push(Diagnostic::warning(
            "connector.missing_target",
            format!(
                "connector '{}': both 'from' and 'to' are required to route",
                c.id
            ),
            c.source_span,
            Some(c.id.clone()),
        ));
    }

    // Enum-value checks (Warnings on unrecognized values, not errors).
    if let Some(r) = c.route.as_deref()
        && !matches!(r, "straight" | "orthogonal" | "avoid")
    {
        diagnostics.push(Diagnostic::warning(
            "connector.invalid_route",
            format!(
                "connector '{}': route '{r}' is not one of straight/orthogonal/avoid",
                c.id
            ),
            c.source_span,
            Some(c.id.clone()),
        ));
    }
    for (label, marker) in [
        ("marker-start", c.marker_start.as_deref()),
        ("marker-end", c.marker_end.as_deref()),
    ] {
        if let Some(m) = marker
            && !matches!(m, "none" | "arrow")
        {
            diagnostics.push(Diagnostic::warning(
                "connector.invalid_marker",
                format!(
                    "connector '{}': {label} '{m}' is not one of none/arrow",
                    c.id
                ),
                c.source_span,
                Some(c.id.clone()),
            ));
        }
    }
    // A connector anchor is `auto` or a nine-point grid position: one or more
    // hyphen-separated bands from top/bottom/left/right/center (mid/middle accepted
    // as center), e.g. `top`, `center`, `bottom-right`. Mirrors the scene resolver.
    fn is_valid_anchor(a: &str) -> bool {
        if a == "auto" {
            return true;
        }
        let mut recognized = false;
        for part in a.split('-') {
            match part {
                "top" | "bottom" | "left" | "right" | "center" | "centre" | "mid" | "middle" => {
                    recognized = true;
                }
                _ => return false,
            }
        }
        recognized
    }
    for (label, anchor) in [
        ("from-anchor", c.from_anchor.as_deref()),
        ("to-anchor", c.to_anchor.as_deref()),
    ] {
        if let Some(a) = anchor
            && !is_valid_anchor(a)
        {
            diagnostics.push(Diagnostic::warning(
                "connector.invalid_anchor",
                format!(
                    "connector '{}': {label} '{a}' is not 'auto' or a nine-point anchor \
                     (top/center/bottom × left/center/right, e.g. bottom-right)",
                    c.id
                ),
                c.source_span,
                Some(c.id.clone()),
            ));
        }
    }

    // Unknown properties.
    check_unknown_props(
        "connector",
        &c.id,
        &c.unknown_props,
        c.source_span,
        diagnostics,
    );
}

/// Emit the forward-compat `node.unknown_kind` warning and register the
/// optional id. The child recursion stays in the dispatcher so the unknown
/// node's children are walked in the same position as before.
pub(in crate::validate::check) fn check_unknown(
    u: &UnknownNode,
    seen_ids: &mut BTreeSet<String>,
    diagnostics: &mut Vec<Diagnostic>,
) {
    diagnostics.push(Diagnostic::warning(
        "node.unknown_kind",
        format!(
            "unknown node kind '{}' (forward-compatibility; \
             this kind may be valid in a later schema version)",
            u.kind
        ),
        u.source_span,
        u.id.clone(),
    ));
    // Register the id (if any) so the unknown node is addressable and
    // participates in duplicate-id detection alongside known nodes.
    if let Some(id) = &u.id {
        register_id(id, seen_ids, diagnostics);
    }
}