Skip to main content

binary_codec_derive/
lib.rs

1extern crate proc_macro;
2
3use quote::{format_ident, quote};
4use syn::{
5    Attribute, Data, DeriveInput, Fields, Lit, PathArguments, Type, parse_macro_input,
6    punctuated::Punctuated, token::Comma,
7};
8
9#[proc_macro_derive(
10    ToBytes,
11    attributes(
12        bits,
13        skip_bits,
14        dyn_int,
15        dyn_length,
16        key_dyn_length,
17        val_dyn_length,
18        toggles,
19        toggled_by,
20        toggled_by_variant,
21        length_for,
22        length_by,
23        variant_for,
24        variant_by,
25        multi_enum,
26        no_discriminator,
27        discriminator_bits,
28        codec_error,
29        codec_ser_error,
30        codec_de_error,
31    )
32)]
33pub fn generate_code_to_bytes(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
34    generate_code_binary_serializer(false, input)
35}
36
37#[proc_macro_derive(
38    FromBytes,
39    attributes(
40        bits,
41        skip_bits,
42        dyn_int,
43        key_dyn_length,
44        val_dyn_length,
45        dyn_length,
46        toggles,
47        toggled_by,
48        toggled_by_variant,
49        length_for,
50        length_by,
51        variant_for,
52        variant_by,
53        multi_enum,
54        no_discriminator,
55        discriminator_bits,
56        codec_error,
57        codec_ser_error,
58        codec_de_error,
59    )
60)]
61pub fn generate_code_from_bytes(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
62    generate_code_binary_serializer(true, input)
63}
64
65fn generate_code_binary_serializer(
66    read: bool,
67    input: proc_macro::TokenStream,
68) -> proc_macro::TokenStream {
69    // Parse code input (TokenStream) to AST
70    let ast = parse_macro_input!(input as DeriveInput);
71
72    match ast.data {
73        Data::Struct(ref data) => generate_struct_serializer(read, &ast, data),
74        Data::Enum(ref data) => generate_enum_serializer(read, &ast, data),
75        _ => panic!("ToBytes can only be used on structs or enums"),
76    }
77}
78
79fn generate_field_serializer(
80    read: bool,
81    field_ident: &proc_macro2::Ident,
82    field_type: &syn::Type,
83    field: &syn::Field,
84    is_enum: bool,
85) -> proc_macro2::TokenStream {
86    let single_ident_type_name = if let Type::Path(path) = field_type {
87        if path.path.segments.len() == 1 {
88            Some(path.path.segments[0].ident.to_string())
89        } else {
90            None
91        }
92    } else {
93        None
94    };
95
96    let mut toggle_key = None;
97    // allow multiple variant_for / length_for entries
98    let mut variant_keys: Vec<String> = Vec::new();
99    let mut length_keys: Vec<String> = Vec::new();
100    let mut toggled_by_variant = None;
101    let mut toggled_by = None;
102    let mut variant_by = None;
103    let mut length_by = None;
104    let mut is_dynamic_int = false;
105    let mut has_dynamic_length = false;
106    let mut bits_count = None;
107    let mut skip_bits = None;
108    let mut key_dyn_length = false;
109    let mut val_dyn_length = false;
110    let mut multi_enum = false;
111
112    // Search attributes for length/toggle declarations
113    for attr in field.attrs.iter() {
114        let ident = attr.path().get_ident().map(|i| i.clone().to_string());
115        match ident.as_deref() {
116            Some("dyn_int") => is_dynamic_int = true,
117            Some("dyn_length") => has_dynamic_length = true,
118            Some("key_dyn_length") => key_dyn_length = true,
119            Some("val_dyn_length") => val_dyn_length = true,
120            Some("multi_enum") => multi_enum = true,
121            Some("toggles") => toggle_key = get_string_value_from_attribute(attr),
122            Some("variant_for") => {
123                if let Some(v) = get_string_value_from_attribute(attr) {
124                    variant_keys.push(v);
125                }
126            }
127            Some("length_for") => {
128                if let Some(v) = get_string_value_from_attribute(attr) {
129                    length_keys.push(v);
130                }
131            }
132            Some("toggled_by") => toggled_by = get_string_value_from_attribute(attr),
133            Some("toggled_by_variant") => {
134                toggled_by_variant = get_string_value_from_attribute(attr)
135            }
136            Some("variant_by") => variant_by = get_string_value_from_attribute(attr),
137            Some("length_by") => length_by = get_string_value_from_attribute(attr),
138            Some("bits") => bits_count = get_int_value_from_attribute(attr).map(|b| b as u8),
139            Some("skip_bits") => skip_bits = get_int_value_from_attribute(attr).map(|b| b as u8),
140            _ => {} // None => continue
141        }
142    }
143
144    let val_reference = if matches!(single_ident_type_name, Some(s) if s == String::from("RefCell"))
145    {
146        if read {
147            quote! {
148                *#field_ident.borrow()
149            }
150        } else {
151            quote! {
152                *_p_val.borrow()
153            }
154        }
155    } else {
156        if read {
157            quote! {
158                _p_val
159            }
160        } else {
161            quote! {
162                *_p_val
163            }
164        }
165    };
166
167    // Runtime toggle_key
168    let toggles = if let Some(key) = toggle_key {
169        quote! {
170            _p_config.set_toggle(#key, #val_reference);
171        }
172    } else {
173        quote! {}
174    };
175
176    // Runtime length_for keys (support multiple)
177    let length_calls: Vec<proc_macro2::TokenStream> = length_keys
178        .iter()
179        .map(|k| quote! { _p_config.set_length(#k, #val_reference as usize); })
180        .collect();
181
182    let length = if !length_calls.is_empty() {
183        quote! { #(#length_calls)* }
184    } else {
185        quote! {}
186    };
187
188    // Runtime variant_for keys (support multiple)
189    let variant_calls: Vec<proc_macro2::TokenStream> = variant_keys
190        .iter()
191        .map(|k| quote! { _p_config.set_variant(#k, #val_reference as u8); })
192        .collect();
193
194    let variant = if !variant_calls.is_empty() {
195        quote! { #(#variant_calls)* }
196    } else {
197        quote! {}
198    };
199
200    // Compose code to handle field
201    let f_ident = if is_enum {
202        quote! { #field_ident }
203    } else {
204        quote! { &self.#field_ident }
205    };
206
207    let before = if read {
208        quote! {}
209    } else {
210        quote! {
211            let _p_val = #f_ident;
212            #toggles
213            #length
214            #variant
215        }
216    };
217
218    let after = if read {
219        quote! {
220            let #field_ident = _p_val;
221            #toggles
222            #length
223            #variant
224        }
225    } else {
226        quote! {}
227    };
228
229    let skip_bits_code = if let Some(skip) = skip_bits && skip > 0 {
230        if read {
231            quote! {
232                let _ = _p_stream.read_small(#skip)?;
233            }
234        } else {
235            quote! {
236                _p_stream.write_small(0, #skip);
237            }
238        }
239    } else {
240        quote! {}
241    };
242
243    let handle_field = generate_code_for_handling_field(
244        read,
245        field_type,
246        field_ident,
247        bits_count,
248        toggled_by,
249        toggled_by_variant,
250        variant_by,
251        length_by,
252        is_dynamic_int,
253        has_dynamic_length,
254        key_dyn_length,
255        val_dyn_length,
256        multi_enum,
257        false,
258        0,
259    );
260
261    quote! {
262        #before
263        #skip_bits_code
264        #handle_field
265        #after
266    }
267}
268
269fn generate_struct_serializer(
270    read: bool,
271    ast: &DeriveInput,
272    data_struct: &syn::DataStruct,
273) -> proc_macro::TokenStream {
274    let fields = &data_struct.fields;
275    let struct_name = &ast.ident;
276
277    // Iterate all fields in the struct
278    let field_serializations = fields.iter().map(|field| {
279        generate_field_serializer(
280            read,
281            &field
282                .ident
283                .as_ref()
284                .expect("binary-codec does not support fields without a name"),
285            &field.ty,
286            field,
287            false,
288        )
289    });
290
291    let error_type = generate_error_type(read, &ast.attrs);
292    let serializer_code = if read {
293        let vars = fields.iter().map(|f| f.ident.as_ref().unwrap());
294
295        // read bytes code
296        quote! {
297            impl<T : Clone> binary_codec::BinaryDeserializer<T, #error_type> for #struct_name {
298                fn read_bytes(
299                    stream: &mut binary_codec::BitStreamReader,
300                    config: Option<&mut binary_codec::SerializerConfig<T>>,
301                ) -> Result<Self, #error_type> {
302                    let mut _new_config = binary_codec::SerializerConfig::new(None);
303                    let _p_config = config.unwrap_or(&mut _new_config);
304                    let _p_stream = stream;
305
306                    #(#field_serializations)*
307
308                    Ok(Self {
309                        #(#vars),*
310                    })
311                }
312            }
313        }
314    } else {
315        // write bytes code
316        quote! {
317            impl<T : Clone> binary_codec::BinarySerializer<T, #error_type> for #struct_name {
318                fn write_bytes(
319                    &self,
320                    stream: &mut binary_codec::BitStreamWriter,
321                    config: Option<&mut binary_codec::SerializerConfig<T>>,
322                ) -> Result<(), #error_type> {
323                    let mut _new_config = binary_codec::SerializerConfig::new(None);
324                    let _p_config = config.unwrap_or(&mut _new_config);
325                    let _p_stream = stream;
326
327                    #(#field_serializations)*
328                    Ok(())
329                }
330            }
331        }
332    };
333
334    serializer_code.into()
335}
336
337fn generate_enum_serializer(
338    read: bool,
339    ast: &DeriveInput,
340    data_enum: &syn::DataEnum,
341) -> proc_macro::TokenStream {
342    let enum_name = &ast.ident;
343    let error_type = generate_error_type(read, &ast.attrs);
344
345    let mut no_disc_prefix = false;
346    let mut disc_bits = None;
347
348    // Search attributes for variant_by declarations
349    for attr in ast.attrs.iter() {
350        // #[no_disc_prefix] attribute
351        if attr.path().is_ident("no_discriminator") {
352            no_disc_prefix = true;
353        }
354
355        if attr.path().is_ident("discriminator_bits") {
356            disc_bits = get_int_value_from_attribute(attr).map(|b| b as u8);
357        }
358    }
359
360    if let Some(bits) = disc_bits {
361        if no_disc_prefix {
362            panic!("Cannot use discriminator_bits and no_discriminator together");
363        }
364
365        if bits < 1 || bits > 8 {
366            panic!("discriminator_bits should be between 1 and 8");
367        }
368    }
369
370    let mut configure_functions = Vec::new();
371
372    // Compute discriminant values following Rust rules: explicit values are used,
373    // unspecified values get previous + 1 (or 0 for the first unspecified).
374    let mut disc_values: Vec<u8> = Vec::with_capacity(data_enum.variants.len());
375    let mut last_val: Option<u8> = None;
376    for variant in data_enum.variants.iter() {
377        let val = if let Some((_, expr)) = &variant.discriminant {
378            match expr {
379                syn::Expr::Lit(syn::ExprLit {
380                    lit: Lit::Int(lit_int),
381                    ..
382                }) => lit_int
383                    .base10_parse::<u8>()
384                    .expect("Invalid discriminant integer"),
385                _ => panic!("Discriminant must be an integer literal"),
386            }
387        } else {
388            match last_val {
389                Some(v) => v + 1,
390                None => 0,
391            }
392        };
393
394        if val > u8::from(u8::MAX) {
395            panic!("Discriminant value too large (must fit in u8)");
396        }
397
398        disc_values.push(val);
399        last_val = Some(val);
400    }
401
402    // Create discriminant getter
403    let disc_variants = data_enum
404        .variants
405        .iter()
406        .enumerate()
407        .map(|(i, variant)| {
408            let var_ident = &variant.ident;
409            let disc_value = disc_values[i];
410
411            for attr in variant.attrs.iter() {
412                if attr.path().is_ident("toggled_by") {
413                    let field = get_string_value_from_attribute(attr)
414                        .expect("toggled_by for multi_enum should have a value");
415                    configure_functions.push(quote! {
416                        _p_config.configure_multi_disc(stringify!(#enum_name), #disc_value, #field);
417                    });
418                }
419            }
420
421            match &variant.fields {
422                Fields::Unit => quote! {
423                    Self::#var_ident => #disc_value
424                },
425                Fields::Unnamed(_) => quote! {
426                    Self::#var_ident(..) => #disc_value
427                },
428                Fields::Named(_) => quote! {
429                    Self::#var_ident { .. } => #disc_value
430                },
431            }
432        })
433        .collect::<Vec<_>>();
434
435    // Assign discriminant values starting from 0
436    let serialization_variants = data_enum.variants.iter().enumerate().map(|(i, variant)| {
437        let var_ident = &variant.ident;
438        let disc_value = disc_values[i];
439        let fields = &variant.fields;
440
441        // TODO: problem might be that attrs are not used from the fields??.
442
443        let write_disc = if no_disc_prefix {
444            quote! {}
445        } else {
446            let disc_writer = if let Some(bits) = disc_bits {
447                quote! {
448                    _p_stream.write_small(_p_disc, #bits);
449                }
450            } else {
451                quote! {
452                    _p_stream.write_fixed_int(_p_disc);
453                }
454            };
455
456            quote! {
457                let _p_disc: u8 = #disc_value;
458                #disc_writer
459            }
460        };
461
462        match fields {
463            Fields::Unit => {
464                if read {
465                    quote! {
466                        #disc_value => {
467                            Ok(Self::#var_ident)
468                        }
469                    }
470                } else {
471                    quote! {
472                        Self::#var_ident => {
473                            #write_disc
474                        }
475                    }
476                }
477            }
478            Fields::Unnamed(fields_unnamed) => {
479                let field_count = fields_unnamed.unnamed.len();
480                let idents: Vec<_> = (0..field_count).map(|i| format_ident!("f{}", i)).collect();
481                let ident_refs: Vec<&syn::Ident> = idents.iter().collect();
482                let field_serializations =
483                    generate_enum_field_serializations(read, &ident_refs, &fields_unnamed.unnamed);
484                if read {
485                    quote! {
486                        #disc_value => {
487                            #(#field_serializations)*
488                            Ok(Self::#var_ident(#(#idents),*))
489                        }
490                    }
491                } else {
492                    quote! {
493                        Self::#var_ident(#(#idents),*) => {
494                            #write_disc
495                            #(#field_serializations)*
496                        }
497                    }
498                }
499            }
500            Fields::Named(fields_named) => {
501                let field_idents: Vec<_> = fields_named
502                    .named
503                    .iter()
504                    .map(|f| f.ident.as_ref().unwrap())
505                    .collect();
506
507                let field_serializations =
508                    generate_enum_field_serializations(read, &field_idents, &fields_named.named);
509
510                if read {
511                    quote! {
512                        #disc_value => {
513                            #(#field_serializations)*
514                            Ok(Self::#var_ident { #(#field_idents),* })
515                        }
516                    }
517                } else {
518                    quote! {
519                        Self::#var_ident { #(#field_idents),* } => {
520                            #write_disc
521                            #(#field_serializations)*
522                        }
523                    }
524                }
525            }
526        }
527    });
528
529    if read {
530        let disc_reader = if let Some(bits) = disc_bits {
531            quote! {
532                _p_stream.read_small(#bits)?
533            }
534        } else {
535            quote! {
536                 _p_stream.read_fixed_int()?
537            }
538        };
539
540        quote! {
541            impl #enum_name {
542                pub fn configure_multi_disc<T : Clone>(config: &mut binary_codec::SerializerConfig<T>) {
543                    let _p_config = config;
544                    #(#configure_functions)*
545                }
546            }
547
548            impl<T : Clone> binary_codec::BinaryDeserializer<T, #error_type> for #enum_name {
549                fn read_bytes(
550                    stream: &mut binary_codec::BitStreamReader,
551                    config: Option<&mut binary_codec::SerializerConfig<T>>,
552                ) -> Result<Self, #error_type> {
553                    let mut _new_config = binary_codec::SerializerConfig::new(None);
554                    let _p_config = config.unwrap_or(&mut _new_config);
555                    let _p_stream = stream;
556
557                    let _p_disc = if let Some(disc) = _p_config.discriminator.take() {
558                        disc
559                    } else {
560                        #disc_reader
561                    };
562
563                    match _p_disc {
564                        #(#serialization_variants,)*
565                        _ => Err(binary_codec::DeserializationError::UnknownDiscriminant(_p_disc).into()),
566                    }
567                }
568            }
569        }
570        .into()
571    } else {
572        quote! {
573            impl<T : Clone> binary_codec::BinarySerializer<T, #error_type> for #enum_name {
574                fn write_bytes(
575                    &self,
576                    stream: &mut binary_codec::BitStreamWriter,
577                    config: Option<&mut binary_codec::SerializerConfig<T>>,
578                ) -> Result<(), #error_type> {
579                    let mut _new_config = binary_codec::SerializerConfig::new(None);
580                    let _p_config = config.unwrap_or(&mut _new_config);
581                    #(#configure_functions)*
582                    let _p_stream = stream;
583
584                    match self {
585                        #(#serialization_variants)*
586                    }
587
588                    Ok(())
589                }
590            }
591
592            impl #enum_name {
593                pub fn get_discriminator(&self) -> u8 {
594                    match self {
595                        #(#disc_variants,)*
596                    }
597                }
598            }
599        }
600        .into()
601    }
602}
603
604fn generate_enum_field_serializations(
605    read: bool,
606    idents: &Vec<&syn::Ident>,
607    fields: &Punctuated<syn::Field, Comma>,
608) -> Vec<proc_macro2::TokenStream> {
609    let field_serializations = fields.iter().enumerate().map(|(i, f)| {
610        let field_type = &f.ty;
611        let field_ident = &idents[i];
612
613        generate_field_serializer(read, &field_ident, field_type, f, true)
614    });
615    field_serializations.collect()
616}
617
618fn generate_code_for_handling_field(
619    read: bool,
620    field_type: &Type,
621    field_name: &syn::Ident,
622    bits_count: Option<u8>,
623    toggled_by: Option<String>,
624    toggled_by_variant: Option<String>,
625    variant_by: Option<String>,
626    length_by: Option<String>,
627    is_dynamic_int: bool,
628    has_dynamic_length: bool,
629    key_dyn_length: bool,
630    val_dyn_length: bool,
631    multi_enum: bool,
632    direct_collection_child: bool,
633    level: usize,
634) -> proc_macro2::TokenStream {
635    if let Type::Path(path) = field_type {
636        let path = &path.path;
637
638        if let Some(ident) = path.get_ident() {
639            let ident_name = ident.to_string();
640
641            // Single segment without arguments
642            match ident_name.as_str() {
643                "bool" => {
644                    if read {
645                        quote! { let _p_val = _p_stream.read_bit()?;}
646                    } else {
647                        quote! { _p_stream.write_bit(*_p_val); }
648                    }
649                }
650                "i8" => {
651                    if let Some(bits_count) = bits_count.as_ref() {
652                        if *bits_count < 1 || *bits_count > 7 {
653                            panic!("Bits count should be between 1 and 7");
654                        }
655
656                        if read {
657                            quote! { let _p_val = binary_codec::ZigZag::to_signed(_p_stream.read_small(#bits_count)?); }
658                        } else {
659                            quote! { _p_stream.write_small(binary_codec::ZigZag::to_unsigned(*_p_val), #bits_count); }
660                        }
661                    } else {
662                        if read {
663                            quote! { let _p_val = _p_stream.read_fixed_int()?; }
664                        } else {
665                            quote! { _p_stream.write_fixed_int(*_p_val); }
666                        }
667                    }
668                }
669                "u8" => {
670                    if let Some(bits_count) = bits_count.as_ref() {
671                        if *bits_count < 1 || *bits_count > 7 {
672                            panic!("Bits count should be between 1 and 7");
673                        }
674
675                        if read {
676                            quote! { let _p_val = _p_stream.read_small(#bits_count)?; }
677                        } else {
678                            quote! { _p_stream.write_small(*_p_val, #bits_count); }
679                        }
680                    } else {
681                        if read {
682                            quote! { let _p_val = _p_stream.read_byte()?; }
683                        } else {
684                            quote! { _p_stream.write_byte(*_p_val); }
685                        }
686                    }
687                }
688                "u16" | "u32" | "u64" | "u128" => {
689                    if is_dynamic_int {
690                        let dynint: proc_macro2::TokenStream = generate_dynint(read);
691                        if read {
692                            quote! {
693                                #dynint
694                                let _p_val = _p_dyn as #ident;
695                            }
696                        } else {
697                            quote! {
698                                let _p_dyn = *_p_val as u128;
699                                #dynint
700                            }
701                        }
702                    } else {
703                        if read {
704                            quote! { let _p_val = _p_stream.read_fixed_int()?; }
705                        } else {
706                            quote! { _p_stream.write_fixed_int(*_p_val); }
707                        }
708                    }
709                }
710                "i16" | "i32" | "i64" | "i128" => {
711                    if is_dynamic_int {
712                        let dynint: proc_macro2::TokenStream = generate_dynint(read);
713                        if read {
714                            quote! {
715                                #dynint
716                                let _p_val: #ident = binary_codec::ZigZag::to_signed(_p_dyn);
717                            }
718                        } else {
719                            quote! {
720                                let _p_dyn = binary_codec::ZigZag::to_unsigned(*_p_val) as u128;
721                                #dynint
722                            }
723                        }
724                    } else {
725                        if read {
726                            quote! { let _p_val = _p_stream.read_fixed_int()?; }
727                        } else {
728                            quote! { _p_stream.write_fixed_int(*_p_val); }
729                        }
730                    }
731                }
732                "f32" | "f64" => {
733                    if read {
734                        quote! { let _p_val = _p_stream.read_fixed_int()?; }
735                    } else {
736                        quote! { _p_stream.write_fixed_int(*_p_val); }
737                    }
738                }
739                "String" => {
740                    let size_key = generate_size_key(length_by, has_dynamic_length).1;
741
742                    if read {
743                        quote! {
744                            let _p_val = binary_codec::utils::read_string(_p_stream, #size_key, _p_config)?;
745                        }
746                    } else {
747                        quote! {
748                            binary_codec::utils::write_string(_p_val, #size_key, _p_stream, _p_config)?;
749                        }
750                    }
751                }
752                "SocketAddrV4" => {
753                    if read {
754                        quote! {
755                            let _p_val = binary_codec::utils::read_socketaddr_v4(_p_stream)?;
756                        }
757                    } else {
758                        quote! {
759                            binary_codec::utils::write_socketaddr_v4(_p_val, _p_stream);
760                        }
761                    }
762                }
763                "SocketAddrV6" => {
764                    if read {
765                        quote! {
766                            let _p_val = binary_codec::utils::read_socketaddr_v6(_p_stream)?;
767                        }
768                    } else {
769                        quote! {
770                            binary_codec::utils::write_socketaddr_v6(_p_val, _p_stream);
771                        }
772                    }
773                }
774                _ => {
775                    let size_key = generate_size_key(length_by, has_dynamic_length).1;
776
777                    let variant_code = if variant_by.is_some() {
778                        quote! {
779                            _p_config.discriminator = _p_config.get_variant(#variant_by);
780                        }
781                    } else if multi_enum {
782                        let config_multi = if !direct_collection_child {
783                            quote! { #ident::configure_multi_disc(_p_config); }
784                        } else {
785                            quote! {}
786                        };
787
788                        quote! {
789                            #config_multi
790                            _p_config.discriminator = _p_config.get_next_multi_disc(stringify!(#field_name), #ident_name);
791                        }
792                    } else {
793                        quote! {
794                            _p_config.discriminator = None;
795                        }
796                    };
797
798                    if read {
799                        quote! {
800                            #variant_code
801                            let _p_val = binary_codec::utils::read_object(_p_stream, #size_key, _p_config)?;
802                        }
803                    } else {
804                        quote! {
805                            #variant_code
806                            binary_codec::utils::write_object(_p_val, #size_key, _p_stream, _p_config)?;
807                        }
808                    }
809                }
810            }
811        } else {
812            // Multiple segments, or arguments
813            if path.segments.len() == 1 {
814                let ident = &path.segments[0].ident;
815                let ident_name = ident.to_string();
816
817                match ident_name.as_ref() {
818                    "RefCell" => {
819                        let inner_type = get_inner_type(path).expect("Option missing inner type");
820                        let handle = generate_code_for_handling_field(
821                            read,
822                            inner_type,
823                            field_name,
824                            bits_count,
825                            None,
826                            None,
827                            variant_by,
828                            length_by,
829                            is_dynamic_int,
830                            has_dynamic_length,
831                            key_dyn_length,
832                            val_dyn_length,
833                            multi_enum,
834                            false,
835                            level + 1,
836                        );
837
838                        if read {
839                            quote! {
840                                #handle
841                                let _p_val = RefCell::new(_p_val);
842                            }
843                        } else {
844                            quote! {
845                                let _p_val = &*_p_val.borrow();
846                                #handle
847                            }
848                        }
849                    }
850                    "Option" => {
851                        let inner_type = get_inner_type(path).expect("Option missing inner type");
852                        let handle = generate_code_for_handling_field(
853                            read,
854                            inner_type,
855                            field_name,
856                            bits_count,
857                            None,
858                            None,
859                            variant_by,
860                            length_by,
861                            is_dynamic_int,
862                            has_dynamic_length,
863                            key_dyn_length,
864                            val_dyn_length,
865                            multi_enum,
866                            false,
867                            level + 1,
868                        );
869
870                        let option_name: syn::Ident = format_ident!("__option_{}", level);
871
872                        if let Some(toggled_by) = toggled_by {
873                            // If toggled_by is set, read or write it
874                            let toggled_by = quote! {
875                                _p_config.get_toggle(#toggled_by).unwrap_or(false)
876                            };
877
878                            if read {
879                                quote! {
880                                    let mut #option_name: Option<#inner_type> = None;
881                                    if #toggled_by {
882                                        #handle
883                                        #option_name = Some(_p_val);
884                                    }
885                                    let _p_val = #option_name;
886                                }
887                            } else {
888                                quote! {
889                                    if #toggled_by {
890                                        let _p_val = _p_val.as_ref().expect("Expected Some value, because toggled_by field is true");
891                                        #handle
892                                    }
893                                }
894                            }
895                        } else if let Some(toggled_by_variant) = toggled_by_variant {
896                            // If toggled_by_variant is set, read or write it
897                            let toggled_by = quote! {
898                                _p_config.get_variant_toggle(#toggled_by_variant).unwrap_or(false)
899                            };
900
901                            if read {
902                                quote! {
903                                    let mut #option_name: Option<#inner_type> = None;
904                                    if #toggled_by {
905                                        #handle
906                                        #option_name = Some(_p_val);
907                                    }
908                                    let _p_val = #option_name;
909                                }
910                            } else {
911                                quote! {
912                                    if #toggled_by {
913                                        let _p_val = _p_val.as_ref().expect("Expected Some value, because toggled_by_variant field evalutates to true");
914                                        #handle
915                                    }
916                                }
917                            }
918                        } else {
919                            // If space available, read it, write it if not None
920                            if read {
921                                quote! {
922                                    let mut #option_name: Option<#inner_type> = None;
923                                    if _p_stream.bytes_left() > 0 {
924                                        #handle
925                                        #option_name = Some(_p_val);
926                                    }
927                                    let _p_val = #option_name;
928                                }
929                            } else {
930                                quote! {
931                                    if let Some(_p_val) = _p_val.as_ref() {
932                                        #handle
933                                    }
934                                }
935                            }
936                        }
937                    }
938                    "Vec" => {
939                        let vec_name = format_ident!("__val_{}", level);
940                        let inner_type = get_inner_type(path).expect("Vec missing inner type");
941
942                        // If inner type is u8, optimize to bulk read/write bytes
943                        if let Type::Path(inner_path) = inner_type {
944                            if let Some(inner_ident) = inner_path.path.get_ident() {
945                                if inner_ident == "u8" {
946                                    let (has_size, size_key) =
947                                        generate_size_key(length_by, has_dynamic_length);
948
949                                    if read {
950                                        if has_size || multi_enum {
951                                            // sized read
952                                            let len_code = if multi_enum {
953                                                quote! {
954                                                    // multi_enum sized Vec<u8>
955                                                    let _p_len = _p_config.get_multi_disc_size("u8");
956                                                }
957                                            } else {
958                                                quote! {
959                                                    let _p_len = binary_codec::utils::get_read_size(_p_stream, #size_key, _p_config)?;
960                                                }
961                                            };
962
963                                            return quote! {
964                                                #len_code
965                                                let _p_val = _p_stream.read_bytes(_p_len)?.to_vec();
966                                            };
967                                        } else {
968                                            // read all remaining bytes
969                                            return quote! {
970                                                let _p_len = _p_stream.bytes_left();
971                                                let _p_val = _p_stream.read_bytes(_p_len)?.to_vec();
972                                            };
973                                        }
974                                    } else {
975                                        // write path: if sized, write size first
976                                        let write_size = if has_size {
977                                            quote! {
978                                                let _p_len = _p_val.len();
979                                                binary_codec::utils::write_size(_p_len, #size_key, _p_stream, _p_config)?;
980                                            }
981                                        } else {
982                                            quote! {}
983                                        };
984
985                                        return quote! {
986                                            #write_size
987                                            _p_stream.write_bytes(_p_val);
988                                        };
989                                    }
990                                }
991                            }
992                        }
993
994                        // Fallback to element-wise handling for non-u8 inner types
995                        let handle = generate_code_for_handling_field(
996                            read,
997                            inner_type,
998                            field_name,
999                            bits_count,
1000                            None,
1001                            None,
1002                            None,
1003                            None,
1004                            is_dynamic_int,
1005                            val_dyn_length,
1006                            false,
1007                            false,
1008                            multi_enum,
1009                            true,
1010                            level + 1,
1011                        );
1012
1013                        let (has_size, size_key) = generate_size_key(length_by, has_dynamic_length);
1014
1015                        let write_code = quote! {
1016                            for _p_val in _p_val {
1017                                #handle
1018                            }
1019                        };
1020
1021                        if has_size || (read && multi_enum) {
1022                            if read {
1023                                let len_code = if multi_enum && let Type::Path(path) = inner_type {
1024                                    let enum_ident = path
1025                                        .path
1026                                        .get_ident()
1027                                        .expect("Expected ident for multi_enum inner type");
1028                                    quote! {
1029                                        #enum_ident::configure_multi_disc(_p_config);
1030                                        let _p_len = _p_config.get_multi_disc_size(stringify!(#enum_ident));
1031                                    }
1032                                } else {
1033                                    quote! {
1034                                        let _p_len = binary_codec::utils::get_read_size(_p_stream, #size_key, _p_config)?;
1035                                    }
1036                                };
1037
1038                                quote! {
1039                                    #len_code
1040                                    let mut #vec_name = Vec::<#inner_type>::with_capacity(_p_len);
1041                                    for _ in 0.._p_len {
1042                                        #handle
1043                                        #vec_name.push(_p_val);
1044                                    }
1045                                    let _p_val = #vec_name;
1046                                }
1047                            } else {
1048                                quote! {
1049                                    let _p_len = _p_val.len();
1050                                    binary_codec::utils::write_size(_p_len, #size_key, _p_stream, _p_config)?;
1051                                    #write_code
1052                                }
1053                            }
1054                        } else {
1055                            if read {
1056                                quote! {
1057                                    let mut #vec_name = Vec::<#inner_type>::new();
1058                                    while _p_stream.bytes_left() > 0 {
1059                                        #handle
1060                                        #vec_name.push(_p_val);
1061                                    }
1062                                    let _p_val = #vec_name;
1063                                }
1064                            } else {
1065                                quote! {
1066                                    #write_code
1067                                }
1068                            }
1069                        }
1070                    }
1071                    "HashMap" => {
1072                        let (key_type, value_type) =
1073                            get_two_types(path).expect("Failed to get HashMap types");
1074
1075                        let handle_key = generate_code_for_handling_field(
1076                            read,
1077                            key_type,
1078                            field_name,
1079                            None,
1080                            None,
1081                            None,
1082                            None,
1083                            None,
1084                            is_dynamic_int,
1085                            key_dyn_length,
1086                            false,
1087                            false,
1088                            false,
1089                            false,
1090                            level + 1,
1091                        );
1092
1093                        let handle_value = generate_code_for_handling_field(
1094                            read,
1095                            value_type,
1096                            field_name,
1097                            None,
1098                            None,
1099                            None,
1100                            None,
1101                            None,
1102                            is_dynamic_int,
1103                            val_dyn_length,
1104                            false,
1105                            false,
1106                            false,
1107                            false,
1108                            level + 1,
1109                        );
1110
1111                        let (has_size, size_key) = generate_size_key(length_by, has_dynamic_length);
1112
1113                        let write_code = quote! {
1114                            for (key, value) in _p_val {
1115                                let _p_val = key;
1116                                #handle_key
1117                                let _p_val = value;
1118                                #handle_value
1119                            }
1120                        };
1121
1122                        if read {
1123                            if has_size {
1124                                quote! {
1125                                    let _p_len = binary_codec::utils::get_read_size(_p_stream, #size_key, _p_config)?;
1126                                    let mut _p_map = std::collections::HashMap::<#key_type, #value_type>::with_capacity(_p_len);
1127                                    for _ in 0.._p_len {
1128                                        let _p_key;
1129                                        #handle_key
1130                                        _p_key = _p_val;
1131                                        let _p_value;
1132                                        #handle_value
1133                                        _p_value = _p_val;
1134                                        _p_map.insert(_p_key, _p_value);
1135                                    }
1136                                    let _p_val = _p_map;
1137                                }
1138                            } else {
1139                                quote! {
1140                                    let mut _p_map = std::collections::HashMap::<#key_type, #value_type>::new();
1141                                    while _p_stream.bytes_left() > 0 {
1142                                        let _p_key;
1143                                        #handle_key
1144                                        _p_key = _p_val;
1145                                        let _p_value;
1146                                        #handle_value
1147                                        _p_value = _p_val;
1148                                        _p_map.insert(_p_key, _p_value);
1149                                    }
1150                                    let _p_val = _p_map;
1151                                }
1152                            }
1153                        } else {
1154                            if has_size {
1155                                quote! {
1156                                    let _p_len = _p_val.len();
1157                                    binary_codec::utils::write_size(_p_len, #size_key, _p_stream, _p_config)?;
1158                                    #write_code
1159                                }
1160                            } else {
1161                                quote! {
1162                                    #write_code
1163                                }
1164                            }
1165                        }
1166                    }
1167                    _ => {
1168                        panic!("Type not implemented")
1169                    }
1170                }
1171            } else {
1172                panic!("Multi-segment paths are not supported");
1173            }
1174        }
1175    } else if let Type::Array(array) = field_type {
1176        let len: usize = if let syn::Expr::Lit(ref arr_len_lit) = array.len {
1177            if let Lit::Int(ref lit_int) = arr_len_lit.lit {
1178                lit_int
1179                    .base10_parse()
1180                    .expect("Failed to parse literal to usize")
1181            } else {
1182                panic!("Expected an int to determine array length");
1183            }
1184        } else {
1185            panic!("Expected literal to determine array length");
1186        };
1187
1188        let array_type = &*array.elem;
1189        // Optimize [u8; N] to bulk read_bytes / write_bytes
1190        if let Type::Path(at_path) = array_type {
1191            if let Some(at_ident) = at_path.path.get_ident() {
1192                if at_ident == "u8" {
1193                    if read {
1194                        quote! {
1195                            let _p_slice = _p_stream.read_bytes(#len)?;
1196                            let _p_val = <[u8; #len]>::try_from(_p_slice).expect("Failed to convert slice to array");
1197                        }
1198                    } else {
1199                        quote! {
1200                            _p_stream.write_bytes(_p_val);
1201                        }
1202                    }
1203                } else {
1204                    let handle = generate_code_for_handling_field(
1205                        read,
1206                        array_type,
1207                        field_name,
1208                        bits_count,
1209                        None,
1210                        None,
1211                        None,
1212                        None,
1213                        is_dynamic_int,
1214                        val_dyn_length,
1215                        false,
1216                        false,
1217                        false,
1218                        true,
1219                        level + 1,
1220                    );
1221
1222                    let array_name = format_ident!("__val_{}", level);
1223
1224                    if read {
1225                        quote! {
1226                            let mut #array_name = Vec::<#array_type>::with_capacity(#len);
1227                            for _ in 0..#len {
1228                                #handle;
1229                                #array_name.push(_p_val);
1230                            }
1231                            let _p_val = TryInto::<[#array_type; #len]>::try_into(#array_name).expect("Failed to convert Vec to array");
1232                        }
1233                    } else {
1234                        quote! {
1235                            for _p_val in _p_val {
1236                                #handle
1237                            }
1238                        }
1239                    }
1240                }
1241            } else {
1242                // fallback to element handling
1243                let handle = generate_code_for_handling_field(
1244                    read,
1245                    array_type,
1246                    field_name,
1247                    bits_count,
1248                    None,
1249                    None,
1250                    None,
1251                    None,
1252                    is_dynamic_int,
1253                    val_dyn_length,
1254                    false,
1255                    false,
1256                    false,
1257                    true,
1258                    level + 1,
1259                );
1260
1261                let array_name = format_ident!("__val_{}", level);
1262
1263                if read {
1264                    quote! {
1265                        let mut #array_name = Vec::<#array_type>::with_capacity(#len);
1266                        for _ in 0..#len {
1267                            #handle;
1268                            #array_name.push(_p_val);
1269                        }
1270                        let _p_val = TryInto::<[#array_type; #len]>::try_into(#array_name).expect("Failed to convert Vec to array");
1271                    }
1272                } else {
1273                    quote! {
1274                        for _p_val in _p_val {
1275                            #handle
1276                        }
1277                    }
1278                }
1279            }
1280        } else {
1281            panic!("Unsupported array element type");
1282        }
1283    } else {
1284        panic!("Field type of '{:?}' not supported", field_name);
1285    }
1286}
1287
1288fn generate_error_type(read: bool, attrs: &[Attribute]) -> proc_macro2::TokenStream {
1289    if let Some(custom) = get_custom_error_type(read, attrs) {
1290        return custom;
1291    }
1292
1293    if read {
1294        quote! { binary_codec::DeserializationError }
1295    } else {
1296        quote! { binary_codec::SerializationError }
1297    }
1298}
1299
1300fn get_custom_error_type(read: bool, attrs: &[Attribute]) -> Option<proc_macro2::TokenStream> {
1301    let specific = if read {
1302        "codec_de_error"
1303    } else {
1304        "codec_ser_error"
1305    };
1306
1307    let specific_value = attrs
1308        .iter()
1309        .find(|attr| attr.path().is_ident(specific))
1310        .and_then(get_string_value_from_attribute);
1311
1312    if let Some(value) = specific_value {
1313        return Some(parse_error_type(&value));
1314    }
1315
1316    let common_value = attrs
1317        .iter()
1318        .find(|attr| attr.path().is_ident("codec_error"))
1319        .and_then(get_string_value_from_attribute);
1320
1321    common_value.map(|value| parse_error_type(&value))
1322}
1323
1324fn parse_error_type(value: &str) -> proc_macro2::TokenStream {
1325    let ty: Type = syn::parse_str(value).expect("Invalid error type for codec_error");
1326    quote! { #ty }
1327}
1328
1329fn generate_size_key(
1330    length_by: Option<String>,
1331    has_dynamic_length: bool,
1332) -> (bool, proc_macro2::TokenStream) {
1333    if let Some(length_by) = length_by.as_ref() {
1334        (true, quote! { Some(#length_by) })
1335    } else if has_dynamic_length {
1336        (true, quote! { Some("__dynamic") })
1337    } else {
1338        (false, quote! { None })
1339    }
1340}
1341
1342fn get_string_value_from_attribute(attr: &Attribute) -> Option<String> {
1343    match &attr.meta {
1344        syn::Meta::Path(_) => None,
1345        syn::Meta::List(list_value) => {
1346            // #[myattribute("value")]
1347            for token in list_value.tokens.clone().into_iter() {
1348                if let proc_macro2::TokenTree::Literal(lit) = token {
1349                    return Some(lit.to_string().trim_matches('"').to_string());
1350                }
1351            }
1352
1353            None
1354        }
1355        syn::Meta::NameValue(name_value) => {
1356            if let syn::Expr::Lit(lit_expr) = &name_value.value {
1357                if let Lit::Str(lit_str) = &lit_expr.lit {
1358                    return Some(lit_str.value());
1359                }
1360            }
1361
1362            None
1363        }
1364    }
1365}
1366
1367fn get_int_value_from_attribute(attr: &Attribute) -> Option<i32> {
1368    match &attr.meta {
1369        syn::Meta::Path(_) => None,
1370        syn::Meta::List(list_value) => {
1371            // #[myattribute(value)]
1372            for token in list_value.tokens.clone().into_iter() {
1373                if let proc_macro2::TokenTree::Literal(lit) = token {
1374                    if let Ok(val) = lit.to_string().parse::<i32>() {
1375                        return Some(val);
1376                    }
1377                }
1378            }
1379
1380            None
1381        }
1382        syn::Meta::NameValue(name_value) => {
1383            if let syn::Expr::Lit(lit_expr) = &name_value.value {
1384                if let Lit::Int(lit_int) = &lit_expr.lit {
1385                    return Some(lit_int.base10_parse().expect("Not a valid int value"));
1386                }
1387            }
1388
1389            None
1390        }
1391    }
1392}
1393
1394fn get_inner_type(path: &syn::Path) -> Option<&syn::Type> {
1395    if let Some(PathArguments::AngleBracketed(args)) =
1396        path.segments.last().map(|seg| &seg.arguments)
1397    {
1398        if let Some(arg) = args.args.first() {
1399            if let syn::GenericArgument::Type(inner_type) = arg {
1400                return Some(inner_type);
1401            }
1402        }
1403    }
1404
1405    None
1406}
1407
1408fn get_two_types(path: &syn::Path) -> Option<(&syn::Type, &syn::Type)> {
1409    if let Some(PathArguments::AngleBracketed(args)) =
1410        path.segments.last().map(|seg| &seg.arguments)
1411    {
1412        let mut types = args.args.iter().filter_map(|arg| {
1413            if let syn::GenericArgument::Type(inner_type) = arg {
1414                Some(inner_type)
1415            } else {
1416                None
1417            }
1418        });
1419
1420        if let (Some(t1), Some(t2)) = (types.next(), types.next()) {
1421            return Some((t1, t2));
1422        }
1423    }
1424
1425    None
1426}
1427
1428fn generate_dynint(read: bool) -> proc_macro2::TokenStream {
1429    if read {
1430        quote! {
1431            let _p_dyn = _p_stream.read_dyn_int()?;
1432        }
1433    } else {
1434        quote! {
1435            _p_stream.write_dyn_int(_p_dyn);
1436        }
1437    }
1438}