packtool-macro 0.6.0

In memory packing made easy: procedural macro definitions
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
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
use proc_macro2::TokenStream;
use quote::quote;
use syn::Result;

use crate::ast::{
    AccessorType, Container, Data, PackedAttributes, PackedEnum, PackedField, PackedStruct,
    PackedTuple, PackedUnitOrigin, PackedVariant, ValueType,
};

pub fn packed_definitions(container: Container) -> TokenStream {
    let ident = container.ident();

    if let Err(error) = check(&container) {
        return error.to_compile_error();
    }

    let size = expand_size(&container);
    let check = expand_check(&container);
    let unchecked_read_from_slice = expand_read_from_slice(&container);
    let unchecked_write_to_slice = expand_write_to_slice(&container);
    let accessors = expand_accessors(&container);

    // Capture the type's generics and, for every type parameter, inject a
    // `T: Packed` bound into the where-clause — the standard `derive` pattern.
    // That makes `<T as Packed>::SIZE` and the read/write/check calls valid in
    // the generated body. For concrete types `generics` is empty, so
    // `split_for_impl()` yields nothing and the output is unchanged.
    //
    // LIFETIMES: we only bind type parameters here. A `Packed` type OWNS its
    // bytes — `unchecked_read_from_slice` reconstructs an owned `Self` from a
    // slice, and there are no borrowing fields — so a lifetime parameter cannot
    // meaningfully appear on a `Packed` struct: any `&'a _` field would itself
    // have to be `Packed`, which borrows do not implement. We therefore neither
    // bind nor special-case lifetimes; a lifetime-bearing field simply fails the
    // `T: Packed` requirement on that field, as it should.
    let mut generics = container.generics();
    let type_param_idents: Vec<syn::Ident> =
        generics.type_params().map(|tp| tp.ident.clone()).collect();
    if !type_param_idents.is_empty() {
        let where_clause = generics.make_where_clause();
        for ident in &type_param_idents {
            where_clause
                .predicates
                .push(syn::parse_quote!(#ident: ::packtool::Packed));
        }
    }
    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

    quote! {
        impl #impl_generics #ident #ty_generics #where_clause {
            #accessors
        }

        impl #impl_generics Packed for #ident #ty_generics #where_clause {
            const SIZE: usize = #size;

            #unchecked_read_from_slice
            #unchecked_write_to_slice

            #check
        }
    }
}

fn check(container: &Container) -> Result<()> {
    match &container.data {
        Data::Unit(unit) => {
            check_no_attribute_accessor("Unit", &container.attributes)?;
            check_no_attribute_fallback("Unit", &container.attributes)?;
            // all unit types need to have a value associated
            if container.attributes.value.is_none() {
                return Err(syn::Error::new_spanned(
                    unit._struct_token,
                    "Expecting a value associated to this type (#[packed(valued = ...)])",
                ));
            }
        }
        Data::Tuple(t) => {
            check_no_attribute_value(
                "an unnamed struct (parenthesis struct)",
                &container.attributes,
            )?;
            check_no_attribute_accessor(
                "an unnamed struct (parenthesis struct)",
                &container.attributes,
            )?;
            check_no_attribute_fallback(
                "an unnamed struct (parenthesis struct)",
                &container.attributes,
            )?;
            check_no_value_in_field(&t.fields)?;
        }
        Data::Struct(s) => {
            check_no_attribute_value("a named struct (braced struct)", &container.attributes)?;
            check_no_attribute_accessor("a named struct (braced struct)", &container.attributes)?;
            check_no_attribute_fallback("a named struct (braced struct)", &container.attributes)?;
            check_no_value_in_field(&s.fields)?;
        }
        Data::Enum(enumeration) => {
            check_no_attribute_value("an enum", &container.attributes)?;
            check_no_attribute_accessor("an enum", &container.attributes)?;
            check_no_attribute_fallback("an enum", &container.attributes)?;
            check_enum(container, enumeration)?;
        }
    }

    Ok(())
}

/// Validate a packed enum.
///
/// The supported shapes are:
///
/// * a pure unit enum — every variant is a unit with an explicit discriminant
///   and the enum carries a `#[repr(...)]` (the historical behaviour); or
/// * the above PLUS at most one catch-all `#[packed(fallback)]` variant, which
///   must be a single-field tuple variant with NO discriminant, whose field
///   type is exactly the `#[repr(...)]` integer type.
///
/// Any other shape — a data-carrying non-fallback variant, more than one
/// fallback, a fallback of the wrong arity, or a fallback whose field type does
/// not match the repr — is rejected here at compile time.
fn check_enum(container: &Container, enumeration: &PackedEnum) -> Result<()> {
    // at most one fallback variant.
    let fallbacks: Vec<&PackedVariant> = enumeration
        .variants
        .iter()
        .filter(|v| v.attributes.fallback)
        .collect();
    if let Some(extra) = fallbacks.get(1) {
        return Err(syn::Error::new_spanned(
            &extra.ident,
            "packed enums may declare at most one #[packed(fallback)] variant",
        ));
    }
    let fallback = fallbacks.first().copied();

    // every non-fallback variant must be a unit variant with an explicit
    // discriminant — unchanged from the historical behaviour.
    for variant in enumeration
        .variants
        .iter()
        .filter(|v| !v.attributes.fallback)
    {
        if !variant.fields.is_empty() {
            return Err(syn::Error::new_spanned(
                enumeration._struct_token,
                "packed enums with data-carrying variants are not supported yet",
            ));
        }
        if variant.discriminant.is_none() {
            return Err(syn::Error::new_spanned(
                &variant.ident,
                "Missing explicit discriminant for packed enum",
            ));
        }
    }

    // every packed enum needs a repr to fix its wire size.
    let repr = match container.attributes.repr.as_ref() {
        Some(repr) => repr,
        None => {
            return Err(syn::Error::new_spanned(
                enumeration._struct_token,
                "Pure enumeration variants should have a repr(...) attributes to set the size",
            ));
        }
    };

    // validate the fallback variant's shape.
    if let Some(fallback) = fallback {
        if fallback.discriminant.is_some() {
            return Err(syn::Error::new_spanned(
                &fallback.ident,
                "a #[packed(fallback)] variant must not declare a discriminant",
            ));
        }
        if fallback.fields.len() != 1 {
            return Err(syn::Error::new_spanned(
                &fallback.ident,
                "a #[packed(fallback)] variant must be a single-field tuple variant carrying the #[repr(...)] integer",
            ));
        }
        let field = fallback
            .fields
            .first()
            .expect("the fallback variant has exactly one field");
        if !fallback_field_matches_repr(&field.ty, repr) {
            return Err(syn::Error::new_spanned(
                &field.ty,
                "a #[packed(fallback)] variant's field type must be the same integer type as the enum's #[repr(...)]",
            ));
        }
    }

    Ok(())
}

/// `true` when the fallback field's type is exactly the repr integer type
/// (e.g. `Other(u16)` for `#[repr(u16)]`).
fn fallback_field_matches_repr(ty: &syn::Type, repr: &syn::Path) -> bool {
    let repr_ident = match repr.get_ident() {
        Some(ident) => ident,
        None => return false,
    };
    match ty {
        syn::Type::Path(type_path) if type_path.qself.is_none() => {
            type_path.path.is_ident(repr_ident)
        }
        _ => false,
    }
}

fn check_no_attribute_accessor(scope: &str, attributes: &PackedAttributes) -> Result<()> {
    if !matches!(attributes.accessor, AccessorType::Default) {
        return Err(syn::Error::new(
            proc_macro2::Span::call_site(),
            format!(
                "Cannot have an accessor associated to {scope}",
                scope = scope
            ),
        ));
    }
    Ok(())
}

fn check_no_attribute_value(scope: &str, attributes: &PackedAttributes) -> Result<()> {
    if let Some(value) = attributes.value.as_ref() {
        return Err(syn::Error::new(
            value.span(),
            format!("Cannot have a value associated to {scope}", scope = scope),
        ));
    }
    Ok(())
}

/// Reject `#[packed(fallback)]` anywhere it is not a meaningful catch-all: it is
/// only valid on an enum variant, so on any other scope (a struct, tuple, unit,
/// the enum container itself, or a field) it would otherwise be silently ignored.
/// Mirrors `check_no_attribute_value` / `check_no_attribute_accessor`.
fn check_no_attribute_fallback(scope: &str, attributes: &PackedAttributes) -> Result<()> {
    if attributes.fallback {
        return Err(syn::Error::new(
            proc_macro2::Span::call_site(),
            format!("`#[packed(fallback)]` is only valid on an enum variant, not on {scope}"),
        ));
    }
    Ok(())
}

fn check_no_value_in_field<'a, I>(fields: I) -> Result<()>
where
    I: IntoIterator<Item = &'a PackedField>,
{
    for field in fields {
        check_no_attribute_value("a field of a structure", &field.attributes)?;
        check_no_attribute_fallback("a field of a structure", &field.attributes)?;
    }

    Ok(())
}

fn expand_size_from_types<'a, I>(fields: I) -> TokenStream
where
    I: IntoIterator<Item = &'a PackedField>,
{
    let fields = fields.into_iter().map(|f| &f.ty);
    quote! { #( < #fields as Packed >::SIZE )+* }
}

fn expand_size_from_enumeration(repr: &syn::Path, enumeration: &PackedEnum) -> TokenStream {
    assert!(
        !enumeration.variants.is_empty(),
        "unit enums should have been converted to a packed_unit"
    );

    if enumeration.fallback_variant().is_some() {
        // a data-carrying fallback variant inflates `size_of::<Enum>()`, so the
        // wire size is taken from the repr integer width instead — the fallback
        // carries exactly the repr int and no extra bytes.
        quote! { ::core::mem::size_of::<#repr>() }
    } else if enumeration.only_unit_variants() {
        let ident = enumeration.ident();
        quote! { ::core::mem::size_of::<#ident>() }
    } else {
        todo!("variadic size enumeration not working yet")
    }
}

fn expand_size_from_value_type(ident: &syn::Ident, value: &ValueType) -> TokenStream {
    match value {
        ValueType::Lit(lit) => expand_size_from_lit(ident, lit),
        ValueType::Const(con) => {
            //
            quote! { ::std::mem::size_of_val(& #con) }
        }
    }
}

fn expand_size_from_lit(ident: &syn::Ident, value: &syn::Lit) -> TokenStream {
    match value {
        syn::Lit::Str(string) => {
            let len = string.value().len();
            quote! { #len }
        }
        syn::Lit::ByteStr(bytes) => {
            let len = bytes.value().len();
            quote! { #len }
        }
        syn::Lit::Byte(_) => {
            quote! { 1 }
        }
        syn::Lit::Char(char) => {
            let len = char.value().len_utf8();
            quote! { #len }
        }
        syn::Lit::Int(int) => {
            if int.suffix().is_empty() {
                quote! { ::core::mem::size_of::<#ident>() }
            } else {
                let ident = syn::Ident::new(int.suffix(), int.span());
                quote! { ::core::mem::size_of::<#ident>() }
            }
        }
        syn::Lit::Float(_) => {
            syn::Error::new_spanned(value, "floating point values are not supported")
                .to_compile_error()
        }
        syn::Lit::Bool(_) => {
            syn::Error::new_spanned(value, "boolean values are not supported").to_compile_error()
        }
        syn::Lit::Verbatim(_) => {
            syn::Error::new_spanned(value, "verbatim values are not supported").to_compile_error()
        }
        _ => syn::Error::new_spanned(value, "unsupported literal value").to_compile_error(),
    }
}

fn expand_size(container: &Container) -> TokenStream {
    match &container.data {
        Data::Unit(_) => expand_size_from_value_type(
            container.ident(),
            container
                .attributes
                .value
                .as_ref()
                .expect("all units must have a packed(value = %)"),
        ),
        Data::Tuple(tuple) => expand_size_from_types(&tuple.fields),
        Data::Struct(structure) => expand_size_from_types(&structure.fields),
        Data::Enum(enumeration) => expand_size_from_enumeration(
            container
                .attributes
                .repr
                .as_ref()
                .expect("Should have a repr on every enums"),
            enumeration,
        ),
    }
}

fn expand_check_data_unit_value(ident: &syn::Ident, value: &ValueType) -> TokenStream {
    match value {
        ValueType::Lit(lit) => expand_check_data_unit(ident, lit),
        ValueType::Const(con) => {
            quote! {
                fn check(slice: &[u8]) -> ::std::result::Result<(), ::packtool::Error> {
                    fn check_<C: Packed + ::std::fmt::Debug + PartialEq>(con: C, slice: &[u8]) -> ::std::result::Result<(), ::packtool::Error> {
                        let value = <C as Packed>::unchecked_read_from_slice(slice);

                        ::packtool::ensure!(
                            #ident,
                            value == con,
                            "Invalid value, expected {expected:?} but received {received:?}",
                            expected = con,
                            received = slice,
                        );

                        Ok(())
                    }

                    check_(#con, slice)

                }
            }
        }
    }
}

fn expand_check_data_unit(ident: &syn::Ident, value: &syn::Lit) -> TokenStream {
    match value {
        syn::Lit::Str(string) => {
            quote! {
                fn check(slice: &[u8]) -> ::std::result::Result<(), ::packtool::Error> {
                    ::packtool::ensure!(
                        #ident,
                        slice == #string.as_bytes(),
                        "Invalid string, expected {expected} but received {received}",
                        expected = #string,
                        received = ::std::string::String::from_utf8_lossy(slice),
                    );

                    Ok(())
                }
            }
        }
        syn::Lit::ByteStr(bytes) => {
            quote! {
                fn check(slice: &[u8]) -> ::std::result::Result<(), ::packtool::Error> {
                    ::packtool::ensure!(
                        #ident,
                        slice == #bytes,
                        "Invalid string, expected {expected:?} but received {received:?}",
                        expected = #bytes,
                        received = slice,
                    );

                    Ok(())
                }
            }
        }
        syn::Lit::Byte(byte) => {
            quote! {
                fn check(slice: &[u8]) -> ::std::result::Result<(), ::packtool::Error> {
                    ::packtool::ensure!(
                        #ident,
                        slice[0] == #byte,
                        "Invalid byte string, expected {expected:X} but received {received:X}",
                        expected = #byte,
                        received = slice[0],
                    );

                    Ok(())
                }
            }
        }
        syn::Lit::Char(char) => {
            quote! {
                fn check(slice: &[u8]) -> ::std::result::Result<(), ::packtool::Error> {
                    use ::packtool::Context as _;
                    let c = ::std::str::from_utf8(slice)
                        .context("Failed to parse valid utf8 char from the slice")?;

                    ::packtool::ensure!(
                        #ident,
                        c.chars().next() == Some(#char),
                        "Invalid UTF8 encoded char, expected {expected} but received {received}",
                        expected = #char,
                        received = c,
                    );

                    Ok(())
                }
            }
        }
        syn::Lit::Int(int) => {
            if int.suffix().is_empty() {
                syn::Error::new_spanned(
                    int,
                    "expect to know the exact type of the value, add suffix (like in `0i64`)",
                )
                .to_compile_error()
            } else {
                let ident = syn::Ident::new(int.suffix(), int.span());
                quote! {
                    fn check(slice: &[u8]) -> ::std::result::Result<(), ::packtool::Error> {
                        use ::packtool::Context as _;
                        use ::core::convert::TryInto as _;
                        let int = <#ident>::from_le_bytes(
                            slice.try_into()
                                .context("expecting to parse integer value")?
                        );

                        ::packtool::ensure!(
                            #ident,
                            int == #int,
                            "Invalid packed integer, expected {expected} but received {received}",
                            expected = #int,
                            received = int,
                        );

                        Ok(())
                    }
                }
            }
        }
        syn::Lit::Float(_) => {
            syn::Error::new_spanned(value, "floating point values are not supported")
                .to_compile_error()
        }
        syn::Lit::Bool(_) => {
            syn::Error::new_spanned(value, "boolean values are not supported").to_compile_error()
        }
        syn::Lit::Verbatim(_) => {
            syn::Error::new_spanned(value, "verbatim values are not supported").to_compile_error()
        }
        _ => syn::Error::new_spanned(value, "unsupported literal value").to_compile_error(),
    }
}

fn expand_check_data_field(
    field: &PackedField,
    index: usize,
    start: TokenStream,
) -> (TokenStream, TokenStream) {
    let ty = &field.ty;
    let on_error = if let Some(ident) = field.ident.as_ref() {
        quote! {
            context(
                ::packtool::Error::invalid_field::<#ty>(
                    stringify!(#ident)
                )
            )
        }
    } else {
        quote! {
            context(
                ::packtool::Error::invalid_tuple::<#ty>(
                    #index
                )
            )
        }
    };

    let end = quote! {
        #start + <#ty as Packed>::SIZE
    };
    let quote = quote! {
        <#ty as Packed>::check(&slice[(#start)..(#end)]).#on_error?;
    };

    (quote, end)
}

fn expand_check_data_fields<'a, I>(fields: I) -> TokenStream
where
    I: IntoIterator<Item = &'a PackedField>,
{
    let mut checks = Vec::new();

    let mut start = quote! { 0 };
    for (index, field) in fields.into_iter().enumerate() {
        let (check, end) = expand_check_data_field(field, index, start.clone());
        checks.push(check);
        start = end;
    }

    quote! { #(#checks)* }
}

fn expand_check_data_variants<'a, I>(repr: &syn::Path, variants: I) -> TokenStream
where
    I: IntoIterator<Item = &'a PackedVariant>,
{
    let mut discriminants = Vec::new();

    for variant in variants.into_iter() {
        let discriminant = if let Some(discriminant) = variant.discriminant.as_ref() {
            discriminant
        } else {
            panic!("should always be a discriminant")
        };
        discriminants.push(&discriminant.1);
    }

    let value = if repr.is_ident("u8") {
        quote! { slice[0] }
    } else if repr.is_ident("i8") {
        quote! { slice[0] as i8 }
    } else {
        quote! {
            <#repr>::from_le_bytes(
                slice.try_into()
                    .context("invalid length")?
            )
        }
    };

    quote! {
        match #value {
            #[allow(clippy::unused_unit)]
            # ( #discriminants )|* => {
                ()
            }
            found => return Err(
                ::packtool::Error::invalid_discriminant::<Self, _>(
                    found,
                    ::core::concat!(#(#discriminants , ", "),*),
                )
            ),
        }
    }
}

fn expand_check_data_tuple(tuple: &PackedTuple) -> TokenStream {
    let fields = expand_check_data_fields(&tuple.fields);
    quote! {
        fn check(slice: &[u8]) -> ::std::result::Result<(), ::packtool::Error> {
            use ::core::convert::TryInto as _;
            use ::packtool::Context as _;

            #fields

            Ok(())
        }
    }
}

fn expand_check_data_structure(structure: &PackedStruct) -> TokenStream {
    let fields = expand_check_data_fields(&structure.fields);

    quote! {
        fn check(slice: &[u8]) -> ::std::result::Result<(), ::packtool::Error> {
            use ::core::convert::TryInto as _;
            use ::packtool::Context as _;

            #fields

            Ok(())
        }
    }
}

fn expand_check_data_enumeration(repr: &syn::Path, enumeration: &PackedEnum) -> TokenStream {
    if enumeration.fallback_variant().is_some() {
        // with a fallback every repr value is valid: an unknown discriminant
        // simply decodes into the catch-all variant carrying the raw integer.
        return quote! {
            fn check(_slice: &[u8]) -> ::std::result::Result<(), ::packtool::Error> {
                Ok(())
            }
        };
    }

    let variants = expand_check_data_variants(repr, &enumeration.variants);

    quote! {
        fn check(slice: &[u8]) -> ::std::result::Result<(), ::packtool::Error> {
            use ::core::convert::TryInto as _;
            use ::packtool::Context as _;

            #variants

            Ok(())
        }
    }
}

fn expand_check(container: &Container) -> TokenStream {
    match &container.data {
        Data::Unit(_) => expand_check_data_unit_value(
            container.ident(),
            container
                .attributes
                .value
                .as_ref()
                .expect("all units must have a packed(value = %)"),
        ),
        Data::Tuple(tuple) => expand_check_data_tuple(tuple),
        Data::Struct(structure) => expand_check_data_structure(structure),
        Data::Enum(enumeration) => expand_check_data_enumeration(
            container
                .attributes
                .repr
                .as_ref()
                .expect("Should have a repr on every enums"),
            enumeration,
        ),
    }
}

fn expand_read_from_slice_data_unit(ident: &syn::Ident, from: &PackedUnitOrigin) -> TokenStream {
    let constructor = match from {
        PackedUnitOrigin::Unit => quote! { #ident },
        PackedUnitOrigin::Tuple => quote! { #ident () },
        PackedUnitOrigin::Brace => quote! { #ident {} },
    };

    quote! {
        fn unchecked_read_from_slice(_view: &[u8]) -> Self {
            #constructor
        }
    }
}

fn expand_read_from_slice_data_variants(
    repr: &syn::Path,
    ident: &syn::Ident,
    enumeration: &PackedEnum,
) -> TokenStream {
    let fallback = enumeration.fallback_variant();

    let mut discriminants = Vec::new();

    for variant in enumeration
        .variants
        .iter()
        .filter(|v| !v.attributes.fallback)
    {
        let (_, discriminant) = if let Some(discriminant) = variant.discriminant.as_ref() {
            discriminant
        } else {
            panic!("should always be a discriminant")
        };
        let variant = &variant.ident;

        discriminants.push({
            quote! {
                #discriminant => { #ident :: #variant }
            }
        });
    }

    let value = if repr.is_ident("u8") {
        quote! { slice[0] }
    } else if repr.is_ident("i8") {
        quote! { slice[0] as i8 }
    } else {
        quote! {
            <#repr>::from_le_bytes(
                slice.try_into().unwrap()
            )
        }
    };

    // an unknown discriminant either decodes into the fallback variant carrying
    // the raw repr integer, or — with no fallback — is unreachable because
    // `check` already rejected it.
    let default = if let Some(fallback) = fallback {
        let fallback = &fallback.ident;
        quote! { unknown => { #ident :: #fallback ( unknown ) } }
    } else {
        quote! { _ => ::core::panic!("Invalid discriminant") }
    };

    quote! {
        match #value {
            #( #discriminants )*
            #default
        }
    }
}

fn expand_read_from_slice_data_enumeration(
    repr: &syn::Path,
    ident: &syn::Ident,
    enumeration: &PackedEnum,
) -> TokenStream {
    let variants = expand_read_from_slice_data_variants(repr, ident, enumeration);

    quote! {
        fn unchecked_read_from_slice(slice: &[u8]) -> Self {
            use ::core::convert::TryInto as _;

            #variants
        }
    }
}

fn expand_read_from_slice_data_field(
    field: &PackedField,
    start: TokenStream,
) -> (TokenStream, TokenStream) {
    let ty = &field.ty;

    let end = quote! {
        #start + <#ty as Packed>::SIZE
    };
    let quote = if let Some(ident) = field.ident.as_ref() {
        quote! {
            #ident : <#ty as Packed>::unchecked_read_from_slice(&slice[(#start)..(#end)])
        }
    } else {
        quote! {
            <#ty as Packed>::unchecked_read_from_slice(&slice[(#start)..(#end)])
        }
    };

    (quote, end)
}

fn expand_read_from_slice_data_fields<'a, I>(fields: I) -> TokenStream
where
    I: IntoIterator<Item = &'a PackedField>,
{
    let mut checks = Vec::new();

    let mut start = quote! { 0 };
    for field in fields.into_iter() {
        let (check, end) = expand_read_from_slice_data_field(field, start.clone());
        checks.push(check);
        start = end;
    }

    quote! { #(#checks),* }
}

fn expand_read_from_slice_data_tuple(tuple: &PackedTuple) -> TokenStream {
    let ident = tuple.ident();
    let fields = expand_read_from_slice_data_fields(&tuple.fields);
    quote! {
        fn unchecked_read_from_slice(slice: &[u8]) -> Self {
            use ::core::convert::TryInto as _;

            #ident (
                #fields
            )
        }
    }
}

fn expand_read_from_slice_data_structure(structure: &PackedStruct) -> TokenStream {
    let fields = expand_read_from_slice_data_fields(&structure.fields);
    let ident = structure.ident();

    quote! {
        fn unchecked_read_from_slice(slice: &[u8]) -> Self {
            use ::core::convert::TryInto as _;

            #ident {
                #fields
            }
        }
    }
}

fn expand_read_from_slice(container: &Container) -> TokenStream {
    match &container.data {
        Data::Unit(unit) => expand_read_from_slice_data_unit(container.ident(), &unit.from),
        Data::Tuple(tuple) => expand_read_from_slice_data_tuple(tuple),
        Data::Struct(structure) => expand_read_from_slice_data_structure(structure),
        Data::Enum(enumeration) => expand_read_from_slice_data_enumeration(
            container
                .attributes
                .repr
                .as_ref()
                .expect("Should have a repr on every enums"),
            container.ident(),
            enumeration,
        ),
    }
}

fn expand_write_to_slice_data_unit_value(value: &ValueType) -> TokenStream {
    match value {
        ValueType::Lit(lit) => expand_write_to_slice_data_unit(lit),
        ValueType::Const(con) => {
            quote! {
                fn unchecked_write_to_slice(&self, slice: &mut [u8]) {
                    #con.unchecked_write_to_slice(slice);
                }
            }
        }
    }
}

fn expand_write_to_slice_data_unit(value: &syn::Lit) -> TokenStream {
    match value {
        syn::Lit::Str(string) => {
            quote! {
                fn unchecked_write_to_slice(&self, slice: &mut [u8]) {
                    slice.copy_from_slice(#string.as_bytes());
                }
            }
        }
        syn::Lit::ByteStr(bytes) => {
            quote! {
                fn unchecked_write_to_slice(&self, slice: &mut [u8]) {
                    slice.copy_from_slice(#bytes);
                }
            }
        }
        syn::Lit::Byte(byte) => {
            quote! {
                fn unchecked_write_to_slice(&self, slice: &mut [u8]) {
                    slice[0] = #byte;
                }
            }
        }
        syn::Lit::Char(char) => {
            quote! {
                fn unchecked_write_to_slice(&self, slice: &mut [u8]) {
                    slice.copy_from_slice(#char.encode_utf8(&mut [0u8; 4]).as_bytes());
                }
            }
        }
        syn::Lit::Int(int) => {
            if int.suffix().is_empty() {
                syn::Error::new_spanned(
                    int,
                    "expect to know the exact type of the value, add suffix (like in `0i64`)",
                )
                .to_compile_error()
            } else {
                quote! {
                    fn unchecked_write_to_slice(&self, slice: &mut [u8]) {
                        slice.copy_from_slice(
                            &(#int).to_le_bytes()
                        );
                    }
                }
            }
        }
        syn::Lit::Float(_) => {
            syn::Error::new_spanned(value, "floating point values are not supported")
                .to_compile_error()
        }
        syn::Lit::Bool(_) => {
            syn::Error::new_spanned(value, "boolean values are not supported").to_compile_error()
        }
        syn::Lit::Verbatim(_) => {
            syn::Error::new_spanned(value, "verbatim values are not supported").to_compile_error()
        }
        _ => syn::Error::new_spanned(value, "unsupported literal value").to_compile_error(),
    }
}

fn expand_write_to_slice_data_variants(
    repr: &syn::Path,
    ident: &syn::Ident,
    enumeration: &PackedEnum,
) -> TokenStream {
    let mut discriminants = Vec::new();

    for variant in enumeration.variants.iter() {
        let variant_ident = &variant.ident;

        if variant.attributes.fallback {
            // the fallback variant binds its raw repr integer and writes it
            // verbatim — exactly the repr width, no discriminant.
            let value = if repr.is_ident("u8") {
                quote! { slice[0] = *value; }
            } else if repr.is_ident("i8") {
                // Reinterpret the signed byte as `u8` for the single-byte slot —
                // the inverse of the `slice[0] as i8` read.
                quote! { slice[0] = *value as u8; }
            } else {
                quote! {
                    slice.copy_from_slice(&<#repr>::to_le_bytes(*value));
                }
            };

            discriminants.push(quote! {
                #ident :: #variant_ident ( value ) => { #value }
            });
            continue;
        }

        let (_, discriminant) = if let Some(discriminant) = variant.discriminant.as_ref() {
            discriminant
        } else {
            panic!("should always be a discriminant")
        };

        let value = if repr.is_ident("u8") {
            quote! { slice[0] = #discriminant; }
        } else if repr.is_ident("i8") {
            // Store the signed discriminant as its `u8` byte (the inverse of the
            // `slice[0] as i8` read); `as i8` here would assign an `i8` to a `u8`.
            // Parenthesised so a negative discriminant emits `(-5) as u8`, not
            // `-(5 as u8)` — the latter negates a `u8` (E0600).
            quote! { slice[0] = (#discriminant) as u8; }
        } else {
            quote! {
                slice.copy_from_slice(&<#repr>::to_le_bytes(#discriminant));
            }
        };

        discriminants.push({
            quote! {
                #ident :: #variant_ident => { #value }
            }
        });
    }

    quote! {
        match self {
            #( #discriminants ),*
        }
    }
}

fn expand_write_to_slice_data_enumeration(
    repr: &syn::Path,
    ident: &syn::Ident,
    enumeration: &PackedEnum,
) -> TokenStream {
    let variants = expand_write_to_slice_data_variants(repr, ident, enumeration);

    quote! {
        fn unchecked_write_to_slice(&self, slice: &mut [u8]) {
            #variants
        }
    }
}

fn expand_write_to_slice_data_field(
    field: &PackedField,
    index: syn::Index,
    start: TokenStream,
) -> (TokenStream, TokenStream) {
    let ty = &field.ty;

    let end = quote! {
        #start + <#ty as Packed>::SIZE
    };
    let quote = if let Some(ident) = field.ident.as_ref() {
        quote! {
            self.#ident.unchecked_write_to_slice(&mut slice[(#start)..(#end)])
        }
    } else {
        quote! {
            self.#index.unchecked_write_to_slice(&mut slice[(#start)..(#end)])
        }
    };

    (quote, end)
}

fn expand_write_to_slice_data_fields<'a, I>(fields: I) -> TokenStream
where
    I: IntoIterator<Item = &'a PackedField>,
{
    let mut checks = Vec::new();

    let mut start = quote! { 0 };
    for (index, field) in fields.into_iter().enumerate() {
        let (check, end) =
            expand_write_to_slice_data_field(field, syn::Index::from(index), start.clone());
        checks.push(check);
        start = end;
    }

    quote! { #(#checks);* }
}

fn expand_write_to_slice_data_tuple(tuple: &PackedTuple) -> TokenStream {
    let fields = expand_write_to_slice_data_fields(&tuple.fields);
    quote! {
        fn unchecked_write_to_slice(&self, slice: &mut [u8]) {
            use ::core::convert::TryInto as _;

            #fields
        }
    }
}

fn expand_write_to_slice_data_structure(structure: &PackedStruct) -> TokenStream {
    let fields = expand_write_to_slice_data_fields(&structure.fields);

    quote! {
        fn unchecked_write_to_slice(&self, slice: &mut [u8]) {
            #fields
        }
    }
}

fn expand_write_to_slice(container: &Container) -> TokenStream {
    match &container.data {
        Data::Unit(_) => {
            expand_write_to_slice_data_unit_value(container.attributes.value.as_ref().unwrap())
        }
        Data::Tuple(tuple) => expand_write_to_slice_data_tuple(tuple),
        Data::Struct(structure) => expand_write_to_slice_data_structure(structure),
        Data::Enum(enumeration) => expand_write_to_slice_data_enumeration(
            container
                .attributes
                .repr
                .as_ref()
                .expect("Should have a repr on every enums"),
            container.ident(),
            enumeration,
        ),
    }
}

fn expand_field_accessor(
    field: &PackedField,
    index: usize,
    start: TokenStream,
) -> (TokenStream, TokenStream) {
    let ty = &field.ty;
    let end = quote! {
        #start + <#ty as Packed>::SIZE
    };

    let ident = match &field.attributes.accessor {
        AccessorType::Ignore => return (quote! {}, end),
        AccessorType::Custom(ident) => ident.clone(),
        AccessorType::Default => {
            if let Some(ident) = field.ident.as_ref() {
                ident.clone()
            } else {
                syn::Ident::new(&format!("_{}", index), proc_macro2::Span::call_site())
            }
        }
    };

    let accessor = quote! {
        pub fn #ident<'a>(view: ::packtool::View<'a, Self>) -> ::packtool::View<'a, #ty> {
            ::packtool::View::unchecked_from_slice(&view.as_slice()[#start..#end])
        }
    };

    (accessor, end)
}

fn expand_fields_accessors<'a, I>(fields: I) -> TokenStream
where
    I: IntoIterator<Item = &'a PackedField>,
{
    let mut fields_accessors = Vec::new();

    let mut start = quote! { 0 };
    for (index, field) in fields.into_iter().enumerate() {
        let (accessor, end) = expand_field_accessor(field, index, start.clone());
        fields_accessors.push(accessor);
        start = end;
    }

    quote! {
        #( #fields_accessors )*
    }
}

fn expand_tuple_accessors(tuple: &PackedTuple) -> TokenStream {
    let fields_accessors = expand_fields_accessors(&tuple.fields);

    quote! {
         #fields_accessors
    }
}

fn expand_structure_accessors(structure: &PackedStruct) -> TokenStream {
    let fields_accessors = expand_fields_accessors(&structure.fields);

    quote! {
         #fields_accessors
    }
}

fn expand_accessors(container: &Container) -> TokenStream {
    match &container.data {
        Data::Unit(_) => {
            // no accessor for the unit type
            quote! {}
        }
        Data::Enum(_enumeration) => {
            // no accessor for the enum type
            quote! {}
        }
        Data::Tuple(tuple) => expand_tuple_accessors(tuple),
        Data::Struct(structure) => expand_structure_accessors(structure),
    }
}