layline-codegen 0.5.0

Rust source generation for layline.
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
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
//! Rules for messages and choices.

use alloc::format;
use alloc::string::String;
use alloc::vec::Vec;

use super::bits::validate_bits;
use super::coverage::{Places, check_value, coverage, stampable};
use super::names::{Known, Reads, Seen, read_only, resolve, stated_with};
use super::{Invalid, field_names, ident, kind_paths, type_path};
use crate::{
    Absence, ChoiceDef, Collection, Count, Discriminant, Kind, Len, MessageDef, Param, Presence,
    Scalar, Segment, Stated,
};

/// Checks a message: every reference names an earlier field, and nothing follows a segment that
/// reads to the end of the body.
pub(crate) fn validate_message(msg: &MessageDef) -> Result<(), Invalid> {
    ident("message", &msg.name)?;
    if let Some(lifetime) = &msg.lifetime {
        ident("lifetime", lifetime)?;
        if lifetime == "static" || lifetime == "_" {
            return Err(Invalid::Other(format!(
                "message `{}`: `'{lifetime}` cannot name the input the message borrows. \
                 Name a lifetime, such as `'a`",
                msg.name
            )));
        }
        if !msg.needs.is_empty() || msg.bits.is_some() {
            return Err(Invalid::Other(format!(
                "message `{}`: a message that borrows its input is byte-addressed and takes no \
                 parameters. Remove the lifetime, or the parameters and bit addressing",
                msg.name
            )));
        }
    }
    borrowed(&msg.segments, msg.lifetime.is_some())?;
    let mut seen = needs(msg)?;
    if msg.bits.is_some() {
        return validate_bits(msg);
    }
    validate_segments(&msg.segments, &mut seen)
}

/// Checks a choice: distinct arm values, and each arm a valid body.
///
/// An arm cannot refer to fields outside itself.
pub(crate) fn validate_choice(choice: &ChoiceDef) -> Result<(), Invalid> {
    ident("choice", &choice.name)?;
    if let Some(other) = &choice.other {
        ident("fallback arm", other)?;
    }
    let mut values: Vec<u64> = Vec::new();
    for arm in &choice.arms {
        ident("arm", &arm.name)?;
        if values.contains(&arm.value) {
            return Err(Invalid::DuplicateArm(arm.value));
        }
        values.push(arm.value);
        borrowed(&arm.body, false)?;
        validate_segments(&arm.body, &mut Vec::new())?;
    }
    if choice.arms.is_empty() && choice.other.is_none() {
        return Err(Invalid::Other(format!(
            "choice `{}` has no arms and no fallback. Declare an arm, or add a fallback",
            choice.name
        )));
    }
    Ok(())
}

/// Checks borrowed collections: bytes in a message with a lifetime, sized up front.
fn borrowed(segments: &[Segment], lifetime: bool) -> Result<(), Invalid> {
    for seg in segments {
        let Segment::Repeat { name, element, count, collection, .. } = seg else { continue };
        if !collection.borrows() {
            continue;
        }
        let no = |why: &str| Err(Invalid::field(name, String::from(why)));
        if !lifetime {
            return no(
                "a borrowed collection needs a message with a lifetime, which the bytes outlive. \
                 Declare the message with one, or collect a `Vec`",
            );
        }
        if *element != Kind::Scalar(Scalar::U(8)) {
            return no("a borrowed collection is bytes. Declare `&'a [u8]`, or collect a `Vec`");
        }
        match (collection, count) {
            (Collection::BorrowedArray, Count::Window(Len::Bytes(_))) => {}
            (Collection::BorrowedArray, _) => {
                return no("`&'a [u8; N]` is N bytes. Count it with `Len::Bytes(N)`");
            }
            (
                _,
                Count::Field { .. }
                | Count::Fill { .. }
                | Count::Window(Len::Field { .. } | Len::Bytes(_)),
            ) => {}
            _ => {
                return no(
                    "a borrowed collection is sized before it is read: by a count, a length, \
                     a fixed number of bytes, or the rest of the body",
                );
            }
        }
    }
    Ok(())
}

/// Checks the parameters and returns them as the first names a body can refer to.
fn needs(msg: &MessageDef) -> Result<Seen<'_>, Invalid> {
    if !msg.needs.is_empty() && msg.bits.is_some() {
        return Err(Invalid::Other(format!(
            "message `{}`: a bit-addressed message cannot take parameters. \
             Remove the bit addressing, or remove the parameters",
            msg.name
        )));
    }
    let mut seen: Seen<'_> = Vec::new();
    let fields = declared(&msg.segments);
    for Param { name, repr } in &msg.needs {
        ident("parameter", name)?;
        if seen.iter().any(|k| k.name == name.as_str()) {
            return Err(Invalid::Other(format!(
                "message `{}` declares the parameter `{name}` twice. Rename one",
                msg.name
            )));
        }
        if fields.contains(&name.as_str()) {
            return Err(Invalid::Other(format!(
                "message `{}`: `{name}` is both a parameter and a field. Rename one",
                msg.name
            )));
        }
        if !Kind::Scalar(*repr).is_integer() {
            return Err(Invalid::Other(format!(
                "message `{}`: parameter `{name}` is `{}`. A parameter must be an integer",
                msg.name,
                repr.primitive(),
            )));
        }
        seen.push(Known { name, kind: Kind::Scalar(*repr), param: true });
    }
    Ok(seen)
}

/// Every field name the body declares.
fn declared(segments: &[Segment]) -> Vec<&str> {
    let mut out = Vec::new();
    for seg in segments {
        match seg {
            Segment::Block(fields) => out.extend(fields.iter().map(|f| f.name.as_str())),
            Segment::Opt { field, .. } => out.push(field.name.as_str()),
            other => out.extend(other.name()),
        }
    }
    out
}

/// The current bit position, or `None` after a variable-length segment.
type Cursor = Option<u64>;

fn advance(pos: Cursor, bits: Option<u64>) -> Cursor {
    Some(pos? + bits?)
}

/// Checks an `#[at]` position against the cursor.
fn stated_here(pos: Cursor, name: &str, stated: Stated) -> Result<(), Invalid> {
    let Some(pos) = pos else {
        return Err(Invalid::field(
            name,
            String::from(
                "`#[at]` after a variable-length segment, whose offset is only known at run time. \
                 Put `#[at]` on an earlier field",
            ),
        ));
    };
    stated.check(name, pos).map_err(Invalid::Stated)
}

fn validate_segments<'a>(segments: &'a [Segment], seen: &mut Seen<'a>) -> Result<(), Invalid> {
    let mut open_ended: Option<&str> = None;
    let places: Places<'a> = segments.iter().flat_map(Segment::places).collect();
    let optional: Places<'a> = segments.iter().filter_map(Segment::placed_optionally).collect();
    let mut pos: Cursor = Some(0);
    for seg in segments {
        if let Some(who) = open_ended {
            return Err(Invalid::AfterOpenEnd(format!(
                "`{who}` reads to the end of the body, so nothing can follow it"
            )));
        }
        if let Some(name) = seg.name() {
            ident("field", name)?;
        }
        if let Some(stated) = seg.stated() {
            stated_here(pos, seg.name().unwrap_or("?"), stated)?;
        }
        if let Some(align) = seg.align()
            && align < 2
        {
            return Err(Invalid::field(
                seg.name().unwrap_or("?"),
                format!("an alignment of {align} pads nothing. Use 2 or more, or no alignment"),
            ));
        }
        match seg {
            Segment::Block(fields) => {
                let mut at = pos;
                for f in fields {
                    field_names(f)?;
                    if f.kind.bits().is_none() {
                        return Err(Invalid::Other(format!(
                            "field `{}`: a variable-length field cannot be in a block. \
                             Make it a `Segment::Value` of its own",
                            f.name
                        )));
                    }
                    if matches!(f.kind, Kind::Checksum { .. }) {
                        return Err(Invalid::Other(format!(
                            "field `{}`: a checksum cannot be in a block. \
                             Make it a `Segment::Checksum` of its own",
                            f.name,
                        )));
                    }
                    if matches!(f.kind, Kind::Scalar(Scalar::Bool)) {
                        return Err(Invalid::field(
                            &f.name,
                            String::from(
                                "a byte-addressed message stores a `bool` in a whole byte. \
                                 Declare it `Scalar::ByteBool`, or make the message bit-addressed",
                            ),
                        ));
                    }
                    if let Some(stated) = f.stated {
                        stated_here(at, &f.name, stated)?;
                    }
                    at = advance(at, f.kind.bits());
                    seen.push(Known::field(&f.name, &f.kind));
                }
            }
            Segment::Value { name, kind, .. } => {
                kind_paths(name, kind)?;
                match kind {
                    Kind::Var { .. } | Kind::Msg { .. } => {}
                    Kind::Text { len, .. } => match len {
                        Len::Field { by, .. } => resolve(seen, &by.field, Reads::Number)?,
                        Len::Bytes(_) | Len::Until { .. } => {}
                        Len::Fill => open_ended = Some("text(fill)"),
                    },
                    other => {
                        return Err(Invalid::field(
                            name,
                            format!(
                                "`Segment::Value` takes a `Kind::Var`, `Kind::Msg` or \
                                 `Kind::Text`, and {other:?} has a fixed width. Put it in a block"
                            ),
                        ));
                    }
                }
                stated_with(seen, name, kind)?;
                seen.push(Known::field(name, kind));
            }
            Segment::Repeat { name, element, count, at, .. } => {
                kind_paths(name, element)?;
                variable_element(name, element)?;
                match count {
                    Count::Field { by, .. } | Count::Squared { by, .. } => {
                        resolve(seen, &by.field, Reads::Number)?;
                    }
                    Count::Window(Len::Until { .. }) => {
                        if element.bits() != Some(8) {
                            return Err(Invalid::Other(format!(
                                "collection `{name}`: a run ended by a terminator byte needs \
                                 one-byte elements, not {element:?}. \
                                 Use `Count::Terminated` for wider elements"
                            )));
                        }
                    }
                    Count::Terminated { mask } => {
                        if *mask == 0 {
                            return Err(Invalid::Other(format!(
                                "collection `{name}`: a mask of zero never marks the last \
                                 element. Write the bits the last element sets"
                            )));
                        }
                        if matches!(element, Kind::Var { .. } | Kind::Text { .. }) {
                            return Err(Invalid::Other(format!(
                                "collection `{name}`: a terminated run needs fixed-width \
                                 elements, and this element is variable-length. Give it a count"
                            )));
                        }
                    }
                    Count::Strided { by, stride, .. } => {
                        resolve(seen, &by.field, Reads::Number)?;
                        resolve(seen, &stride.field, Reads::Number)?;
                        if stride.field == by.field {
                            let field = &by.field;
                            return Err(Invalid::Other(format!(
                                "collection `{name}`: `{field}` is both the count and the \
                                 stride. Use a separate field for the stride"
                            )));
                        }
                        if matches!(
                            element,
                            Kind::Var { .. } | Kind::Msg { .. } | Kind::Text { .. }
                        ) {
                            return Err(Invalid::Other(format!(
                                "collection `{name}`: a variable-length element cannot have a \
                                 stride. Give it a count (`Count::Field`)"
                            )));
                        }
                    }
                    Count::Window(len) => match len {
                        Len::Field { by, .. } => resolve(seen, &by.field, Reads::Number)?,
                        Len::Bytes(n) if at.is_some() => {
                            return Err(Invalid::Other(format!(
                                "collection `{name}`: an offset and a fixed {n} bytes give no \
                                 element count. \
                                 Use `Segment::Placed`, `Len::Field` or `Count::Field`"
                            )));
                        }
                        Len::Bytes(_) => {}
                        other => {
                            return Err(Invalid::Other(format!(
                                "collection `{name}`: a byte window of {other:?} is not \
                                 supported. Use a fixed number of bytes, or a length field"
                            )));
                        }
                    },
                    Count::Fill { .. } => open_ended = Some("repeat(fill)"),
                }
                if let Some(field) = at {
                    resolve(seen, field, Reads::Number)?;
                    read_only(seen, field, "a collection offset")?;
                }
                stated_with(seen, name, element)?;
            }
            Segment::Placed { name, kind, at, absent, .. } => {
                kind_paths(name, kind)?;
                resolve(seen, at, Reads::Number)?;
                read_only(seen, at, "a record offset")?;
                stated_with(seen, name, kind)?;
                if let Some(Absence::ZeroLength { by, .. }) = absent {
                    resolve(seen, by, Reads::Number)?;
                    read_only(seen, by, "a record length")?;
                    if by == at {
                        return Err(Invalid::field(
                            name,
                            format!(
                                "`{by}` is both the record's offset and its length. \
                                 Use a separate field for the length"
                            ),
                        ));
                    }
                }
                match kind {
                    Kind::Nested { bytes: 0, ty } => {
                        return Err(Invalid::field(
                            name,
                            format!("`{ty}` at an offset has a size of zero bytes. Give it a size"),
                        ));
                    }
                    Kind::Nested { .. } | Kind::Msg { .. } => {}
                    other => {
                        return Err(Invalid::field(
                            name,
                            format!(
                                "a record at an offset must be a nested layout or a message, \
                                 not {other:?}. Declare a `#[derive(Layout)]` struct"
                            ),
                        ));
                    }
                }
            }
            Segment::Switch { name, on, choice, window, .. } => {
                type_path(name, "the catalogue", choice)?;
                if let Discriminant::Field(field) = on {
                    resolve(seen, field, Reads::Number)?;
                }
                match window {
                    None => {}
                    Some(Len::Bytes(0)) => {
                        return Err(Invalid::field(
                            name,
                            String::from(
                                "a union of zero bytes. \
                                 Remove the window, or give the size the format defines",
                            ),
                        ));
                    }
                    Some(Len::Bytes(n)) => {
                        if matches!(on, Discriminant::BodyLength) {
                            return Err(Invalid::Other(format!(
                                "field `{name}`: a switch on the body length cannot also have a \
                                 fixed size of {n} bytes. \
                                 Switch on a field, or remove the window"
                            )));
                        }
                    }
                    Some(Len::Field { by, .. }) => {
                        if matches!(on, Discriminant::BodyLength) {
                            return Err(Invalid::field(
                                name,
                                format!(
                                    "a switch on the body length cannot also take its size \
                                     from `{}`. Switch on a field, or remove the window",
                                    by.field,
                                ),
                            ));
                        }
                        resolve(seen, &by.field, Reads::Number)?;
                    }
                    Some(other) => {
                        return Err(Invalid::field(
                            name,
                            format!(
                                "a switch window of {other:?} is not supported. \
                                 Use a fixed number of bytes, or a length field"
                            ),
                        ));
                    }
                }
            }
            Segment::Opt { when, field } => {
                field_names(field)?;
                stated_with(seen, &field.name, &field.kind)?;
                match when {
                    Presence::Remaining => {}
                    Presence::Mask { field: flag, mask }
                    | Presence::MaskClear { field: flag, mask } => {
                        resolve(seen, flag, Reads::Number)?;
                        read_only(seen, flag, "presence bits")?;
                        if *mask == 0 {
                            return Err(Invalid::field(
                                &field.name,
                                format!(
                                    "the mask tests no bits of `{flag}`, so the field is never \
                                     present. Write the bit that marks it present",
                                ),
                            ));
                        }
                    }
                    Presence::Flag { field: flag } | Presence::FlagClear { field: flag } => {
                        resolve(seen, flag, Reads::Flag)?;
                        read_only(seen, flag, "presence bits")?;
                    }
                    Presence::Bit => {
                        return Err(Invalid::field(
                            &field.name,
                            String::from(
                                "a byte-addressed message has no inline presence bits. \
                                 Test a flag field, or make this message bit-addressed",
                            ),
                        ));
                    }
                }
                match &field.kind {
                    Kind::Text { len: Len::Field { by, .. }, .. } => {
                        let src = &by.field;
                        return Err(Invalid::field(
                            &field.name,
                            format!(
                                "an optional string cannot take its length from `{src}`, which \
                                 is always on the wire. Give it a terminator or a fixed length",
                            ),
                        ));
                    }
                    Kind::Text { len: Len::Fill, .. } => open_ended = Some("optional text(fill)"),
                    Kind::Text { len, .. } if !matches!(len, Len::Bytes(_) | Len::Until { .. }) => {
                        return Err(Invalid::field(
                            &field.name,
                            format!(
                                "optional text of {len:?} length. \
                                 Give it a terminator or a fixed length",
                            ),
                        ));
                    }
                    _ => {}
                }
            }
            Segment::Checksum { name, repr, algorithm, over, .. } => {
                check_value(name, repr, algorithm)?;
                coverage(&places, &optional, name, over)?;
            }
        }
        pos = advance(pos, seg.fixed_bits());
    }
    stampable(segments)?;
    Ok(())
}

/// Refuses a text element that does not end itself.
fn variable_element(name: &str, element: &Kind) -> Result<(), Invalid> {
    let Kind::Text { len, .. } = element else {
        return Ok(());
    };
    match len {
        Len::Bytes(_) | Len::Until { .. } => Ok(()),
        other => Err(Invalid::Other(format!(
            "collection `{name}`: text elements of {other:?} length have no end. \
             Give each a terminator or a fixed length",
        ))),
    }
}