zenith-core 0.0.0-beta.2

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
//! Transforms for the specialized renderable nodes: field, toc, footnote,
//! shape, and connector.

use kdl::KdlNode;

use crate::ast::node::{ConnectorNode, FieldNode, FootnoteNode, ShapeNode, TextSpan, TocNode};
use crate::error::ParseError;

use super::helpers::{
    collect_unknown_props, node_span, optional_bool_prop, optional_dimension_prop,
    optional_f64_prop, optional_property_value, optional_property_value_aliased,
    optional_string_prop, optional_string_prop_aliased, required_string_prop,
};
use super::leaf::transform_span;

pub(crate) const SHAPE_KNOWN_PROPS: &[&str] = &[
    "id",
    "name",
    "role",
    "x",
    "y",
    "w",
    "h",
    "kind",
    "fill",
    "stroke",
    "stroke-width",
    "stroke_width",
    "radius",
    "stroke-alignment",
    "stroke_alignment",
    "padding",
    "h-align",
    "h_align",
    "v-align",
    "v_align",
    "text-style",
    "text_style",
    "style",
    "opacity",
    "visible",
    "locked",
    "rotate",
    "anchor",
    "anchor-zone",
    "anchor_zone",
    "anchor-sibling",
    "anchor_sibling",
    "anchor-edge",
    "anchor_edge",
    "anchor-gap",
    "anchor_gap",
    "anchor-parent",
    "anchor_parent",
];

pub(super) fn transform_shape(node: &KdlNode) -> Result<ShapeNode, ParseError> {
    let id = required_string_prop(node, "id")?.to_owned();

    let mut spans: Vec<TextSpan> = Vec::new();
    if let Some(children) = node.children() {
        for child in children.nodes() {
            if child.name().value() == "span" {
                spans.push(transform_span(child)?);
            }
        }
    }

    let unknown_props = collect_unknown_props(node, SHAPE_KNOWN_PROPS);

    Ok(ShapeNode {
        id,
        name: optional_string_prop(node, "name").map(str::to_owned),
        role: optional_string_prop(node, "role").map(str::to_owned),
        x: optional_property_value(node, "x"),
        y: optional_property_value(node, "y"),
        w: optional_property_value(node, "w"),
        h: optional_property_value(node, "h"),
        kind: optional_string_prop(node, "kind").map(str::to_owned),
        fill: optional_property_value(node, "fill"),
        stroke: optional_property_value(node, "stroke"),
        stroke_width: optional_property_value_aliased(node, "stroke-width", "stroke_width"),
        radius: optional_property_value(node, "radius"),
        stroke_alignment: optional_string_prop_aliased(
            node,
            "stroke-alignment",
            "stroke_alignment",
        )
        .map(str::to_owned),
        padding: optional_property_value(node, "padding"),
        h_align: optional_string_prop_aliased(node, "h-align", "h_align").map(str::to_owned),
        v_align: optional_string_prop_aliased(node, "v-align", "v_align").map(str::to_owned),
        text_style: optional_string_prop_aliased(node, "text-style", "text_style")
            .map(str::to_owned),
        spans,
        style: optional_string_prop(node, "style").map(str::to_owned),
        opacity: optional_f64_prop(node, "opacity"),
        visible: optional_bool_prop(node, "visible"),
        locked: optional_bool_prop(node, "locked"),
        rotate: optional_dimension_prop(node, "rotate"),
        anchor: optional_string_prop(node, "anchor").map(str::to_owned),
        anchor_zone: optional_string_prop(node, "anchor-zone")
            .or_else(|| optional_string_prop(node, "anchor_zone"))
            .map(str::to_owned),
        anchor_sibling: optional_string_prop(node, "anchor-sibling")
            .or_else(|| optional_string_prop(node, "anchor_sibling"))
            .map(str::to_owned),
        anchor_edge: optional_string_prop(node, "anchor-edge")
            .or_else(|| optional_string_prop(node, "anchor_edge"))
            .map(str::to_owned),
        anchor_gap: optional_dimension_prop(node, "anchor-gap")
            .or_else(|| optional_dimension_prop(node, "anchor_gap")),
        anchor_parent: optional_bool_prop(node, "anchor-parent")
            .or_else(|| optional_bool_prop(node, "anchor_parent")),
        source_span: node_span(node),
        unknown_props,
    })
}

pub(crate) const CONNECTOR_KNOWN_PROPS: &[&str] = &[
    "id",
    "name",
    "role",
    "from",
    "to",
    "from-anchor",
    "from_anchor",
    "to-anchor",
    "to_anchor",
    "route",
    "marker-start",
    "marker_start",
    "marker-end",
    "marker_end",
    "stroke",
    "stroke-width",
    "stroke_width",
    "opacity",
    "visible",
    "locked",
    "rotate",
    "style",
    "text-style",
    "text_style",
];

pub(super) fn transform_connector(node: &KdlNode) -> Result<ConnectorNode, ParseError> {
    let id = required_string_prop(node, "id")?.to_owned();

    let mut spans: Vec<TextSpan> = Vec::new();
    if let Some(children) = node.children() {
        for child in children.nodes() {
            if child.name().value() == "span" {
                spans.push(transform_span(child)?);
            }
        }
    }

    let unknown_props = collect_unknown_props(node, CONNECTOR_KNOWN_PROPS);

    Ok(ConnectorNode {
        id,
        name: optional_string_prop(node, "name").map(str::to_owned),
        role: optional_string_prop(node, "role").map(str::to_owned),
        from: optional_string_prop(node, "from").map(str::to_owned),
        to: optional_string_prop(node, "to").map(str::to_owned),
        from_anchor: optional_string_prop_aliased(node, "from-anchor", "from_anchor")
            .map(str::to_owned),
        to_anchor: optional_string_prop_aliased(node, "to-anchor", "to_anchor").map(str::to_owned),
        route: optional_string_prop(node, "route").map(str::to_owned),
        marker_start: optional_string_prop_aliased(node, "marker-start", "marker_start")
            .map(str::to_owned),
        marker_end: optional_string_prop_aliased(node, "marker-end", "marker_end")
            .map(str::to_owned),
        stroke: optional_property_value(node, "stroke"),
        stroke_width: optional_property_value_aliased(node, "stroke-width", "stroke_width"),
        opacity: optional_f64_prop(node, "opacity"),
        visible: optional_bool_prop(node, "visible"),
        locked: optional_bool_prop(node, "locked"),
        rotate: optional_dimension_prop(node, "rotate"),
        style: optional_string_prop(node, "style").map(str::to_owned),
        text_style: optional_string_prop_aliased(node, "text-style", "text_style")
            .map(str::to_owned),
        spans,
        source_span: node_span(node),
        unknown_props,
    })
}

pub(crate) const FIELD_KNOWN_PROPS: &[&str] = &[
    "id",
    "name",
    "role",
    "type",
    "recto",
    "verso",
    "target",
    "folio-style",
    "folio_style",
    "suppress-first",
    "suppress_first",
    "x",
    "y",
    "w",
    "h",
    "style",
    "fill",
    "font-family",
    "font_family",
    "font-size",
    "font_size",
    "opacity",
    "visible",
    "locked",
    "anchor",
    "anchor-zone",
    "anchor_zone",
    "anchor-sibling",
    "anchor_sibling",
    "anchor-edge",
    "anchor_edge",
    "anchor-gap",
    "anchor_gap",
    "anchor-parent",
    "anchor_parent",
];

/// Transform a `field` node into a [`FieldNode`].
///
/// Reads required `id` and `type`; optional `recto`/`verso`/`target` strings;
/// optional `x`/`y`/`w`/`h` geometry; and visual props (`style`/`fill`/
/// `font-family`/`font-size`). The `type` value is preserved verbatim (the
/// validator warns on an unknown type), as is `target` (the compiler resolves
/// the page-ref and the validator warns on an unresolved one).
pub(super) fn transform_field(node: &KdlNode) -> Result<FieldNode, ParseError> {
    let id = required_string_prop(node, "id")?.to_owned();
    let field_type = required_string_prop(node, "type")?.to_owned();

    let font_family = optional_property_value_aliased(node, "font-family", "font_family");
    let font_size = optional_property_value_aliased(node, "font-size", "font_size");
    let folio_style =
        optional_string_prop_aliased(node, "folio-style", "folio_style").map(str::to_owned);
    let suppress_first = optional_bool_prop(node, "suppress-first")
        .or_else(|| optional_bool_prop(node, "suppress_first"));

    let unknown_props = collect_unknown_props(node, FIELD_KNOWN_PROPS);

    Ok(FieldNode {
        id,
        name: optional_string_prop(node, "name").map(str::to_owned),
        role: optional_string_prop(node, "role").map(str::to_owned),
        field_type,
        recto: optional_string_prop(node, "recto").map(str::to_owned),
        verso: optional_string_prop(node, "verso").map(str::to_owned),
        target: optional_string_prop(node, "target").map(str::to_owned),
        folio_style,
        suppress_first,
        x: optional_property_value(node, "x"),
        y: optional_property_value(node, "y"),
        w: optional_property_value(node, "w"),
        h: optional_property_value(node, "h"),
        style: optional_string_prop(node, "style").map(str::to_owned),
        fill: optional_property_value(node, "fill"),
        font_family,
        font_size,
        opacity: optional_f64_prop(node, "opacity"),
        visible: optional_bool_prop(node, "visible"),
        locked: optional_bool_prop(node, "locked"),
        anchor: optional_string_prop(node, "anchor").map(str::to_owned),
        anchor_zone: optional_string_prop(node, "anchor-zone")
            .or_else(|| optional_string_prop(node, "anchor_zone"))
            .map(str::to_owned),
        anchor_sibling: optional_string_prop(node, "anchor-sibling")
            .or_else(|| optional_string_prop(node, "anchor_sibling"))
            .map(str::to_owned),
        anchor_edge: optional_string_prop(node, "anchor-edge")
            .or_else(|| optional_string_prop(node, "anchor_edge"))
            .map(str::to_owned),
        anchor_gap: optional_dimension_prop(node, "anchor-gap")
            .or_else(|| optional_dimension_prop(node, "anchor_gap")),
        anchor_parent: optional_bool_prop(node, "anchor-parent")
            .or_else(|| optional_bool_prop(node, "anchor_parent")),
        source_span: node_span(node),
        unknown_props,
    })
}

pub(crate) const TOC_KNOWN_PROPS: &[&str] = &[
    "id",
    "name",
    "role",
    "match-role",
    "match_role",
    "match-style",
    "match_style",
    "leader",
    "folio-style",
    "folio_style",
    "x",
    "y",
    "w",
    "h",
    "style",
    "fill",
    "font-family",
    "font_family",
    "font-size",
    "font_size",
    "opacity",
    "visible",
    "locked",
    "anchor",
    "anchor-zone",
    "anchor_zone",
    "anchor-sibling",
    "anchor_sibling",
    "anchor-edge",
    "anchor_edge",
    "anchor-gap",
    "anchor_gap",
    "anchor-parent",
    "anchor_parent",
];

/// Transform a `toc` node into a [`TocNode`].
///
/// Reads required `id`; optional selector props (`match-role`, `match-style`);
/// optional `leader` and `folio-style` strings; optional `x`/`y`/`w`/`h`
/// geometry; and visual props (`style`/`fill`/`font-family`/`font-size`).
/// The `match-role`/`match-style` values are preserved verbatim for the
/// compiler; the validator warns when both are absent. The folio style is
/// preserved verbatim (validated at compile time).
pub(super) fn transform_toc(node: &KdlNode) -> Result<TocNode, ParseError> {
    let id = required_string_prop(node, "id")?.to_owned();

    let match_role =
        optional_string_prop_aliased(node, "match-role", "match_role").map(str::to_owned);
    let match_style =
        optional_string_prop_aliased(node, "match-style", "match_style").map(str::to_owned);
    let leader = optional_string_prop(node, "leader").map(str::to_owned);
    let folio_style =
        optional_string_prop_aliased(node, "folio-style", "folio_style").map(str::to_owned);
    let font_family = optional_property_value_aliased(node, "font-family", "font_family");
    let font_size = optional_property_value_aliased(node, "font-size", "font_size");

    let unknown_props = collect_unknown_props(node, TOC_KNOWN_PROPS);

    Ok(TocNode {
        id,
        name: optional_string_prop(node, "name").map(str::to_owned),
        role: optional_string_prop(node, "role").map(str::to_owned),
        match_role,
        match_style,
        leader,
        folio_style,
        x: optional_property_value(node, "x"),
        y: optional_property_value(node, "y"),
        w: optional_property_value(node, "w"),
        h: optional_property_value(node, "h"),
        style: optional_string_prop(node, "style").map(str::to_owned),
        fill: optional_property_value(node, "fill"),
        font_family,
        font_size,
        opacity: optional_f64_prop(node, "opacity"),
        visible: optional_bool_prop(node, "visible"),
        locked: optional_bool_prop(node, "locked"),
        anchor: optional_string_prop(node, "anchor").map(str::to_owned),
        anchor_zone: optional_string_prop(node, "anchor-zone")
            .or_else(|| optional_string_prop(node, "anchor_zone"))
            .map(str::to_owned),
        anchor_sibling: optional_string_prop(node, "anchor-sibling")
            .or_else(|| optional_string_prop(node, "anchor_sibling"))
            .map(str::to_owned),
        anchor_edge: optional_string_prop(node, "anchor-edge")
            .or_else(|| optional_string_prop(node, "anchor_edge"))
            .map(str::to_owned),
        anchor_gap: optional_dimension_prop(node, "anchor-gap")
            .or_else(|| optional_dimension_prop(node, "anchor_gap")),
        anchor_parent: optional_bool_prop(node, "anchor-parent")
            .or_else(|| optional_bool_prop(node, "anchor_parent")),
        source_span: node_span(node),
        unknown_props,
    })
}

pub(crate) const FOOTNOTE_KNOWN_PROPS: &[&str] = &[
    "id",
    "name",
    "role",
    "marker",
    "style",
    "fill",
    "font-family",
    "font_family",
    "font-size",
    "font_size",
];

/// Transform a `footnote` node into a [`FootnoteNode`].
///
/// Reads the required `id`; the optional `marker` override; visual props
/// (`style`/`fill`/`font-family`/`font-size`); and the content `span` children
/// (the same span model a `text` node uses). A footnote has NO geometry.
pub(super) fn transform_footnote(node: &KdlNode) -> Result<FootnoteNode, ParseError> {
    let id = required_string_prop(node, "id")?.to_owned();

    let font_family = optional_property_value_aliased(node, "font-family", "font_family");
    let font_size = optional_property_value_aliased(node, "font-size", "font_size");

    let mut spans: Vec<TextSpan> = Vec::new();
    if let Some(children) = node.children() {
        for child in children.nodes() {
            if child.name().value() == "span" {
                spans.push(transform_span(child)?);
            }
        }
    }

    let unknown_props = collect_unknown_props(node, FOOTNOTE_KNOWN_PROPS);

    Ok(FootnoteNode {
        id,
        name: optional_string_prop(node, "name").map(str::to_owned),
        role: optional_string_prop(node, "role").map(str::to_owned),
        marker: optional_string_prop(node, "marker").map(str::to_owned),
        spans,
        style: optional_string_prop(node, "style").map(str::to_owned),
        fill: optional_property_value(node, "fill"),
        font_family,
        font_size,
        source_span: node_span(node),
        unknown_props,
    })
}