espforge-bindings 0.6.0

a scaffolding++ tool for creating esp32 no_std esphal without the hassle
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//! The coercion layer for schema-generated element attributes.
//!
//! `generated_elements.rs` (written by `build.rs`) declares one `Attrs` struct
//! per element in `espf.schema.kdl`: one field per declared attribute, typed by
//! that attribute's `kind`, carrying the byte span it came from. The generated
//! structs call the helpers below, which are the only place a spelling
//! (`"0x48"`, `"true"`, `"$gpio18"`, `"output"`) becomes a typed value.
//!
//! Two rules keep this layer honest:
//!
//! - **The schema owns presence and kind.** "Is `address` required", "is `mode`
//!   0..=3", "is `parity` one of none/even/odd" are read from
//!   `espf.schema.kdl`, never restated in lowering.
//! - **The message is not owned here.** A coercion returns an [`Issue`]; the
//!   element's lowering turns it into the text the user sees. That keeps this
//!   step byte-identical for existing errors, and leaves one place per element
//!   to change when diagnostics become codes (extreme.md ยง3.4).

use espf::context::{ParsedNode, TagAttribute};

/// A byte range in the source file.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Span {
    /// First byte of the value, as an offset into the `.espf` file.
    pub start: usize,
    /// One past the last byte of the value.
    pub end: usize,
}

impl Span {
    /// A zero-width span at the element's tag: where a defaulted attribute
    /// "comes from".
    pub fn at_node(node: &ParsedNode<'_>) -> Self {
        Self {
            start: node.offset,
            end: node.offset,
        }
    }
}

/// A coerced attribute value together with the span it was written at.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Spanned<T> {
    pub value: T,
    pub span: Span,
}

/// A `$name` reference to another element, as in `gpio="$gpio18"`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Ref<'a> {
    /// The value as written, including the `$` when the author used one.
    pub raw: &'a str,
    /// The referenced id, without the `$`.
    pub name: &'a str,
    pub span: Span,
}

impl<'a> Ref<'a> {
    fn at(raw: &'a str, span: Span) -> Self {
        Self {
            raw,
            name: raw.trim_start_matches('$'),
            span,
        }
    }
}

/// A chip-select (`kind="pin_or_ref"`): a GPIO number or a `<gpio>` id.
///
/// Both spellings are a pin in the end; which one the author wrote is kept
/// because turning a `<gpio>` id into a number needs the gpio table, which is
/// the element's business, not this layer's.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PinOrRef<'a> {
    /// `cs="9"`: the GPIO number itself.
    Pin(Spanned<u8>),
    /// `cs="gpio5"` or `cs="$gpio5"`: a `<gpio>` id.
    Ref(Ref<'a>),
}

/// Why an attribute could not be read.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Issue<'a> {
    /// Not written, or written empty, where the schema says required.
    Missing { attr: &'static str },
    /// Written, but not a value of the declared kind.
    Invalid {
        attr: &'static str,
        /// The value as written, so the message can quote it.
        value: &'a str,
    },
}

fn find<'a>(node: &'a ParsedNode<'a>, attr: &str) -> Option<&'a TagAttribute<'a>> {
    node.attributes.iter().find(|a| a.key == attr)
}

fn span_of(attr: &TagAttribute<'_>) -> Span {
    Span {
        start: attr.val_offset,
        end: attr.val_offset + attr.val.len(),
    }
}

fn missing<'a>(attr: &'static str) -> Issue<'a> {
    Issue::Missing { attr }
}

fn invalid<'a>(attr: &'static str, value: &'a str) -> Issue<'a> {
    Issue::Invalid { attr, value }
}

fn shown<'a>(attr: &'a TagAttribute<'a>) -> Spanned<&'a str> {
    Spanned {
        value: attr.val,
        span: span_of(attr),
    }
}

/// A required identifier: written and non-empty.
pub fn ident<'a>(
    node: &'a ParsedNode<'a>,
    name: &'static str,
) -> Result<Spanned<&'a str>, Issue<'a>> {
    let attr = find(node, name).ok_or(missing(name))?;
    if attr.val.is_empty() {
        return Err(missing(name));
    }
    Ok(shown(attr))
}

/// A required free text value.
pub fn text<'a>(
    node: &'a ParsedNode<'a>,
    name: &'static str,
) -> Result<Spanned<&'a str>, Issue<'a>> {
    let attr = find(node, name).ok_or(missing(name))?;
    Ok(shown(attr))
}

/// A free text value, defaulted from the schema when absent.
pub fn text_or<'a>(
    node: &'a ParsedNode<'a>,
    name: &'static str,
    default: &'a str,
) -> Result<Spanned<&'a str>, Issue<'a>> {
    match find(node, name) {
        Some(attr) => Ok(shown(attr)),
        None => Ok(Spanned {
            value: default,
            span: Span::at_node(node),
        }),
    }
}

/// An optional free text value.
pub fn opt_text<'a>(
    node: &'a ParsedNode<'a>,
    name: &'static str,
) -> Result<Option<Spanned<&'a str>>, Issue<'a>> {
    Ok(find(node, name).map(shown))
}

/// `kind="freq"`: kept as the text the author wrote.
///
/// The units and their limits ("400kHz", "40MHz", and the peripheral-specific
/// "not supported yet") belong to the peripheral that applies the frequency,
/// where each already has its own wording. Phase 2 moves the parse and that
/// wording together; until then a second parser here would be a third
/// vocabulary.
pub fn frequency<'a>(
    node: &'a ParsedNode<'a>,
    name: &'static str,
) -> Result<Spanned<&'a str>, Issue<'a>> {
    text(node, name)
}

/// A `kind="freq"` value with a schema default.
pub fn frequency_or<'a>(
    node: &'a ParsedNode<'a>,
    name: &'static str,
    default: &'a str,
) -> Result<Spanned<&'a str>, Issue<'a>> {
    text_or(node, name, default)
}

/// An optional `kind="freq"` value.
pub fn opt_frequency<'a>(
    node: &'a ParsedNode<'a>,
    name: &'static str,
) -> Result<Option<Spanned<&'a str>>, Issue<'a>> {
    opt_text(node, name)
}

/// A required `true` / `false`.
pub fn flag<'a>(node: &'a ParsedNode<'a>, name: &'static str) -> Result<Spanned<bool>, Issue<'a>> {
    let attr = find(node, name).ok_or(missing(name))?;
    flag_value(attr, name)
}

/// A `true` / `false` value, defaulted from the schema when absent.
pub fn flag_or<'a>(
    node: &'a ParsedNode<'a>,
    name: &'static str,
    default: bool,
) -> Result<Spanned<bool>, Issue<'a>> {
    match find(node, name) {
        Some(attr) => flag_value(attr, name),
        None => Ok(Spanned {
            value: default,
            span: Span::at_node(node),
        }),
    }
}

fn flag_value<'a>(
    attr: &'a TagAttribute<'a>,
    name: &'static str,
) -> Result<Spanned<bool>, Issue<'a>> {
    let value = match attr.val {
        "true" => true,
        "false" => false,
        other => return Err(invalid(name, other)),
    };
    Ok(Spanned {
        value,
        span: span_of(attr),
    })
}

/// A required `u32`.
pub fn number<'a>(node: &'a ParsedNode<'a>, name: &'static str) -> Result<Spanned<u32>, Issue<'a>> {
    let attr = find(node, name).ok_or(missing(name))?;
    number_value(attr, name)
}

/// A required `u32` with a schema `range`.
pub fn number_in<'a>(
    node: &'a ParsedNode<'a>,
    name: &'static str,
    low: u32,
    high: u32,
) -> Result<Spanned<u32>, Issue<'a>> {
    let attr = find(node, name).ok_or(missing(name))?;
    let value = attr
        .val
        .parse::<u32>()
        .map_err(|_| invalid(name, attr.val))?;
    if !(low..=high).contains(&value) {
        return Err(invalid(name, attr.val));
    }
    Ok(Spanned {
        value,
        span: span_of(attr),
    })
}

/// A `u32` value, defaulted from the schema when absent.
pub fn number_or<'a>(
    node: &'a ParsedNode<'a>,
    name: &'static str,
    default: u32,
) -> Result<Spanned<u32>, Issue<'a>> {
    match find(node, name) {
        Some(attr) => number_value(attr, name),
        None => Ok(Spanned {
            value: default,
            span: Span::at_node(node),
        }),
    }
}

/// A `u32` with a schema `range`, defaulted when absent.
pub fn number_in_or<'a>(
    node: &'a ParsedNode<'a>,
    name: &'static str,
    low: u32,
    high: u32,
    default: u32,
) -> Result<Spanned<u32>, Issue<'a>> {
    let Some(attr) = find(node, name) else {
        return Ok(Spanned {
            value: default,
            span: Span::at_node(node),
        });
    };
    let value = attr
        .val
        .parse::<u32>()
        .map_err(|_| invalid(name, attr.val))?;
    if !(low..=high).contains(&value) {
        return Err(invalid(name, attr.val));
    }
    Ok(Spanned {
        value,
        span: span_of(attr),
    })
}

fn number_value<'a>(
    attr: &'a TagAttribute<'a>,
    name: &'static str,
) -> Result<Spanned<u32>, Issue<'a>> {
    let value = attr
        .val
        .parse::<u32>()
        .map_err(|_| invalid(name, attr.val))?;
    Ok(Spanned {
        value,
        span: span_of(attr),
    })
}

/// A required GPIO number.
pub fn pin<'a>(node: &'a ParsedNode<'a>, name: &'static str) -> Result<Spanned<u8>, Issue<'a>> {
    let attr = find(node, name).ok_or(missing(name))?;
    Ok(Spanned {
        value: attr
            .val
            .parse::<u8>()
            .map_err(|_| invalid(name, attr.val))?,
        span: span_of(attr),
    })
}

/// An optional GPIO number.
pub fn opt_pin<'a>(
    node: &'a ParsedNode<'a>,
    name: &'static str,
) -> Result<Option<Spanned<u8>>, Issue<'a>> {
    match find(node, name) {
        Some(attr) => Ok(Some(Spanned {
            value: attr
                .val
                .parse::<u8>()
                .map_err(|_| invalid(name, attr.val))?,
            span: span_of(attr),
        })),
        None => Ok(None),
    }
}

/// A required `0x..` or decimal byte.
pub fn hex<'a>(node: &'a ParsedNode<'a>, name: &'static str) -> Result<Spanned<u8>, Issue<'a>> {
    let attr = find(node, name).ok_or(missing(name))?;
    hex_value(attr, name)
}

/// An optional `0x..` or decimal byte.
pub fn opt_hex<'a>(
    node: &'a ParsedNode<'a>,
    name: &'static str,
) -> Result<Option<Spanned<u8>>, Issue<'a>> {
    match find(node, name) {
        Some(attr) => Ok(Some(hex_value(attr, name)?)),
        None => Ok(None),
    }
}

fn hex_value<'a>(attr: &'a TagAttribute<'a>, name: &'static str) -> Result<Spanned<u8>, Issue<'a>> {
    let parsed = match attr
        .val
        .strip_prefix("0x")
        .or_else(|| attr.val.strip_prefix("0X"))
    {
        Some(hex) => u8::from_str_radix(hex, 16),
        None => attr.val.parse::<u8>(),
    };
    Ok(Spanned {
        value: parsed.map_err(|_| invalid(name, attr.val))?,
        span: span_of(attr),
    })
}

/// A required `kind="pin_or_ref"` value: a GPIO number, or anything else as
/// a `<gpio>` reference.
pub fn pin_or_ref<'a>(
    node: &'a ParsedNode<'a>,
    name: &'static str,
) -> Result<PinOrRef<'a>, Issue<'a>> {
    let attr = find(node, name).ok_or(missing(name))?;
    let span = span_of(attr);
    Ok(match attr.val.parse::<u8>() {
        Ok(pin) => PinOrRef::Pin(Spanned { value: pin, span }),
        Err(_) => PinOrRef::Ref(Ref::at(attr.val, span)),
    })
}

/// A required `$`-reference to another element.
pub fn reference<'a>(node: &'a ParsedNode<'a>, name: &'static str) -> Result<Ref<'a>, Issue<'a>> {
    let attr = find(node, name).ok_or(missing(name))?;
    Ok(Ref::at(attr.val, span_of(attr)))
}

/// An optional `$`-reference to another element.
pub fn opt_reference<'a>(
    node: &'a ParsedNode<'a>,
    name: &'static str,
) -> Result<Option<Ref<'a>>, Issue<'a>> {
    Ok(find(node, name).map(|attr| Ref::at(attr.val, span_of(attr))))
}

/// A required value from a generated `choices` enum.
///
/// `parse` is the enum's own [`parse`](crate::components::generated_elements)
/// function, which is generated from the same `choices` list this checks
/// against โ€” so the enum and the validation cannot disagree.
pub fn parsed<'a, T: Copy>(
    node: &'a ParsedNode<'a>,
    name: &'static str,
    parse: fn(&str) -> Option<T>,
) -> Result<Spanned<T>, Issue<'a>> {
    let attr = find(node, name).ok_or(missing(name))?;
    parsed_value(attr, name, parse)
}

/// A `choices` value, defaulted from the schema when absent.
pub fn parsed_or<'a, T: Copy>(
    node: &'a ParsedNode<'a>,
    name: &'static str,
    parse: fn(&str) -> Option<T>,
    default: T,
) -> Result<Spanned<T>, Issue<'a>> {
    match find(node, name) {
        Some(attr) => parsed_value(attr, name, parse),
        None => Ok(Spanned {
            value: default,
            span: Span::at_node(node),
        }),
    }
}

/// An optional `choices` value.
pub fn opt_parsed<'a, T: Copy>(
    node: &'a ParsedNode<'a>,
    name: &'static str,
    parse: fn(&str) -> Option<T>,
) -> Result<Option<Spanned<T>>, Issue<'a>> {
    match find(node, name) {
        Some(attr) => Ok(Some(parsed_value(attr, name, parse)?)),
        None => Ok(None),
    }
}

fn parsed_value<'a, T: Copy>(
    attr: &'a TagAttribute<'a>,
    name: &'static str,
    parse: fn(&str) -> Option<T>,
) -> Result<Spanned<T>, Issue<'a>> {
    Ok(Spanned {
        value: parse(attr.val).ok_or_else(|| invalid(name, attr.val))?,
        span: span_of(attr),
    })
}

/// A `kind="list"` attribute: every occurrence, in source order.
///
/// An absent list is empty, so this cannot fail. The `+` spelling
/// (`servers+="pool.ntp.org"`) and the plain one are both collected.
pub fn list<'a>(node: &'a ParsedNode<'a>, name: &str) -> Vec<Spanned<&'a str>> {
    node.attributes
        .iter()
        .filter(|a| a.key == name)
        .map(shown)
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    fn node<'a>(section: &str, src: &'a str) -> ParsedNode<'a> {
        espf::parser::parse_espf(src).sections[section][0].clone()
    }

    #[test]
    fn a_reference_keeps_both_spellings_and_its_span() {
        let n = node(
            "components",
            "# components\n  <led id=\"l\" gpio=\"$gpio18\" />\n",
        );
        let r = reference(&n, "gpio").unwrap();
        assert_eq!(r.raw, "$gpio18");
        assert_eq!(r.name, "gpio18");
        let written_at = &"# components\n  <led id=\"l\" gpio="[..];
        assert_eq!(r.span.start, written_at.len() + 1);
        assert_eq!(r.span.end - r.span.start, "$gpio18".len());
    }

    #[test]
    fn missing_identifiers_are_missing_even_when_empty() {
        let n = node(
            "components",
            "# components\n  <led id=\"\" gpio=\"$gpio18\" />\n",
        );
        assert_eq!(ident(&n, "id"), Err(Issue::Missing { attr: "id" }));
    }

    #[test]
    fn a_bad_choice_reports_the_value_as_written() {
        let n = node(
            "components",
            "# components\n  <led id=\"l\" gpio=\"$g\" active_low=\"yes\" />\n",
        );
        assert_eq!(
            flag(&n, "active_low"),
            Err(Issue::Invalid {
                attr: "active_low",
                value: "yes"
            })
        );
    }

    #[test]
    fn hex_accepts_both_spellings_and_a_range_is_enforced() {
        let hi = node(
            "components",
            "# components\n  <i2c_component id=\"c\" i2c=\"$i2c0\" address=\"0x48\" />\n",
        );
        assert_eq!(hex(&hi, "address").unwrap().value, 0x48);

        let range = node(
            "peripherals",
            "# peripherals\n  <spi id=\"s\" sclk=\"1\" mosi=\"2\" mode=\"3\" />\n",
        );
        assert_eq!(number_in_or(&range, "mode", 0, 3, 0).unwrap().value, 3);
        assert_eq!(
            number_in_or(&range, "mode", 0, 2, 0),
            Err(Issue::Invalid {
                attr: "mode",
                value: "3"
            })
        );
    }

    #[test]
    fn defaults_are_attributed_to_the_element() {
        let n = node("espforge", "# espforge\n  <console />\n");
        let default = text_or(&n, "type", "uart").unwrap();
        assert_eq!(default.value, "uart");
        assert_eq!(default.span, Span::at_node(&n));
    }

    #[test]
    fn a_list_collects_every_occurrence() {
        let n = node(
            "services",
            "# services\n  <ntp id=\"ntp0\" servers+=\"pool.ntp.org\" servers+=\"time.google.com\" />\n",
        );
        let servers: Vec<&str> = list(&n, "servers").into_iter().map(|s| s.value).collect();
        assert_eq!(servers, ["pool.ntp.org", "time.google.com"]);
    }
}