layline-derive 0.2.0

Derive macros 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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//! `Layout` refusals.

use super::*;

#[test]
fn at_mismatch() {
    refused(
        r#"use layline::Layout;

#[derive(Layout)]
#[layout(bits = 16)]
pub struct At {
    #[bits(2)]
    pub a: u8,
    #[bits(14)]
    #[at(bit = 3)]
    pub b: u16,
}"#,
        "#[at(bit = 3)] but the solver placed it at bit 2",
    );
}

#[test]
fn at_mismatch_under_msb_names_both_numberings() {
    refused(
        r#"use layline::Layout;

#[derive(Layout)]
#[layout(bits = 32, endian = be, order = msb)]
pub struct Can {
    #[bits(29)]
    #[at(bit = 0)]
    pub id: u32,
    #[bits(1)]
    #[at(bit = 2)]
    pub ide: bool,
    #[bits(1)]
    pub rtr: bool,
    #[bits(1)]
    pub err: bool,
}"#,
        "field `ide`: #[at(bit = 2)] (declared numbering, msb) is physical bit 29, but the \
         solver placed it at physical bit 2 — declared bit 29",
    );
}

#[test]
fn bits_in_byte_mode() {
    refused(
        r#"#[derive(Debug, Clone, PartialEq)]
struct Level(u8);

impl layline::FieldCodec for Level {
    const BITS: u32 = 8;
    fn from_raw(raw: u64) -> Self {
        Self(raw as u8)
    }
    fn to_raw(&self) -> u64 {
        self.0 as u64
    }
}

#[derive(layline::Layout)]
#[layout(bytes = 1)]
struct Byte {
    #[bits(8)]
    level: Level,
}

#[derive(layline::Message)]
struct Msg {
    #[bits(8)]
    level: Level,
}"#,
        "field `level`: byte mode has no bit fields. \
         Write `#[codec(N)]` for a field narrower than its type",
    );
}

#[test]
fn bool_width() {
    refused(
        r#"use layline::Layout;

#[derive(Layout)]
#[layout(bits = 8)]
pub struct B {
    #[bits(2)]
    pub flag: bool,
    #[bits(6)]
    pub rest: u8,
}"#,
        "field `flag`: a `bool` is one bit, not #[bits(2)]",
    );
}

#[test]
fn bytes_tiling_short() {
    refused(
        r#"use layline::Layout;

#[derive(Layout)]
#[layout(bytes = 16)]
pub struct Short {
    pub a: u32,
    pub b: f32,
}"#,
        "`Short`: declares 16 bytes but its fields end at byte 8. \
         Add 8 bytes of reserved `[u8; N]` fields after `b`",
    );
}

#[test]
fn checksum_cycle() {
    refused(
        r#"#[derive(layline::Message)]
struct Cycle {
    body: u16,
    #[checksum(wire::Crc16Ccitt, over = ..=b)]
    a: u16,
    #[checksum(wire::Crc16Ccitt, over = ..=a)]
    b: u16,
}"#,
        "checksums `a` and `b` cover each other. \
         Narrow one range so it does not reach the other",
    );
}

#[test]
fn layout_checksum_covers_a_later_checksum() {
    refused(
        r#"use layline::Layout;

#[derive(Layout)]
#[layout(bytes = 6, endian = le)]
pub struct Backwards {
    pub body: u16,
    #[checksum(wire::Crc16Ccitt, over = ..=second)]
    pub first: u16,
    #[checksum(wire::Crc16Ccitt, over = body..)]
    pub second: u16,
}"#,
        "field `first`: the checksum range covers `second`, a later checksum",
    );
}

#[test]
fn layout_magic_in_bit_mode() {
    refused(
        r#"use layline::Layout;

#[derive(Layout)]
#[layout(bits = 16, order = msb)]
pub struct Word {
    #[bits(8)]
    #[magic(0xAA)]
    pub sig: u8,
    #[bits(8)]
    pub rest: u8,
}"#,
        "field `sig`: #[magic] needs whole bytes, and a bit-addressed layout has none. \
         Use `#[layout(bytes = N)]`",
    );
}

#[test]
fn layout_magic_not_a_literal() {
    refused(
        r#"use layline::Layout;

const SIGNATURE: u16 = 0xAA55;

#[derive(Layout)]
#[layout(bytes = 2, endian = le)]
pub struct BootTail {
    #[magic(SIGNATURE)]
    pub signature: u16,
}"#,
        "field `signature`: #[magic(..)] takes one literal. \
         Write a byte string such as `b\"SEAL\"`, or an integer such as `0xAA55`",
    );
}

#[test]
fn layout_magic_on_a_codec_field() {
    refused(
        r#"use layline::{FieldCodec, Layout};

#[derive(FieldCodec)]
#[bits(8)]
pub struct Mode(u8);

#[derive(Layout)]
#[layout(bytes = 1)]
pub struct Header {
    #[codec(8)]
    #[magic(0x01)]
    pub mode: Mode,
}"#,
        "field `mode`: #[magic] needs an integer scalar (`u8`..`u64`, `i8`..`i64`) or a `[u8; N]`",
    );
}

#[test]
fn layout_magic_too_wide() {
    refused(
        r#"use layline::Layout;

#[derive(Layout)]
#[layout(bytes = 1)]
pub struct Tag {
    #[magic(0x1FF)]
    pub tag: u8,
}"#,
        "#[magic(511)] does not fit 1 byte",
    );
}

#[test]
fn layout_magic_with_view() {
    refused(
        r#"use layline::Layout;

#[derive(Layout)]
#[layout(bytes = 4, endian = le, view)]
pub struct Header {
    #[magic(b"AB")]
    pub sync: [u8; 2],
    pub rest: u16,
}"#,
        "`Header`: `view` cannot check the #[magic] on `sync`. Remove `view` or the #[magic]",
    );
}

#[test]
fn narrow_bits_disagree() {
    refused(
        r#"use layline::{Layout, U};

#[derive(Layout)]
#[layout(bits = 8)]
pub struct Header {
    #[bits(5)]
    pub version: U<4>,
    #[bits(3)]
    pub rest: u8,
}"#,
        "#[bits(5)] disagrees with `U<4>`, which is 4 bits",
    );
}

#[test]
fn stated_run_disagrees() {
    refused(
        r#"#[derive(layline::Message)]
struct Wrong {
    n: u8,
    #[at(byte = 4)]
    #[count(n)]
    items: Vec<u16>,
}"#,
        "field `items`: #[at(byte = 4)] but the solver placed it at byte 1",
    );
}

#[test]
fn tiling_overflow() {
    refused(
        r#"use layline::Layout;

#[derive(Layout)]
#[layout(bits = 16)]
pub struct Overflow {
    #[bits(10)]
    pub a: u16,
    #[bits(10)]
    pub b: u16,
}"#,
        "field `b`: bits 10..20 overflow the 16-bit layout",
    );
}

#[test]
fn tiling_short() {
    refused(
        r#"use layline::Layout;

#[derive(Layout)]
#[layout(bits = 16)]
pub struct Short {
    #[bits(2)]
    pub a: u8,
    #[bits(11)]
    pub b: u16,
}"#,
        "`Short`: declares 16 bits but its fields end at bit 13. \
         Add 3 bits of named spare fields after `b`",
    );
}

#[test]
fn word_bits_not_bytes() {
    refused(
        r#"use layline::Layout;

#[derive(Layout)]
#[layout(bits = 70)]
pub struct Ragged {
    #[bits(35)]
    pub a: u64,
    #[bits(35)]
    pub b: u64,
}"#,
        "`bits = 70` is not a whole number of bytes",
    );
}

#[test]
fn word_field_wider_than_accessor() {
    refused(
        r#"use layline::Layout;

#[derive(Layout)]
#[layout(bits = 512)]
pub struct Opaque {
    #[bits(200)]
    pub blob: u128,
    #[bits(312)]
    pub rest: u128,
}"#,
        "#[bits(200)] is wider than 128 bits",
    );
}

#[test]
fn layout_field_attribute_on_the_container() {
    refused(
        r#"use layline::Layout;

#[derive(Layout)]
#[layout(bytes = 2)]
#[range(0..=9)]
pub struct L {
    pub a: u16,
}"#,
        "`L`: `#[range]` belongs on a field",
    );
}

#[test]
fn layout_container_attribute_on_a_field() {
    refused(
        r#"use layline::Layout;

#[derive(Layout)]
#[layout(bytes = 2)]
pub struct L {
    #[layout(bytes = 2)]
    pub a: u16,
}"#,
        "field `a`: `#[layout]` belongs above the struct",
    );
}

#[test]
fn words_mode_codec_refused() {
    refused(
        r#"use layline::Layout;

#[derive(Layout)]
#[layout(words = 1)]
pub struct W {
    #[codec(16)]
    #[bits(16)]
    pub a: u16,
}"#,
        "field `a`: `#[codec(N)]` does not apply in a `words = N` layout",
    );
}

#[test]
fn layout_refuses_the_walk_vocabulary_by_name() {
    for word in [
        "count(n)",
        "len(n)",
        "seek(n)",
        "fill",
        "text",
        "var",
        "message",
        "when(n)",
        "switch(n)",
        "until(0)",
        "stride(n)",
    ] {
        refused(
            &format!(
                r#"use layline::Layout;

#[derive(Layout)]
#[layout(bytes = 2)]
pub struct W {{
    pub n: u8,
    #[{word}]
    pub items: u8,
}}"#
            ),
            "is not supported in a `bytes = N` layout",
        );
    }
}

#[test]
fn layout_refuses_the_walk_vocabulary_on_the_container() {
    refused(
        r#"use layline::Layout;

#[derive(Layout)]
#[layout(bytes = 1)]
#[message]
pub struct W {
    pub n: u8,
}"#,
        "`W`: `#[message]` is not supported on a layout",
    );
}

#[test]
fn prefix_value_needs_a_prefix() {
    refused(
        r#"use layline::Layout;

#[derive(Layout)]
#[layout(bits = 16, prefix_value = 0b10)]
pub struct W {
    #[bits(16)]
    pub a: u16,
}"#,
        "`W`: `prefix_value` needs a prefix. Add `prefix = K`",
    );
}

#[test]
fn prefix_value_must_fit_the_prefix() {
    refused(
        r#"use layline::Layout;

#[derive(Layout)]
#[layout(bits = 16, prefix = 2, prefix_value = 0b100)]
pub struct W {
    #[bits(14)]
    pub a: u16,
}"#,
        "`W`: `prefix_value = 0b100` does not fit the 2-bit prefix",
    );
}

#[test]
fn prefix_is_word_mode_only() {
    refused(
        r#"use layline::Layout;

#[derive(Layout)]
#[layout(bytes = 2, prefix = 3)]
pub struct W {
    pub a: u16,
}"#,
        "`W`: `prefix` only applies to a `bits = N` layout",
    );
}

#[test]
fn a_view_cannot_check_a_range() {
    for (layout, field) in [
        ("bytes = 1", "a: u8"),
        ("bits = 8", "#[bits(8)] a: u8"),
        ("words = 1", "#[bits(8)] a: u8"),
    ] {
        refused(
            &format!(
                "#[derive(Layout)] #[layout({layout}, view)] struct V {{ #[range(0..=5)] {field} }}"
            ),
            "`V`: `view` cannot check the #[range] on `a`",
        );
    }
}

#[test]
fn an_overlay_names_a_method_of_its_own() {
    let word = |a: &str, b: &str| {
        format!(
            "#[derive(Layout)] #[layout(bits = 8)] struct W {{ \
             #[bits(4)] {a} a: u8, #[bits(4)] {b} b: u8 }}"
        )
    };
    let cases = [
        (word("#[overlay(decode: Nib)]", ""), "overlay `decode`: writes `fn decode`"),
        (word("#[overlay(encode: Nib)]", ""), "overlay `encode`: writes `fn encode`"),
        (word("#[overlay(x: Nib)] #[overlay(x: Nib)]", ""), "overlay `x`: writes `fn set_x`"),
        (word("#[overlay(x: Nib)]", "#[overlay(x: Nib)]"), "overlay `x`: writes `fn set_x`"),
        (
            word("#[overlay(x: Nib)]", "#[overlay(set_x: Nib)]"),
            "overlay `set_x`: writes `fn set_x`",
        ),
        (
            String::from(
                "#[derive(Layout)] #[layout(words = 1)] struct W { \
                 #[bits(8)] #[overlay(decode: Nib)] a: u8, #[bits(8)] b: u8 }",
            ),
            "overlay `decode`: writes `fn decode`, which `W` already has",
        ),
    ];
    for (src, reason) in cases {
        refused(&src, reason);
    }
}

#[test]
fn prefix_value_is_word_mode_only() {
    for layout in ["bytes = 1", "words = 1"] {
        refused(
            &format!(
                "#[derive(Layout)] #[layout({layout}, prefix_value = 5)] struct W {{ a: u8 }}"
            ),
            "`W`: `prefix_value` only applies to a `bits = N` layout",
        );
    }
}