| 1 | use proc_macro2::TokenStream; |
| 2 | use quote::quote; |
| 3 | use syn::Visibility; |
| 4 | |
| 5 | use crate::generation::common::generate_setting_fields_from_bits_tokens; |
| 6 | use crate::parsing::bitfield_attribute::{BitfieldAttribute, Endian}; |
| 7 | use crate::parsing::bitfield_field::BitfieldField; |
| 8 | |
| 9 | /// Generates the `from_bits` function for the bitfield. |
| 10 | pub(crate) fn generate_from_bits_function_tokens( |
| 11 | vis: Visibility, |
| 12 | fields: &[BitfieldField], |
| 13 | ignored_fields: &[BitfieldField], |
| 14 | bitfield_type: &syn::Type, |
| 15 | bitfield_attribute: &BitfieldAttribute, |
| 16 | ) -> TokenStream { |
| 17 | let setting_fields_from_bits_tokens = generate_setting_fields_from_bits_tokens( |
| 18 | bitfield_type, |
| 19 | fields, |
| 20 | Some(quote! { Self }), |
| 21 | false, |
| 22 | !ignored_fields.is_empty(), |
| 23 | ); |
| 24 | |
| 25 | let swap_bits_endian_tokens = (bitfield_attribute.from_endian == Endian::Little).then(|| { |
| 26 | quote! { |
| 27 | let bits = bits.swap_bytes(); |
| 28 | } |
| 29 | }); |
| 30 | |
| 31 | let ignored_fields_defaults = ignored_fields.iter().map(|field| { |
| 32 | let field_name = &field.name; |
| 33 | let field_ty = &field.ty; |
| 34 | quote! { |
| 35 | #field_name: <#field_ty>::default(), |
| 36 | } |
| 37 | }); |
| 38 | |
| 39 | let initialize_struct_tokens = if !ignored_fields.is_empty() { |
| 40 | quote! { |
| 41 | Self { |
| 42 | val: 0, |
| 43 | #( #ignored_fields_defaults )* |
| 44 | } |
| 45 | } |
| 46 | } else { |
| 47 | quote! { |
| 48 | Self(0) |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | let const_ident_tokens = ignored_fields.is_empty().then(|| quote! { const }); |
| 53 | |
| 54 | quote! { |
| 55 | #[doc = "Creates a new bitfield instance from the given bits."] |
| 56 | #vis #const_ident_tokens fn from_bits(bits: #bitfield_type) -> Self { |
| 57 | #swap_bits_endian_tokens |
| 58 | let mut this = #initialize_struct_tokens; |
| 59 | #setting_fields_from_bits_tokens |
| 60 | this |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /// Generates the `from_bits_with_defaults` function for the bitfield. |
| 66 | pub(crate) fn generate_from_bits_with_defaults_function_tokens( |
| 67 | vis: Visibility, |
| 68 | fields: &[BitfieldField], |
| 69 | bitfield_type: &syn::Type, |
| 70 | bitfield_attribute: &BitfieldAttribute, |
| 71 | ignored_fields_struct: bool, |
| 72 | ) -> TokenStream { |
| 73 | let setting_fields_from_bits_tokens = generate_setting_fields_from_bits_tokens( |
| 74 | bitfield_type, |
| 75 | fields, |
| 76 | Some(quote! { Self }), |
| 77 | true, |
| 78 | ignored_fields_struct, |
| 79 | ); |
| 80 | |
| 81 | let swap_bits_endian_tokens = (bitfield_attribute.from_endian == Endian::Little).then(|| { |
| 82 | quote! { |
| 83 | let bits = bits.swap_bytes(); |
| 84 | } |
| 85 | }); |
| 86 | |
| 87 | let const_ident_tokens = (!ignored_fields_struct).then(|| quote! { const }); |
| 88 | |
| 89 | quote! { |
| 90 | #[doc = "Creates a new bitfield instance from the given bits while respecting field default values."] |
| 91 | #vis #const_ident_tokens fn from_bits_with_defaults(bits: #bitfield_type) -> Self { |
| 92 | #swap_bits_endian_tokens |
| 93 | let mut this = Self::from_bits(bits); |
| 94 | #setting_fields_from_bits_tokens |
| 95 | this |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | pub(crate) fn generate_into_bits_function_tokens( |
| 101 | vis: Visibility, |
| 102 | bitfield_attribute: &BitfieldAttribute, |
| 103 | ignored_fields_struct: bool, |
| 104 | ) -> TokenStream { |
| 105 | let struct_val_ident = if ignored_fields_struct { |
| 106 | quote! { self.val } |
| 107 | } else { |
| 108 | quote! { self.0 } |
| 109 | }; |
| 110 | |
| 111 | let into_bits_impl = match bitfield_attribute.into_endian { |
| 112 | Endian::Big => { |
| 113 | quote! { |
| 114 | #struct_val_ident |
| 115 | } |
| 116 | } |
| 117 | Endian::Little => { |
| 118 | quote! { |
| 119 | #struct_val_ident.swap_bytes() |
| 120 | } |
| 121 | } |
| 122 | }; |
| 123 | |
| 124 | let bitfield_type = &bitfield_attribute.ty; |
| 125 | |
| 126 | quote! { |
| 127 | #[doc = "Returns the bits of the bitfield."] |
| 128 | #vis const fn into_bits(self) -> #bitfield_type { |
| 129 | #into_bits_impl |
| 130 | } |
| 131 | } |
| 132 | } |