senax-encoder-derive 0.2.1

A fast, compact, and schema-evolution-friendly binary serialization library for Rust.
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
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
extern crate proc_macro;

use crc::{Crc, CRC_64_ECMA_182};
use itertools::izip;
use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::quote;
use std::collections::{HashMap, HashSet};
use syn::{
    parse_macro_input, Attribute, Data, DeriveInput, Fields, GenericArgument, Ident, PathArguments,
    Type,
};

/// CRC-64 hasher for field name to ID conversion
const CRC64: Crc<u64> = Crc::<u64>::new(&CRC_64_ECMA_182);

/// Calculate a unique field ID from a field name using CRC-64
///
/// This function generates a deterministic 64-bit ID from a field name by computing
/// the CRC-64 hash. This ensures consistent field IDs across compilation runs
/// while providing excellent distribution and minimizing collision probability.
///
/// # Arguments
///
/// * `name` - The field name to hash
///
/// # Returns
///
/// A 64-bit field ID (never 0, as 0 is reserved as a terminator)
fn calculate_id_from_name(name: &str) -> u64 {
    let crc64_hash = CRC64.checksum(name.as_bytes());
    // Ensure it's not 0 (0 is reserved as terminator)
    if crc64_hash == 0 {
        u64::MAX
    } else {
        crc64_hash
    }
}

/// Generate structure information text for CRC64 hashing
///
/// This function creates a deterministic text representation of the structure
/// that includes type name, field names, and field types. This is used to
/// generate a structure hash for pack format validation.
///
/// # Arguments
///
/// * `input` - The parsed derive input containing structure information
///
/// # Returns
///
/// A string containing the structure information
fn generate_structure_info(input: &DeriveInput) -> String {
    let mut info = String::new();
    info.push_str(&format!("type:{}", input.ident));

    match &input.data {
        Data::Struct(s) => {
            info.push_str("|struct");
            match &s.fields {
                Fields::Named(fields) => {
                    info.push_str("|named");
                    for field in &fields.named {
                        let field_name = field.ident.as_ref().unwrap().to_string();
                        let field_type = {
                            let ty = &field.ty;
                            quote!(#ty).to_string()
                        };
                        info.push_str(&format!("|{}:{}", field_name, field_type));
                    }
                }
                Fields::Unnamed(fields) => {
                    info.push_str("|unnamed");
                    for (i, field) in fields.unnamed.iter().enumerate() {
                        let field_type = {
                            let ty = &field.ty;
                            quote!(#ty).to_string()
                        };
                        info.push_str(&format!("|{}:{}", i, field_type));
                    }
                }
                Fields::Unit => {
                    info.push_str("|unit");
                }
            }
        }
        Data::Enum(e) => {
            info.push_str("|enum");
            for variant in &e.variants {
                let variant_name = variant.ident.to_string();
                info.push_str(&format!("|variant:{}", variant_name));
                match &variant.fields {
                    Fields::Named(fields) => {
                        info.push_str("|named");
                        for field in &fields.named {
                            let field_name = field.ident.as_ref().unwrap().to_string();
                            let field_type = {
                                let ty = &field.ty;
                                quote!(#ty).to_string()
                            };
                            info.push_str(&format!("|{}:{}", field_name, field_type));
                        }
                    }
                    Fields::Unnamed(fields) => {
                        info.push_str("|unnamed");
                        for (i, field) in fields.unnamed.iter().enumerate() {
                            let field_type = {
                                let ty = &field.ty;
                                quote!(#ty).to_string()
                            };
                            info.push_str(&format!("|{}:{}", i, field_type));
                        }
                    }
                    Fields::Unit => {
                        info.push_str("|unit");
                    }
                }
            }
        }
        Data::Union(_) => {
            info.push_str("|union");
        }
    }

    info
}

/// Check if a variant has the #[default] attribute
fn has_default_attribute(attrs: &[Attribute]) -> bool {
    attrs.iter().any(|attr| attr.path().is_ident("default"))
}

/// Field attributes parsed from `#[senax(...)]` annotations
///
/// This struct represents the various attributes that can be applied to fields
/// in structs and enum variants using the `#[senax(...)]` attribute macro.
///
/// # Fields
///
/// * `id` - The unique identifier for this field (computed from name or explicitly set)
/// * `default` - Whether to use default values when the field is missing during decode
/// * `skip_encode` - Whether to exclude this field from encoding
/// * `skip_decode` - Whether to ignore this field during decoding
/// * `skip_default` - Whether to use default value if field is missing
/// * `rename` - Optional alternative name for ID calculation (maintains compatibility when renaming)
#[derive(Debug, Clone)]
#[allow(dead_code)] // The rename field is used indirectly in ID calculation
struct FieldAttributes {
    id: u64,
    default: bool,
    skip_encode: bool,
    skip_decode: bool,
    skip_default: bool,
    rename: Option<String>,
}

/// Container attributes parsed from `#[senax(...)]` annotations at struct/enum level
///
/// This struct represents attributes that can be applied to the entire struct or enum.
///
/// # Fields
///
/// * `disable_encode` - Whether to generate stub implementations for Encode/Decode traits
/// * `disable_pack` - Whether to generate stub implementations for Pack/Unpack traits
#[derive(Debug, Clone, Default)]
struct ContainerAttributes {
    disable_encode: bool,
    disable_pack: bool,
}

/// Extract and parse `#[senax(...)]` attribute values from container (struct/enum) attributes
///
/// This function parses the senax attributes applied to a struct or enum and returns
/// a `ContainerAttributes` struct containing all the parsed values.
///
/// # Arguments
///
/// * `attrs` - The attributes array from the struct/enum
///
/// # Returns
///
/// A `ContainerAttributes` struct with parsed values.
///
/// # Supported Attributes
///
/// * `#[senax(disable_encode)]` - Generate stub implementations for Encode/Decode traits (unimplemented!() only)
/// * `#[senax(disable_pack)]` - Generate stub implementations for Pack/Unpack traits (unimplemented!() only)
fn get_container_attributes(attrs: &[Attribute]) -> ContainerAttributes {
    let mut disable_encode = false;
    let mut disable_pack = false;

    for attr in attrs {
        if attr.path().is_ident("senax") {
            let parsed = attr.parse_args_with(|input: syn::parse::ParseStream| {
                let mut parsed_disable_encode = false;
                let mut parsed_disable_pack = false;

                while !input.is_empty() {
                    let ident = input.parse::<syn::Ident>()?;

                    if ident == "disable_encode" {
                        parsed_disable_encode = true;
                    } else if ident == "disable_pack" {
                        parsed_disable_pack = true;
                    } else {
                        return Err(syn::Error::new(
                            ident.span(),
                            format!("Unknown container attribute: {}", ident),
                        ));
                    }

                    // Consume comma if present, otherwise end
                    if input.peek(syn::Token![,]) {
                        input.parse::<syn::Token![,]>()?;
                    }
                }

                Ok((parsed_disable_encode, parsed_disable_pack))
            });

            if let Ok((parsed_disable_encode, parsed_disable_pack)) = parsed {
                disable_encode = disable_encode || parsed_disable_encode;
                disable_pack = disable_pack || parsed_disable_pack;
            }
        }
    }

    ContainerAttributes {
        disable_encode,
        disable_pack,
    }
}

/// Extract and parse `#[senax(...)]` attribute values from field attributes
///
/// This function parses the senax attributes applied to a field and returns
/// a `FieldAttributes` struct containing all the parsed values.
///
/// # Arguments
///
/// * `attrs` - The attributes array from the field
/// * `field_name` - The name of the field (used for ID calculation if no explicit ID is provided)
///
/// # Returns
///
/// A `FieldAttributes` struct with parsed values. If no explicit ID is provided,
/// the ID is calculated using CRC64 hash of either the rename value or the field name.
///
/// # Supported Attributes
///
/// * `#[senax(id=1234)]` - Explicit field ID
/// * `#[senax(default)]` - Use default value if field is missing during decode
/// * `#[senax(skip_encode)]` - Skip this field during encoding
/// * `#[senax(skip_decode)]` - Skip this field during decoding
/// * `#[senax(skip_default)]` - Skip encoding if field value is default, use default if missing during decode
/// * `#[senax(rename="name")]` - Alternative name for ID calculation
///
/// Multiple attributes can be combined: `#[senax(id=123, default, skip_encode)]`
fn get_field_attributes(attrs: &[Attribute], field_name: &str) -> FieldAttributes {
    let mut id = None;
    let mut default = false;
    let mut skip_encode = false;
    let mut skip_decode = false;
    let mut skip_default = false;
    let mut rename = None;

    for attr in attrs {
        if attr.path().is_ident("senax") {
            // Try to parse #[senax(id=1234, default, skip_encode, skip_decode, skip_default, rename="name")]
            let parsed = attr.parse_args_with(|input: syn::parse::ParseStream| {
                let mut parsed_id = None;
                let mut parsed_default = false;
                let mut parsed_skip_encode = false;
                let mut parsed_skip_decode = false;
                let mut parsed_skip_default = false;
                let mut parsed_rename = None;

                while !input.is_empty() {
                    let ident = input.parse::<syn::Ident>()?;

                    if ident == "id" {
                        input.parse::<syn::Token![=]>()?;
                        let lit = input.parse::<syn::LitInt>()?;
                        if let Ok(id_val) = lit.base10_parse::<u64>() {
                            if id_val == 0 {
                                return Err(syn::Error::new(
                                    lit.span(),
                                    "Field ID 0 is reserved as a terminator",
                                ));
                            }
                            parsed_id = Some(id_val);
                        } else {
                            return Err(syn::Error::new(lit.span(), "Failed to parse ID value"));
                        }
                    } else if ident == "default" {
                        parsed_default = true;
                    } else if ident == "skip_encode" {
                        parsed_skip_encode = true;
                    } else if ident == "skip_decode" {
                        parsed_skip_decode = true;
                    } else if ident == "skip_default" {
                        parsed_skip_default = true;
                    } else if ident == "rename" {
                        input.parse::<syn::Token![=]>()?;
                        let lit_str = input.parse::<syn::LitStr>()?;
                        parsed_rename = Some(lit_str.value());
                    } else {
                        return Err(syn::Error::new(
                            ident.span(),
                            format!("Unknown attribute: {}", ident),
                        ));
                    }

                    // Consume comma if present, otherwise end
                    if input.peek(syn::Token![,]) {
                        input.parse::<syn::Token![,]>()?;
                    }
                }

                Ok((
                    parsed_id,
                    parsed_default,
                    parsed_skip_encode,
                    parsed_skip_decode,
                    parsed_skip_default,
                    parsed_rename,
                ))
            });

            if let Ok((
                parsed_id,
                parsed_default,
                parsed_skip_encode,
                parsed_skip_decode,
                parsed_skip_default,
                parsed_rename,
            )) = parsed
            {
                if let Some(id_val) = parsed_id {
                    id = Some(id_val);
                }
                default = default || parsed_default;
                skip_encode = skip_encode || parsed_skip_encode;
                skip_decode = skip_decode || parsed_skip_decode;
                skip_default = skip_default || parsed_skip_default;
                if let Some(rename_val) = parsed_rename {
                    rename = Some(rename_val);
                }
            } else {
                eprintln!(
                    "Warning: #[senax(...)] attribute for field '{}' is not in the correct format.",
                    field_name
                );
            }
        }
    }

    // ID calculation: Use explicit ID if provided, otherwise calculate CRC64 from rename or field name
    let calculated_id = id.unwrap_or_else(|| {
        let name_for_id = if let Some(ref rename_val) = rename {
            rename_val.as_str()
        } else {
            field_name
        };
        calculate_id_from_name(name_for_id)
    });

    FieldAttributes {
        id: calculated_id,
        default,
        skip_encode,
        skip_decode,
        skip_default,
        rename,
    }
}

/// Check if a type is `Option<T>`
///
/// This helper function determines whether a given type is wrapped in an `Option`.
fn is_option_type(ty: &Type) -> bool {
    if let Type::Path(type_path) = ty {
        type_path
            .path
            .segments
            .last()
            .is_some_and(|seg| seg.ident == "Option")
    } else {
        false
    }
}

/// Extract the inner type `T` from `Option<T>`
///
/// This helper function extracts the wrapped type from an `Option` type.
/// Returns `None` if the type is not an `Option`.
fn extract_inner_type_from_option(ty: &Type) -> Option<&Type> {
    if let Type::Path(type_path) = ty {
        if type_path
            .path
            .segments
            .last()
            .is_some_and(|seg| seg.ident == "Option")
        {
            if let PathArguments::AngleBracketed(args) =
                &type_path.path.segments.last().unwrap().arguments
            {
                if let Some(GenericArgument::Type(inner_ty)) = args.args.first() {
                    return Some(inner_ty);
                }
            }
        }
    }
    None
}

/// Derive macro for implementing the `Encode` trait
///
/// This procedural macro automatically generates an implementation of the `Encode` trait
/// for structs and enums. It supports various field attributes for customizing the
/// encoding behavior.
///
/// # Supported Attributes
///
/// ## Container-level attributes:
/// * `#[senax(disable_encode)]` - Generate stub implementation (unimplemented!() only) for Encode/Decode
///
/// ## Field-level attributes:
/// * `#[senax(id=N)]` - Set explicit field/variant ID
/// * `#[senax(skip_encode)]` - Skip field during encoding
/// * `#[senax(rename="name")]` - Use alternative name for ID calculation
///
/// # Examples
///
/// ```rust
/// #[derive(Encode)]
/// struct MyStruct {
///     #[senax(id=1)]
///     field1: i32,
///     #[senax(skip_encode)]
///     field2: String,
/// }
///
/// // Stub implementation for faster compilation during development
/// #[derive(Encode, Decode)]
/// #[senax(disable_encode)]
/// struct UnfinishedStruct {
///     #[senax(id=1)]
///     field1: i32,
/// }
/// ```
#[proc_macro_derive(Encode, attributes(senax))]
pub fn derive_encode(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let name = &input.ident;
    let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();

    // Check for container-level disable_encode attribute
    let container_attrs = get_container_attributes(&input.attrs);
    if container_attrs.disable_encode {
        return TokenStream::from(quote! {
            impl #impl_generics senax_encoder::Encoder for #name #ty_generics #where_clause {
                fn encode(&self, _writer: &mut bytes::BytesMut) -> senax_encoder::Result<()> {
                    unimplemented!("Encode trait is disabled for {}", stringify!(#name))
                }

                fn is_default(&self) -> bool {
                    unimplemented!("Encode trait is disabled for {}", stringify!(#name))
                }
            }
        });
    }

    let mut default_variant_checks = Vec::new();

    let encode_fields = match &input.data {
        Data::Struct(s) => match &s.fields {
            Fields::Named(fields) => {
                let mut field_encode = Vec::new();
                let mut used_ids_struct = HashSet::new();
                for f in &fields.named {
                    let field_name_str = f.ident.as_ref().unwrap().to_string();
                    let field_attrs = get_field_attributes(&f.attrs, &field_name_str);

                    // Skip fields marked with skip_encode
                    if field_attrs.skip_encode {
                        continue;
                    }

                    if !used_ids_struct.insert(field_attrs.id) {
                        panic!("Field ID (0x{:016X}) is duplicated for struct '{}'. Please specify a different ID for field '{}' using #[senax(id=...)].", field_attrs.id, name, field_name_str);
                    }

                    let field_ident = &f.ident;
                    let ty = &f.ty;
                    let is_option = is_option_type(ty);
                    let field_id = field_attrs.id;

                    if is_option {
                        field_encode.push(quote! {
                            if let Some(val) = &self.#field_ident {
                                senax_encoder::core::write_field_id_optimized(writer, #field_id)?;
                                senax_encoder::Encoder::encode(&val, writer)?;
                            }
                        });
                    } else if field_attrs.skip_default {
                        // For skip_default fields, check if the value is default before encoding
                        field_encode.push(quote! {
                            if senax_encoder::Encoder::is_default(&self.#field_ident) == false {
                                senax_encoder::core::write_field_id_optimized(writer, #field_id)?;
                                senax_encoder::Encoder::encode(&self.#field_ident, writer)?;
                            }
                        });
                    } else {
                        field_encode.push(quote! {
                            senax_encoder::core::write_field_id_optimized(writer, #field_id)?;
                            senax_encoder::Encoder::encode(&self.#field_ident, writer)?;
                        });
                    }
                }
                quote! {
                    writer.put_u8(senax_encoder::core::TAG_STRUCT_NAMED);
                    #(#field_encode)*
                    senax_encoder::core::write_field_id_optimized(writer, 0)?;
                }
            }
            Fields::Unnamed(fields) => {
                let field_count = fields.unnamed.len();
                let field_encode = fields.unnamed.iter().enumerate().map(|(i, _)| {
                    let index = syn::Index::from(i);
                    quote! {
                        senax_encoder::Encoder::encode(&self.#index, writer)?;
                    }
                });
                quote! {
                    writer.put_u8(senax_encoder::core::TAG_STRUCT_UNNAMED);
                    let count: usize = #field_count;
                    senax_encoder::Encoder::encode(&count, writer)?;
                    #(#field_encode)*
                }
            }
            Fields::Unit => quote! {
                writer.put_u8(senax_encoder::core::TAG_STRUCT_UNIT);
            },
        },
        Data::Enum(e) => {
            let mut variant_encode = Vec::new();
            let mut used_ids_enum = HashSet::new();

            for v in &e.variants {
                let variant_name_str = v.ident.to_string();
                let variant_attrs = get_field_attributes(&v.attrs, &variant_name_str);
                let variant_id = variant_attrs.id;
                let is_default_variant = has_default_attribute(&v.attrs);

                if !used_ids_enum.insert(variant_id) {
                    panic!("Variant ID (0x{:016X}) is duplicated for enum '{}'. Please specify a different ID for variant '{}' using #[senax(id=...)].", variant_id, name, variant_name_str);
                }

                let variant_ident = &v.ident;

                // Generate is_default check for this variant if it has #[default] attribute
                if is_default_variant {
                    match &v.fields {
                        Fields::Named(fields) => {
                            let field_idents: Vec<_> = fields
                                .named
                                .iter()
                                .map(|f| f.ident.as_ref().unwrap())
                                .collect();
                            let field_default_checks: Vec<_> = field_idents
                                .iter()
                                .map(|ident| {
                                    quote! { senax_encoder::Encoder::is_default(#ident) }
                                })
                                .collect();

                            if field_default_checks.is_empty() {
                                default_variant_checks.push(quote! {
                                    #name::#variant_ident { .. } => true,
                                });
                            } else {
                                default_variant_checks.push(quote! {
                                    #name::#variant_ident { #(#field_idents),* } => {
                                        #(#field_default_checks)&&*
                                    },
                                });
                            }
                        }
                        Fields::Unnamed(fields) => {
                            let field_count = fields.unnamed.len();
                            let field_bindings: Vec<_> = (0..field_count)
                                .map(|i| Ident::new(&format!("field{}", i), Span::call_site()))
                                .collect();
                            let field_default_checks: Vec<_> = field_bindings
                                .iter()
                                .map(|binding| {
                                    quote! { senax_encoder::Encoder::is_default(#binding) }
                                })
                                .collect();

                            if field_default_checks.is_empty() {
                                default_variant_checks.push(quote! {
                                    #name::#variant_ident(..) => true,
                                });
                            } else {
                                default_variant_checks.push(quote! {
                                    #name::#variant_ident(#(#field_bindings),*) => {
                                        #(#field_default_checks)&&*
                                    },
                                });
                            }
                        }
                        Fields::Unit => {
                            default_variant_checks.push(quote! {
                                #name::#variant_ident => true,
                            });
                        }
                    }
                }

                match &v.fields {
                    Fields::Named(fields) => {
                        let field_idents: Vec<_> = fields
                            .named
                            .iter()
                            .map(|f| f.ident.as_ref().unwrap())
                            .collect();
                        let mut field_encode = Vec::new();
                        let mut used_ids_struct = HashSet::new();
                        for f in &fields.named {
                            let field_name_str = f.ident.as_ref().unwrap().to_string();
                            let field_attrs = get_field_attributes(&f.attrs, &field_name_str);

                            // Skip fields marked with skip_encode
                            if field_attrs.skip_encode {
                                continue;
                            }

                            if !used_ids_struct.insert(field_attrs.id) {
                                panic!("Field ID (0x{:016X}) is duplicated for enum variant '{}'. Please specify a different ID for field '{}' using #[senax(id=...)].", field_attrs.id, variant_ident, field_name_str);
                            }
                            let field_ident = &f.ident;
                            let ty = &f.ty;
                            let is_option = is_option_type(ty);
                            let field_id = field_attrs.id;
                            if is_option {
                                field_encode.push(quote! {
                                    if let Some(val) = #field_ident {
                                        senax_encoder::core::write_field_id_optimized(writer, #field_id)?;
                                        senax_encoder::Encoder::encode(&val, writer)?;
                                    }
                                });
                            } else if field_attrs.skip_default {
                                // For skip_default fields, check if the value is default before encoding
                                field_encode.push(quote! {
                                    if senax_encoder::Encoder::is_default(#field_ident) == false {
                                        senax_encoder::core::write_field_id_optimized(writer, #field_id)?;
                                        senax_encoder::Encoder::encode(&#field_ident, writer)?;
                                    }
                                });
                            } else {
                                field_encode.push(quote! {
                                    senax_encoder::core::write_field_id_optimized(writer, #field_id)?;
                                    senax_encoder::Encoder::encode(&#field_ident, writer)?;
                                });
                            }
                        }
                        variant_encode.push(quote! {
                            #name::#variant_ident { #(#field_idents),* } => {
                                writer.put_u8(senax_encoder::core::TAG_ENUM_NAMED);
                                senax_encoder::core::write_field_id_optimized(writer, #variant_id)?;
                                #(#field_encode)*
                                senax_encoder::core::write_field_id_optimized(writer, 0)?;
                            }
                        });
                    }
                    Fields::Unnamed(fields) => {
                        let field_count = fields.unnamed.len();
                        let field_bindings: Vec<_> = (0..field_count)
                            .map(|i| Ident::new(&format!("field{}", i), Span::call_site()))
                            .collect();
                        let field_bindings_ref = &field_bindings;
                        variant_encode.push(quote! {
                            #name::#variant_ident( #(#field_bindings_ref),* ) => {
                                writer.put_u8(senax_encoder::core::TAG_ENUM_UNNAMED);
                                senax_encoder::core::write_field_id_optimized(writer, #variant_id)?;
                                let count: usize = #field_count;
                                senax_encoder::Encoder::encode(&count, writer)?;
                                #(
                                    senax_encoder::Encoder::encode(&#field_bindings_ref, writer)?;
                                )*
                            }
                        });
                    }
                    Fields::Unit => {
                        variant_encode.push(quote! {
                            #name::#variant_ident => {
                                writer.put_u8(senax_encoder::core::TAG_ENUM);
                                senax_encoder::core::write_field_id_optimized(writer, #variant_id)?;
                            }
                        });
                    }
                }
            }
            quote! {
                match self {
                    #(#variant_encode)*
                }
            }
        }
        Data::Union(_) => unimplemented!("Unions are not supported"),
    };

    let is_default_impl = match &input.data {
        Data::Enum(_) => {
            if default_variant_checks.is_empty() {
                quote! { false }
            } else {
                quote! {
                    match self {
                        #(#default_variant_checks)*
                        _ => false,
                    }
                }
            }
        }
        _ => quote! { false },
    };

    let encode_method = quote! {
        fn encode(&self, writer: &mut bytes::BytesMut) -> senax_encoder::Result<()> {
            use bytes::{Buf, BufMut};
            #encode_fields
            Ok(())
        }

        fn is_default(&self) -> bool {
            #is_default_impl
        }
    };

    TokenStream::from(quote! {
        impl #impl_generics senax_encoder::Encoder for #name #ty_generics #where_clause {
            #encode_method
        }
    })
}

/// Derive macro for implementing the `Decode` trait
///
/// This procedural macro automatically generates an implementation of the `Decode` trait
/// for structs and enums. It supports various field attributes for customizing the
/// decoding behavior and provides forward/backward compatibility.
///
/// # Supported Attributes
///
/// ## Container-level attributes:
/// * `#[senax(disable_encode)]` - Generate stub implementation (unimplemented!() only) for Encode/Decode
///
/// ## Field-level attributes:
/// * `#[senax(id=N)]` - Set explicit field/variant ID
/// * `#[senax(default)]` - Use default value if field is missing
/// * `#[senax(skip_decode)]` - Skip field during decoding (use default value)
/// * `#[senax(skip_default)]` - Use default value if field is missing (same as default for decode)
/// * `#[senax(rename="name")]` - Use alternative name for ID calculation
///
/// # Examples
///
/// ```rust
/// #[derive(Decode)]
/// struct MyStruct {
///     #[senax(id=1)]
///     field1: i32,
///     #[senax(default)]
///     field2: String,
///     #[senax(skip_decode)]
///     field3: bool,
/// }
///
/// // Stub implementation for faster compilation during development
/// #[derive(Encode, Decode)]
/// #[senax(disable_encode)]
/// struct UnfinishedStruct {
///     #[senax(id=1)]
///     field1: i32,
/// }
/// ```
#[proc_macro_derive(Decode, attributes(senax))]
pub fn derive_decode(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let name = &input.ident;
    let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();

    // Check for container-level disable_encode attribute
    let container_attrs = get_container_attributes(&input.attrs);
    if container_attrs.disable_encode {
        return TokenStream::from(quote! {
            impl #impl_generics senax_encoder::Decoder for #name #ty_generics #where_clause {
                fn decode(_reader: &mut bytes::Bytes) -> senax_encoder::Result<Self> {
                    unimplemented!("Decode trait is disabled for {}", stringify!(#name))
                }
            }
        });
    }

    let decode_fields = match &input.data {
        Data::Struct(s) => match &s.fields {
            Fields::Named(fields) => {
                let mut field_idents = Vec::new();
                let mut field_original_types = Vec::new();
                let mut field_ids_for_match = Vec::new();
                let mut field_is_option_flags = Vec::new();
                let mut field_attrs_list = Vec::new();
                let mut used_ids_struct_decode = HashMap::new();

                for f in &fields.named {
                    let field_name_str = f.ident.as_ref().unwrap().to_string();
                    let field_attrs = get_field_attributes(&f.attrs, &field_name_str);

                    if let Some(dup_field_name) =
                        used_ids_struct_decode.insert(field_attrs.id, field_name_str.clone())
                    {
                        panic!("Field ID (0x{:016X}) is duplicated for struct '{}'. Please specify a different ID for field '{}' and '{}' using #[senax(id=...)].", 
                              field_attrs.id, name, dup_field_name, field_name_str);
                    }

                    field_idents.push(f.ident.as_ref().unwrap().clone());
                    field_original_types.push(f.ty.clone());
                    field_ids_for_match.push(field_attrs.id);
                    field_is_option_flags.push(is_option_type(&f.ty));
                    field_attrs_list.push(field_attrs);
                }

                let field_value_definitions = field_idents
                    .iter()
                    .zip(field_original_types.iter())
                    .zip(field_attrs_list.iter())
                    .filter_map(|((ident, original_ty), attrs)| {
                        if attrs.skip_decode {
                            // Fields marked with skip_decode don't store values
                            None
                        } else if is_option_type(original_ty) {
                            Some(quote! { #ident: #original_ty, })
                        } else {
                            Some(quote! { #ident: Option<#original_ty>, })
                        }
                    });

                let match_arms = field_idents
                    .iter()
                    .zip(field_original_types.iter())
                    .zip(field_ids_for_match.iter())
                    .zip(field_attrs_list.iter())
                    .filter_map(|(((ident, original_ty), id_val), attrs)| {
                        if attrs.skip_decode {
                            // Fields marked with skip_decode don't generate match arms (values are skipped)
                            None
                        } else if is_option_type(original_ty) {
                            let inner_ty = extract_inner_type_from_option(original_ty)
                                .unwrap_or_else(|| {
                                    panic!(
                                        "Failed to extract inner type from Option for field {}",
                                        ident
                                    )
                                });
                            Some(quote! {
                                x if x == #id_val => {
                                    field_values.#ident = Some(<#inner_ty as senax_encoder::Decoder>::decode(reader)?);
                                }
                            })
                        } else {
                            Some(quote! {
                                x if x == #id_val => {
                                    field_values.#ident = Some(<#original_ty as senax_encoder::Decoder>::decode(reader)?);
                                }
                            })
                        }
                    });

                let struct_assignments = field_idents
                    .iter()
                    .zip(field_is_option_flags.iter())
                    .zip(field_attrs_list.iter())
                    .map(|((ident, is_opt_flag), attrs)| {
                        if attrs.skip_decode {
                            // Fields marked with skip_decode use default values
                            quote! {
                                #ident: Default::default(),
                            }
                        } else if *is_opt_flag {
                            quote! {
                                #ident: field_values.#ident,
                            }
                        } else if attrs.default || attrs.skip_default {
                            // Fields marked with default or skip_default use default value if missing
                            quote! {
                                #ident: field_values.#ident.unwrap_or_default(),
                            }
                        } else {
                            quote! {
                                #ident: field_values.#ident.ok_or_else(||
                                    senax_encoder::EncoderError::StructDecode(
                                        senax_encoder::StructDecodeError::MissingRequiredField {
                                            field: stringify!(#ident),
                                            struct_name: stringify!(#name),
                                        }
                                    )
                                )?,
                            }
                        }
                    });

                quote! {
                    if reader.remaining() == 0 {
                        return Err(senax_encoder::EncoderError::InsufficientData);
                    }
                    let tag = reader.get_u8();
                    if tag != senax_encoder::core::TAG_STRUCT_NAMED {
                        return Err(senax_encoder::EncoderError::StructDecode(
                            senax_encoder::StructDecodeError::InvalidTag {
                                expected: senax_encoder::core::TAG_STRUCT_NAMED,
                                actual: tag,
                            }
                        ));
                    }

                    #[derive(Default)]
                    struct FieldValues {
                        #( #field_value_definitions )*
                    }

                    let mut field_values = FieldValues::default();

                    loop {
                        let field_id = senax_encoder::core::read_field_id_optimized(reader)?;
                        if field_id == 0 {
                            break;
                        }
                        match field_id {
                            #( #match_arms )*
                            _unknown_id => { senax_encoder::core::skip_value(reader)?; }
                        }
                    }

                    Ok(#name {
                        #( #struct_assignments )*
                    })
                }
            }
            Fields::Unnamed(fields) => {
                let field_count = fields.unnamed.len();
                let field_decode = fields.unnamed.iter().map(|f| {
                    let field_ty = &f.ty;
                    quote! {
                        <#field_ty as senax_encoder::Decoder>::decode(reader)?
                    }
                });
                quote! {
                    if reader.remaining() == 0 {
                        return Err(senax_encoder::EncoderError::InsufficientData);
                    }
                    let tag = reader.get_u8();
                    if tag != senax_encoder::core::TAG_STRUCT_UNNAMED {
                        return Err(senax_encoder::EncoderError::StructDecode(
                            senax_encoder::StructDecodeError::InvalidTag {
                                expected: senax_encoder::core::TAG_STRUCT_UNNAMED,
                                actual: tag,
                            }
                        ));
                    }
                    let count = <usize as senax_encoder::Decoder>::decode(reader)?;
                    if count != #field_count {
                        return Err(senax_encoder::EncoderError::StructDecode(
                            senax_encoder::StructDecodeError::FieldCountMismatch {
                                struct_name: stringify!(#name),
                                expected: #field_count,
                                actual: count,
                            }
                        ));
                    }
                    Ok(#name(
                        #(#field_decode),*
                    ))
                }
            }
            Fields::Unit => quote! {
                if reader.remaining() == 0 {
                    return Err(senax_encoder::EncoderError::InsufficientData);
                }
                let tag = reader.get_u8();
                if tag != senax_encoder::core::TAG_STRUCT_UNIT {
                    return Err(senax_encoder::EncoderError::StructDecode(
                        senax_encoder::StructDecodeError::InvalidTag {
                            expected: senax_encoder::core::TAG_STRUCT_UNIT,
                            actual: tag,
                        }
                    ));
                }
                Ok(#name)
            },
        },
        Data::Enum(e) => {
            let mut unit_variant_arms = Vec::new();
            let mut named_variant_arms = Vec::new();
            let mut unnamed_variant_arms = Vec::new();
            let mut used_ids_enum_decode = HashMap::new();

            for v in &e.variants {
                let variant_name_str = v.ident.to_string();
                let variant_attrs = get_field_attributes(&v.attrs, &variant_name_str);
                let variant_id = variant_attrs.id;

                if let Some(dup_variant) =
                    used_ids_enum_decode.insert(variant_id, variant_name_str.clone())
                {
                    panic!("Variant ID (0x{:016X}) is duplicated for enum '{}'. Please specify a different ID for variant '{}' and '{}' using #[senax(id=...)].", 
                          variant_id, name, dup_variant, variant_name_str);
                }

                let variant_ident = &v.ident;
                match &v.fields {
                    Fields::Named(fields) => {
                        let field_idents: Vec<_> = fields
                            .named
                            .iter()
                            .map(|f| f.ident.as_ref().unwrap().clone())
                            .collect();
                        let field_types: Vec<_> =
                            fields.named.iter().map(|f| f.ty.clone()).collect();
                        let field_attrs_list: Vec<_> = fields
                            .named
                            .iter()
                            .map(|f| {
                                get_field_attributes(
                                    &f.attrs,
                                    &f.ident.as_ref().unwrap().to_string(),
                                )
                            })
                            .collect();

                        let mut field_value_definitions_enum = Vec::new();
                        let mut match_arms_enum_named = Vec::new();
                        let mut struct_assignments_enum_named = Vec::new();

                        for (ident, ty, attrs) in izip!(
                            field_idents.iter(),
                            field_types.iter(),
                            field_attrs_list.iter()
                        ) {
                            if attrs.skip_decode {
                                // Fields marked with skip_decode don't store values
                            } else if is_option_type(ty) {
                                field_value_definitions_enum.push(quote! { #ident: #ty, });
                            } else {
                                field_value_definitions_enum.push(quote! { #ident: Option<#ty>, });
                            }

                            if attrs.skip_decode {
                                // Fields marked with skip_decode don't generate match arms
                            } else if is_option_type(ty) {
                                let inner_ty = extract_inner_type_from_option(ty).unwrap();
                                let field_id = attrs.id;
                                match_arms_enum_named.push(quote! {
                                    x if x == #field_id => { field_values.#ident = Some(<#inner_ty as senax_encoder::Decoder>::decode(reader)?); }
                                });
                            } else {
                                let field_id = attrs.id;
                                match_arms_enum_named.push(quote! {
                                    x if x == #field_id => { field_values.#ident = Some(<#ty as senax_encoder::Decoder>::decode(reader)?); }
                                });
                            }

                            if attrs.skip_decode {
                                // Fields marked with skip_decode use default values
                                struct_assignments_enum_named
                                    .push(quote! { #ident: Default::default(), });
                            } else if is_option_type(ty) {
                                struct_assignments_enum_named
                                    .push(quote! { #ident: field_values.#ident, });
                            } else if attrs.default || attrs.skip_default {
                                // Fields marked with default or skip_default use default value if missing
                                struct_assignments_enum_named.push(quote! {
                                    #ident: field_values.#ident.unwrap_or_default(),
                                });
                            } else {
                                struct_assignments_enum_named.push(quote! {
                                    #ident: field_values.#ident.ok_or_else(||
                                        senax_encoder::EncoderError::EnumDecode(
                                            senax_encoder::EnumDecodeError::MissingRequiredField {
                                                field: stringify!(#ident),
                                                enum_name: stringify!(#name),
                                                variant_name: stringify!(#variant_ident),
                                            }
                                        )
                                    )?,
                                });
                            }
                        }

                        named_variant_arms.push(quote! {
                            x if x == #variant_id => {
                                #[derive(Default)]
                                struct FieldValues { #(#field_value_definitions_enum)* }
                                let mut field_values = FieldValues::default();
                                loop {
                                    let field_id = {
                                        if reader.remaining() == 0 { break; }
                                        let id = senax_encoder::core::read_field_id_optimized(reader)?;
                                        if id == 0 { break; }
                                        id
                                    };
                                    match field_id {
                                        #(#match_arms_enum_named)*
                                        _unknown_id => { senax_encoder::core::skip_value(reader)?; }
                                    }
                                }
                                Ok(#name::#variant_ident { #(#struct_assignments_enum_named)* })
                            }
                        });
                    }
                    Fields::Unnamed(fields) => {
                        let field_types: Vec<_> = fields.unnamed.iter().map(|f| &f.ty).collect();
                        let field_count = field_types.len();
                        unnamed_variant_arms.push(quote! {
                            x if x == #variant_id => {
                                let count = <usize as senax_encoder::Decoder>::decode(reader)?;
                                if count != #field_count {
                                    return Err(senax_encoder::EncoderError::EnumDecode(
                                        senax_encoder::EnumDecodeError::FieldCountMismatch {
                                            enum_name: stringify!(#name),
                                            variant_name: stringify!(#variant_ident),
                                            expected: #field_count,
                                            actual: count,
                                        }
                                    ));
                                }
                                Ok(#name::#variant_ident(
                                    #(
                                        <#field_types as senax_encoder::Decoder>::decode(reader)?,
                                    )*
                                ))
                            }
                        });
                    }
                    Fields::Unit => {
                        unit_variant_arms.push(quote! {
                            x if x == #variant_id => {
                                Ok(#name::#variant_ident)
                            }
                        });
                    }
                }
            }
            quote! {
                if reader.remaining() == 0 {
                    return Err(senax_encoder::EncoderError::InsufficientData);
                }
                let tag = reader.get_u8();
                match tag {
                    senax_encoder::core::TAG_ENUM => {
                        let variant_id = senax_encoder::core::read_field_id_optimized(reader)?;
                        match variant_id {
                            #(#unit_variant_arms)*
                            _ => Err(senax_encoder::EncoderError::EnumDecode(
                                senax_encoder::EnumDecodeError::UnknownVariantId {
                                    variant_id,
                                    enum_name: stringify!(#name),
                                }
                            ))
                        }
                    }
                    senax_encoder::core::TAG_ENUM_NAMED => {
                        let variant_id = senax_encoder::core::read_field_id_optimized(reader)?;
                        match variant_id {
                            #(#named_variant_arms)*
                            _ => Err(senax_encoder::EncoderError::EnumDecode(
                                senax_encoder::EnumDecodeError::UnknownVariantId {
                                    variant_id,
                                    enum_name: stringify!(#name),
                                }
                            ))
                        }
                    }
                    senax_encoder::core::TAG_ENUM_UNNAMED => {
                        let variant_id = senax_encoder::core::read_field_id_optimized(reader)?;
                        match variant_id {
                             #(#unnamed_variant_arms)*
                            _ => Err(senax_encoder::EncoderError::EnumDecode(
                                senax_encoder::EnumDecodeError::UnknownVariantId {
                                    variant_id,
                                    enum_name: stringify!(#name),
                                }
                            ))
                        }
                    }
                    unknown_tag => Err(senax_encoder::EncoderError::EnumDecode(
                        senax_encoder::EnumDecodeError::UnknownTag {
                            tag: unknown_tag,
                            enum_name: stringify!(#name),
                        }
                    ))
                }
            }
        }
        Data::Union(_) => unimplemented!("Unions are not supported"),
    };

    let decode_method = quote! {
        fn decode(reader: &mut bytes::Bytes) -> senax_encoder::Result<Self> {
            use bytes::{Buf, BufMut};
            #decode_fields
        }
    };

    TokenStream::from(quote! {
        impl #impl_generics senax_encoder::Decoder for #name #ty_generics #where_clause {
            #decode_method
        }
    })
}

/// Derive macro for implementing the `Pack` trait (Packer only)
///
/// This procedural macro automatically generates an implementation of the `Packer` trait
/// for structs and enums. It provides compact serialization without field IDs for structs.
///
/// # Supported Attributes
///
/// ## Container-level attributes:
/// * `#[senax(disable_pack)]` - Generate stub implementation (unimplemented!() only) for Pack/Unpack
///
/// # Examples
///
/// ```rust
/// #[derive(Pack)]
/// struct MyStruct {
///     field1: i32,
///     field2: String,
/// }
///
/// // Stub implementation for faster compilation during development
/// #[derive(Pack, Unpack)]
/// #[senax(disable_pack)]
/// struct UnfinishedStruct {
///     field1: i32,
/// }
/// ```
#[proc_macro_derive(Pack, attributes(senax))]
pub fn derive_pack(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let name = &input.ident;
    let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();

    // Check for container-level disable_pack attribute
    let container_attrs = get_container_attributes(&input.attrs);
    if container_attrs.disable_pack {
        return TokenStream::from(quote! {
            impl #impl_generics senax_encoder::Packer for #name #ty_generics #where_clause {
                fn pack(&self, _writer: &mut bytes::BytesMut) -> senax_encoder::Result<()> {
                    unimplemented!("Pack trait is disabled for {}", stringify!(#name))
                }
            }
        });
    }

    // Generate structure information and CRC64 hash for pack format
    let structure_info = generate_structure_info(&input);
    let structure_hash = CRC64.checksum(structure_info.as_bytes());

    // Generate pack implementation for structs and enums (no field IDs for struct fields)
    let pack_fields = match &input.data {
        Data::Struct(s) => match &s.fields {
            Fields::Named(fields) => {
                let field_encode = fields.named.iter().map(|f| {
                    let field_ident = &f.ident;
                    quote! {
                        senax_encoder::Packer::pack(&self.#field_ident, writer)?;
                    }
                });
                quote! {
                    // Write structure hash first for named structs
                    writer.put_u64_le(#structure_hash);
                    #(#field_encode)*
                }
            }
            Fields::Unnamed(fields) => {
                let field_count = fields.unnamed.len();
                let field_encode = fields.unnamed.iter().enumerate().map(|(i, _)| {
                    let index = syn::Index::from(i);
                    quote! {
                        senax_encoder::Packer::pack(&self.#index, writer)?;
                    }
                });
                quote! {
                    // Write field count for unnamed structs
                    let count: usize = #field_count;
                    senax_encoder::Encoder::encode(&count, writer)?;
                    #(#field_encode)*
                }
            }
            Fields::Unit => quote! {
                // Unit structs don't need any additional data
            },
        },
        Data::Enum(e) => {
            let mut variant_pack = Vec::new();
            let mut used_ids_enum_pack = HashSet::new();

            for v in &e.variants {
                let variant_name_str = v.ident.to_string();
                let variant_attrs = get_field_attributes(&v.attrs, &variant_name_str);
                let variant_id = variant_attrs.id;

                if !used_ids_enum_pack.insert(variant_id) {
                    panic!("Variant ID (0x{:016X}) is duplicated for enum '{}'. Please specify a different ID for variant '{}' using #[senax(id=...)].", variant_id, name, variant_name_str);
                }

                let variant_ident = &v.ident;

                match &v.fields {
                    Fields::Named(fields) => {
                        let field_idents: Vec<_> = fields
                            .named
                            .iter()
                            .map(|f| f.ident.as_ref().unwrap())
                            .collect();
                        // For pack, encode fields in order without field IDs
                        let field_pack = field_idents.iter().map(|field_ident| {
                            quote! {
                                senax_encoder::Packer::pack(#field_ident, writer)?;
                            }
                        });
                        variant_pack.push(quote! {
                            #name::#variant_ident { #(#field_idents),* } => {
                                // Write variant ID first, then structure hash for named enums
                                senax_encoder::core::write_field_id_optimized(writer, #variant_id)?;
                                writer.put_u64_le(#structure_hash);
                                #(#field_pack)*
                            }
                        });
                    }
                    Fields::Unnamed(fields) => {
                        let field_count = fields.unnamed.len();
                        let field_bindings: Vec<_> = (0..field_count)
                            .map(|i| Ident::new(&format!("field{}", i), Span::call_site()))
                            .collect();
                        let field_bindings_ref = &field_bindings;
                        variant_pack.push(quote! {
                            #name::#variant_ident( #(#field_bindings_ref),* ) => {
                                // Write variant ID first, then field count for unnamed enums
                                senax_encoder::core::write_field_id_optimized(writer, #variant_id)?;
                                let count: usize = #field_count;
                                senax_encoder::Encoder::encode(&count, writer)?;
                                #(
                                    senax_encoder::Packer::pack(&#field_bindings_ref, writer)?;
                                )*
                            }
                        });
                    }
                    Fields::Unit => {
                        variant_pack.push(quote! {
                            #name::#variant_ident => {
                                // Unit enums only need variant ID
                                senax_encoder::core::write_field_id_optimized(writer, #variant_id)?;
                            }
                        });
                    }
                }
            }
            quote! {
                match self {
                    #(#variant_pack)*
                }
            }
        }
        Data::Union(_) => unimplemented!("Unions are not supported"),
    };

    let pack_method = quote! {
        fn pack(&self, writer: &mut bytes::BytesMut) -> senax_encoder::Result<()> {
            use bytes::{Buf, BufMut};
            #pack_fields
            Ok(())
        }
    };

    TokenStream::from(quote! {
        impl #impl_generics senax_encoder::Packer for #name #ty_generics #where_clause {
            #pack_method
        }
    })
}

/// Derive macro for implementing the `Unpack` trait (Unpacker only)
///
/// This procedural macro automatically generates an implementation of the `Unpacker` trait
/// for structs and enums. It provides compact deserialization that matches the Pack format.
///
/// # Supported Attributes
///
/// ## Container-level attributes:
/// * `#[senax(disable_pack)]` - Generate stub implementation (unimplemented!() only) for Pack/Unpack
///
/// # Examples
///
/// ```rust
/// #[derive(Unpack)]
/// struct MyStruct {
///     field1: i32,
///     field2: String,
/// }
///
/// // Stub implementation for faster compilation during development
/// #[derive(Pack, Unpack)]
/// #[senax(disable_pack)]
/// struct UnfinishedStruct {
///     field1: i32,
/// }
/// ```
#[proc_macro_derive(Unpack, attributes(senax))]
pub fn derive_unpack(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let name = &input.ident;
    let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();

    // Check for container-level disable_pack attribute
    let container_attrs = get_container_attributes(&input.attrs);
    if container_attrs.disable_pack {
        return TokenStream::from(quote! {
            impl #impl_generics senax_encoder::Unpacker for #name #ty_generics #where_clause {
                fn unpack(_reader: &mut bytes::Bytes) -> senax_encoder::Result<Self> {
                    unimplemented!("Unpack trait is disabled for {}", stringify!(#name))
                }
            }
        });
    }

    // Generate structure information and CRC64 hash for pack format validation
    let structure_info = generate_structure_info(&input);
    let structure_hash = CRC64.checksum(structure_info.as_bytes());

    // Generate unpack implementation for structs and enums (no field IDs for struct fields)
    let unpack_fields = match &input.data {
        Data::Struct(s) => match &s.fields {
            Fields::Named(fields) => {
                let field_assignments = fields.named.iter().map(|f| {
                    let field_ident = &f.ident;
                    let field_ty = &f.ty;
                    quote! {
                        #field_ident: <#field_ty as senax_encoder::Unpacker>::unpack(reader)?,
                    }
                });
                quote! {
                    // Read and validate structure hash for named structs
                    if reader.remaining() < 8 {
                        return Err(senax_encoder::EncoderError::InsufficientData);
                    }
                    let received_hash = reader.get_u64_le();
                    if received_hash != #structure_hash {
                        return Err(senax_encoder::EncoderError::StructDecode(
                            senax_encoder::StructDecodeError::StructureHashMismatch {
                                struct_name: stringify!(#name),
                                expected: #structure_hash,
                                actual: received_hash,
                            }
                        ));
                    }

                    Ok(#name {
                        #(#field_assignments)*
                    })
                }
            }
            Fields::Unnamed(fields) => {
                let expected_field_count = fields.unnamed.len();
                let field_decode = fields.unnamed.iter().map(|f| {
                    let field_ty = &f.ty;
                    quote! {
                        <#field_ty as senax_encoder::Unpacker>::unpack(reader)?
                    }
                });
                quote! {
                    // Read and validate field count for unnamed structs
                    let field_count = <usize as senax_encoder::Decoder>::decode(reader)?;
                    if field_count != #expected_field_count {
                        return Err(senax_encoder::EncoderError::StructDecode(
                            senax_encoder::StructDecodeError::FieldCountMismatch {
                                struct_name: stringify!(#name),
                                expected: #expected_field_count,
                                actual: field_count,
                            }
                        ));
                    }

                    Ok(#name(
                        #(#field_decode),*
                    ))
                }
            }
            Fields::Unit => quote! {
                // Unit structs don't need any additional data
                Ok(#name)
            },
        },
        Data::Enum(e) => {
            let mut variant_unpack = Vec::new();
            let mut used_ids_enum_unpack = HashSet::new();

            for v in &e.variants {
                let variant_name_str = v.ident.to_string();
                let variant_attrs = get_field_attributes(&v.attrs, &variant_name_str);
                let variant_id = variant_attrs.id;

                if !used_ids_enum_unpack.insert(variant_id) {
                    panic!("Variant ID (0x{:016X}) is duplicated for enum '{}'. Please specify a different ID for variant '{}' using #[senax(id=...)].", variant_id, name, variant_name_str);
                }

                let variant_ident = &v.ident;
                match &v.fields {
                    Fields::Named(fields) => {
                        let field_idents: Vec<_> = fields
                            .named
                            .iter()
                            .map(|f| f.ident.as_ref().unwrap().clone())
                            .collect();
                        let field_types: Vec<_> =
                            fields.named.iter().map(|f| f.ty.clone()).collect();

                        // For unpack, decode fields in order without expecting field IDs
                        let field_assignments =
                            field_idents
                                .iter()
                                .zip(field_types.iter())
                                .map(|(ident, ty)| {
                                    quote! {
                                        #ident: <#ty as senax_encoder::Unpacker>::unpack(reader)?,
                                    }
                                });

                        variant_unpack.push(quote! {
                            x if x == #variant_id => {
                                // Read and validate structure hash for named variants
                                if reader.remaining() < 8 {
                                    return Err(senax_encoder::EncoderError::InsufficientData);
                                }
                                let received_hash = reader.get_u64_le();
                                if received_hash != #structure_hash {
                                    return Err(senax_encoder::EncoderError::EnumDecode(
                                        senax_encoder::EnumDecodeError::StructureHashMismatch {
                                            enum_name: stringify!(#name),
                                            variant_name: stringify!(#variant_ident),
                                            expected: #structure_hash,
                                            actual: received_hash,
                                        }
                                    ));
                                }
                                Ok(#name::#variant_ident { #(#field_assignments)* })
                            }
                        });
                    }
                    Fields::Unnamed(fields) => {
                        let field_types: Vec<_> = fields.unnamed.iter().map(|f| &f.ty).collect();
                        let expected_field_count = field_types.len();
                        variant_unpack.push(quote! {
                            x if x == #variant_id => {
                                // Read and validate field count for unnamed variants
                                let field_count = <usize as senax_encoder::Decoder>::decode(reader)?;
                                if field_count != #expected_field_count {
                                    return Err(senax_encoder::EncoderError::EnumDecode(
                                        senax_encoder::EnumDecodeError::FieldCountMismatch {
                                            enum_name: stringify!(#name),
                                            variant_name: stringify!(#variant_ident),
                                            expected: #expected_field_count,
                                            actual: field_count,
                                        }
                                    ));
                                }
                                Ok(#name::#variant_ident(
                                    #(
                                        <#field_types as senax_encoder::Unpacker>::unpack(reader)?,
                                    )*
                                ))
                            }
                        });
                    }
                    Fields::Unit => {
                        variant_unpack.push(quote! {
                            x if x == #variant_id => {
                                Ok(#name::#variant_ident)
                            }
                        });
                    }
                }
            }

            // Now we can support mixed variants since variant ID comes first
            quote! {
                let variant_id = senax_encoder::core::read_field_id_optimized(reader)?;
                match variant_id {
                    #(#variant_unpack)*
                    _ => Err(senax_encoder::EncoderError::EnumDecode(
                        senax_encoder::EnumDecodeError::UnknownVariantId {
                            variant_id,
                            enum_name: stringify!(#name),
                        }
                    ))
                }
            }
        }
        Data::Union(_) => unimplemented!("Unions are not supported"),
    };

    let unpack_method = quote! {
        fn unpack(reader: &mut bytes::Bytes) -> senax_encoder::Result<Self> {
            use bytes::{Buf, BufMut};
            #unpack_fields
        }
    };

    TokenStream::from(quote! {
        impl #impl_generics senax_encoder::Unpacker for #name #ty_generics #where_clause {
            #unpack_method
        }
    })
}