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 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 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 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 _ => {} }
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 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 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 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 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 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 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 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 for attr in ast.attrs.iter() {
350 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 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 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 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 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 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 _ => {
753 let size_key = generate_size_key(length_by, has_dynamic_length).1;
754
755 let variant_code = if variant_by.is_some() {
756 quote! {
757 _p_config.discriminator = _p_config.get_variant(#variant_by);
758 }
759 } else if multi_enum {
760 let config_multi = if !direct_collection_child {
761 quote! { #ident::configure_multi_disc(_p_config); }
762 } else {
763 quote! {}
764 };
765
766 quote! {
767 #config_multi
768 _p_config.discriminator = _p_config.get_next_multi_disc(stringify!(#field_name), #ident_name);
769 }
770 } else {
771 quote! {
772 _p_config.discriminator = None;
773 }
774 };
775
776 if read {
777 quote! {
778 #variant_code
779 let _p_val = binary_codec::utils::read_object(_p_stream, #size_key, _p_config)?;
780 }
781 } else {
782 quote! {
783 #variant_code
784 binary_codec::utils::write_object(_p_val, #size_key, _p_stream, _p_config)?;
785 }
786 }
787 }
788 }
789 } else {
790 if path.segments.len() == 1 {
792 let ident = &path.segments[0].ident;
793 let ident_name = ident.to_string();
794
795 match ident_name.as_ref() {
796 "RefCell" => {
797 let inner_type = get_inner_type(path).expect("Option missing inner type");
798 let handle = generate_code_for_handling_field(
799 read,
800 inner_type,
801 field_name,
802 bits_count,
803 None,
804 None,
805 variant_by,
806 length_by,
807 is_dynamic_int,
808 has_dynamic_length,
809 key_dyn_length,
810 val_dyn_length,
811 multi_enum,
812 false,
813 level + 1,
814 );
815
816 if read {
817 quote! {
818 #handle
819 let _p_val = RefCell::new(_p_val);
820 }
821 } else {
822 quote! {
823 let _p_val = &*_p_val.borrow();
824 #handle
825 }
826 }
827 }
828 "Option" => {
829 let inner_type = get_inner_type(path).expect("Option missing inner type");
830 let handle = generate_code_for_handling_field(
831 read,
832 inner_type,
833 field_name,
834 bits_count,
835 None,
836 None,
837 variant_by,
838 length_by,
839 is_dynamic_int,
840 has_dynamic_length,
841 key_dyn_length,
842 val_dyn_length,
843 multi_enum,
844 false,
845 level + 1,
846 );
847
848 let option_name: syn::Ident = format_ident!("__option_{}", level);
849
850 if let Some(toggled_by) = toggled_by {
851 let toggled_by = quote! {
853 _p_config.get_toggle(#toggled_by).unwrap_or(false)
854 };
855
856 if read {
857 quote! {
858 let mut #option_name: Option<#inner_type> = None;
859 if #toggled_by {
860 #handle
861 #option_name = Some(_p_val);
862 }
863 let _p_val = #option_name;
864 }
865 } else {
866 quote! {
867 if #toggled_by {
868 let _p_val = _p_val.as_ref().expect("Expected Some value, because toggled_by field is true");
869 #handle
870 }
871 }
872 }
873 } else if let Some(toggled_by_variant) = toggled_by_variant {
874 let toggled_by = quote! {
876 _p_config.get_variant_toggle(#toggled_by_variant).unwrap_or(false)
877 };
878
879 if read {
880 quote! {
881 let mut #option_name: Option<#inner_type> = None;
882 if #toggled_by {
883 #handle
884 #option_name = Some(_p_val);
885 }
886 let _p_val = #option_name;
887 }
888 } else {
889 quote! {
890 if #toggled_by {
891 let _p_val = _p_val.as_ref().expect("Expected Some value, because toggled_by_variant field evalutates to true");
892 #handle
893 }
894 }
895 }
896 } else {
897 if read {
899 quote! {
900 let mut #option_name: Option<#inner_type> = None;
901 if _p_stream.bytes_left() > 0 {
902 #handle
903 #option_name = Some(_p_val);
904 }
905 let _p_val = #option_name;
906 }
907 } else {
908 quote! {
909 if let Some(_p_val) = _p_val.as_ref() {
910 #handle
911 }
912 }
913 }
914 }
915 }
916 "Vec" => {
917 let vec_name = format_ident!("__val_{}", level);
918 let inner_type = get_inner_type(path).expect("Vec missing inner type");
919
920 if let Type::Path(inner_path) = inner_type {
922 if let Some(inner_ident) = inner_path.path.get_ident() {
923 if inner_ident == "u8" {
924 let (has_size, size_key) =
925 generate_size_key(length_by, has_dynamic_length);
926
927 if read {
928 if has_size || multi_enum {
929 let len_code = if multi_enum {
931 quote! {
932 let _p_len = _p_config.get_multi_disc_size("u8");
934 }
935 } else {
936 quote! {
937 let _p_len = binary_codec::utils::get_read_size(_p_stream, #size_key, _p_config)?;
938 }
939 };
940
941 return quote! {
942 #len_code
943 let _p_val = _p_stream.read_bytes(_p_len)?.to_vec();
944 };
945 } else {
946 return quote! {
948 let _p_len = _p_stream.bytes_left();
949 let _p_val = _p_stream.read_bytes(_p_len)?.to_vec();
950 };
951 }
952 } else {
953 let write_size = if has_size {
955 quote! {
956 let _p_len = _p_val.len();
957 binary_codec::utils::write_size(_p_len, #size_key, _p_stream, _p_config)?;
958 }
959 } else {
960 quote! {}
961 };
962
963 return quote! {
964 #write_size
965 _p_stream.write_bytes(_p_val);
966 };
967 }
968 }
969 }
970 }
971
972 let handle = generate_code_for_handling_field(
974 read,
975 inner_type,
976 field_name,
977 bits_count,
978 None,
979 None,
980 None,
981 None,
982 is_dynamic_int,
983 val_dyn_length,
984 false,
985 false,
986 multi_enum,
987 true,
988 level + 1,
989 );
990
991 let (has_size, size_key) = generate_size_key(length_by, has_dynamic_length);
992
993 let write_code = quote! {
994 for _p_val in _p_val {
995 #handle
996 }
997 };
998
999 if has_size || (read && multi_enum) {
1000 if read {
1001 let len_code = if multi_enum && let Type::Path(path) = inner_type {
1002 let enum_ident = path
1003 .path
1004 .get_ident()
1005 .expect("Expected ident for multi_enum inner type");
1006 quote! {
1007 #enum_ident::configure_multi_disc(_p_config);
1008 let _p_len = _p_config.get_multi_disc_size(stringify!(#enum_ident));
1009 }
1010 } else {
1011 quote! {
1012 let _p_len = binary_codec::utils::get_read_size(_p_stream, #size_key, _p_config)?;
1013 }
1014 };
1015
1016 quote! {
1017 #len_code
1018 let mut #vec_name = Vec::<#inner_type>::with_capacity(_p_len);
1019 for _ in 0.._p_len {
1020 #handle
1021 #vec_name.push(_p_val);
1022 }
1023 let _p_val = #vec_name;
1024 }
1025 } else {
1026 quote! {
1027 let _p_len = _p_val.len();
1028 binary_codec::utils::write_size(_p_len, #size_key, _p_stream, _p_config)?;
1029 #write_code
1030 }
1031 }
1032 } else {
1033 if read {
1034 quote! {
1035 let mut #vec_name = Vec::<#inner_type>::new();
1036 while _p_stream.bytes_left() > 0 {
1037 #handle
1038 #vec_name.push(_p_val);
1039 }
1040 let _p_val = #vec_name;
1041 }
1042 } else {
1043 quote! {
1044 #write_code
1045 }
1046 }
1047 }
1048 }
1049 "HashMap" => {
1050 let (key_type, value_type) =
1051 get_two_types(path).expect("Failed to get HashMap types");
1052
1053 let handle_key = generate_code_for_handling_field(
1054 read,
1055 key_type,
1056 field_name,
1057 None,
1058 None,
1059 None,
1060 None,
1061 None,
1062 is_dynamic_int,
1063 key_dyn_length,
1064 false,
1065 false,
1066 false,
1067 false,
1068 level + 1,
1069 );
1070
1071 let handle_value = generate_code_for_handling_field(
1072 read,
1073 value_type,
1074 field_name,
1075 None,
1076 None,
1077 None,
1078 None,
1079 None,
1080 is_dynamic_int,
1081 val_dyn_length,
1082 false,
1083 false,
1084 false,
1085 false,
1086 level + 1,
1087 );
1088
1089 let (has_size, size_key) = generate_size_key(length_by, has_dynamic_length);
1090
1091 let write_code = quote! {
1092 for (key, value) in _p_val {
1093 let _p_val = key;
1094 #handle_key
1095 let _p_val = value;
1096 #handle_value
1097 }
1098 };
1099
1100 if read {
1101 if has_size {
1102 quote! {
1103 let _p_len = binary_codec::utils::get_read_size(_p_stream, #size_key, _p_config)?;
1104 let mut _p_map = std::collections::HashMap::<#key_type, #value_type>::with_capacity(_p_len);
1105 for _ in 0.._p_len {
1106 let _p_key;
1107 #handle_key
1108 _p_key = _p_val;
1109 let _p_value;
1110 #handle_value
1111 _p_value = _p_val;
1112 _p_map.insert(_p_key, _p_value);
1113 }
1114 let _p_val = _p_map;
1115 }
1116 } else {
1117 quote! {
1118 let mut _p_map = std::collections::HashMap::<#key_type, #value_type>::new();
1119 while _p_stream.bytes_left() > 0 {
1120 let _p_key;
1121 #handle_key
1122 _p_key = _p_val;
1123 let _p_value;
1124 #handle_value
1125 _p_value = _p_val;
1126 _p_map.insert(_p_key, _p_value);
1127 }
1128 let _p_val = _p_map;
1129 }
1130 }
1131 } else {
1132 if has_size {
1133 quote! {
1134 let _p_len = _p_val.len();
1135 binary_codec::utils::write_size(_p_len, #size_key, _p_stream, _p_config)?;
1136 #write_code
1137 }
1138 } else {
1139 quote! {
1140 #write_code
1141 }
1142 }
1143 }
1144 }
1145 _ => {
1146 panic!("Type not implemented")
1147 }
1148 }
1149 } else {
1150 panic!("Multi-segment paths are not supported");
1151 }
1152 }
1153 } else if let Type::Array(array) = field_type {
1154 let len: usize = if let syn::Expr::Lit(ref arr_len_lit) = array.len {
1155 if let Lit::Int(ref lit_int) = arr_len_lit.lit {
1156 lit_int
1157 .base10_parse()
1158 .expect("Failed to parse literal to usize")
1159 } else {
1160 panic!("Expected an int to determine array length");
1161 }
1162 } else {
1163 panic!("Expected literal to determine array length");
1164 };
1165
1166 let array_type = &*array.elem;
1167 if let Type::Path(at_path) = array_type {
1169 if let Some(at_ident) = at_path.path.get_ident() {
1170 if at_ident == "u8" {
1171 if read {
1172 quote! {
1173 let _p_slice = _p_stream.read_bytes(#len)?;
1174 let _p_val = <[u8; #len]>::try_from(_p_slice).expect("Failed to convert slice to array");
1175 }
1176 } else {
1177 quote! {
1178 _p_stream.write_bytes(_p_val);
1179 }
1180 }
1181 } else {
1182 let handle = generate_code_for_handling_field(
1183 read,
1184 array_type,
1185 field_name,
1186 bits_count,
1187 None,
1188 None,
1189 None,
1190 None,
1191 is_dynamic_int,
1192 val_dyn_length,
1193 false,
1194 false,
1195 false,
1196 true,
1197 level + 1,
1198 );
1199
1200 let array_name = format_ident!("__val_{}", level);
1201
1202 if read {
1203 quote! {
1204 let mut #array_name = Vec::<#array_type>::with_capacity(#len);
1205 for _ in 0..#len {
1206 #handle;
1207 #array_name.push(_p_val);
1208 }
1209 let _p_val = TryInto::<[#array_type; #len]>::try_into(#array_name).expect("Failed to convert Vec to array");
1210 }
1211 } else {
1212 quote! {
1213 for _p_val in _p_val {
1214 #handle
1215 }
1216 }
1217 }
1218 }
1219 } else {
1220 let handle = generate_code_for_handling_field(
1222 read,
1223 array_type,
1224 field_name,
1225 bits_count,
1226 None,
1227 None,
1228 None,
1229 None,
1230 is_dynamic_int,
1231 val_dyn_length,
1232 false,
1233 false,
1234 false,
1235 true,
1236 level + 1,
1237 );
1238
1239 let array_name = format_ident!("__val_{}", level);
1240
1241 if read {
1242 quote! {
1243 let mut #array_name = Vec::<#array_type>::with_capacity(#len);
1244 for _ in 0..#len {
1245 #handle;
1246 #array_name.push(_p_val);
1247 }
1248 let _p_val = TryInto::<[#array_type; #len]>::try_into(#array_name).expect("Failed to convert Vec to array");
1249 }
1250 } else {
1251 quote! {
1252 for _p_val in _p_val {
1253 #handle
1254 }
1255 }
1256 }
1257 }
1258 } else {
1259 panic!("Unsupported array element type");
1260 }
1261 } else {
1262 panic!("Field type of '{:?}' not supported", field_name);
1263 }
1264}
1265
1266fn generate_error_type(read: bool, attrs: &[Attribute]) -> proc_macro2::TokenStream {
1267 if let Some(custom) = get_custom_error_type(read, attrs) {
1268 return custom;
1269 }
1270
1271 if read {
1272 quote! { binary_codec::DeserializationError }
1273 } else {
1274 quote! { binary_codec::SerializationError }
1275 }
1276}
1277
1278fn get_custom_error_type(read: bool, attrs: &[Attribute]) -> Option<proc_macro2::TokenStream> {
1279 let specific = if read {
1280 "codec_de_error"
1281 } else {
1282 "codec_ser_error"
1283 };
1284
1285 let specific_value = attrs
1286 .iter()
1287 .find(|attr| attr.path().is_ident(specific))
1288 .and_then(get_string_value_from_attribute);
1289
1290 if let Some(value) = specific_value {
1291 return Some(parse_error_type(&value));
1292 }
1293
1294 let common_value = attrs
1295 .iter()
1296 .find(|attr| attr.path().is_ident("codec_error"))
1297 .and_then(get_string_value_from_attribute);
1298
1299 common_value.map(|value| parse_error_type(&value))
1300}
1301
1302fn parse_error_type(value: &str) -> proc_macro2::TokenStream {
1303 let ty: Type = syn::parse_str(value).expect("Invalid error type for codec_error");
1304 quote! { #ty }
1305}
1306
1307fn generate_size_key(
1308 length_by: Option<String>,
1309 has_dynamic_length: bool,
1310) -> (bool, proc_macro2::TokenStream) {
1311 if let Some(length_by) = length_by.as_ref() {
1312 (true, quote! { Some(#length_by) })
1313 } else if has_dynamic_length {
1314 (true, quote! { Some("__dynamic") })
1315 } else {
1316 (false, quote! { None })
1317 }
1318}
1319
1320fn get_string_value_from_attribute(attr: &Attribute) -> Option<String> {
1321 match &attr.meta {
1322 syn::Meta::Path(_) => None,
1323 syn::Meta::List(list_value) => {
1324 for token in list_value.tokens.clone().into_iter() {
1326 if let proc_macro2::TokenTree::Literal(lit) = token {
1327 return Some(lit.to_string().trim_matches('"').to_string());
1328 }
1329 }
1330
1331 None
1332 }
1333 syn::Meta::NameValue(name_value) => {
1334 if let syn::Expr::Lit(lit_expr) = &name_value.value {
1335 if let Lit::Str(lit_str) = &lit_expr.lit {
1336 return Some(lit_str.value());
1337 }
1338 }
1339
1340 None
1341 }
1342 }
1343}
1344
1345fn get_int_value_from_attribute(attr: &Attribute) -> Option<i32> {
1346 match &attr.meta {
1347 syn::Meta::Path(_) => None,
1348 syn::Meta::List(list_value) => {
1349 for token in list_value.tokens.clone().into_iter() {
1351 if let proc_macro2::TokenTree::Literal(lit) = token {
1352 if let Ok(val) = lit.to_string().parse::<i32>() {
1353 return Some(val);
1354 }
1355 }
1356 }
1357
1358 None
1359 }
1360 syn::Meta::NameValue(name_value) => {
1361 if let syn::Expr::Lit(lit_expr) = &name_value.value {
1362 if let Lit::Int(lit_int) = &lit_expr.lit {
1363 return Some(lit_int.base10_parse().expect("Not a valid int value"));
1364 }
1365 }
1366
1367 None
1368 }
1369 }
1370}
1371
1372fn get_inner_type(path: &syn::Path) -> Option<&syn::Type> {
1373 if let Some(PathArguments::AngleBracketed(args)) =
1374 path.segments.last().map(|seg| &seg.arguments)
1375 {
1376 if let Some(arg) = args.args.first() {
1377 if let syn::GenericArgument::Type(inner_type) = arg {
1378 return Some(inner_type);
1379 }
1380 }
1381 }
1382
1383 None
1384}
1385
1386fn get_two_types(path: &syn::Path) -> Option<(&syn::Type, &syn::Type)> {
1387 if let Some(PathArguments::AngleBracketed(args)) =
1388 path.segments.last().map(|seg| &seg.arguments)
1389 {
1390 let mut types = args.args.iter().filter_map(|arg| {
1391 if let syn::GenericArgument::Type(inner_type) = arg {
1392 Some(inner_type)
1393 } else {
1394 None
1395 }
1396 });
1397
1398 if let (Some(t1), Some(t2)) = (types.next(), types.next()) {
1399 return Some((t1, t2));
1400 }
1401 }
1402
1403 None
1404}
1405
1406fn generate_dynint(read: bool) -> proc_macro2::TokenStream {
1407 if read {
1408 quote! {
1409 let _p_dyn = _p_stream.read_dyn_int()?;
1410 }
1411 } else {
1412 quote! {
1413 _p_stream.write_dyn_int(_p_dyn);
1414 }
1415 }
1416}