|
| 1 | +use proc_macro2::{TokenStream, Ident}; |
| 2 | +use quote::{quote, quote_spanned, format_ident}; |
| 3 | +use syn::{parse_macro_input, spanned::Spanned, Data, DeriveInput, Fields, Generics, punctuated::Iter, Field, Index}; |
| 4 | + |
| 5 | +#[proc_macro_derive(SerializableState)] |
| 6 | +pub fn derive_serializable_state(input: proc_macro::TokenStream) -> proc_macro::TokenStream { |
| 7 | + let input = parse_macro_input!(input as DeriveInput); |
| 8 | + let struct_name = input.ident; |
| 9 | + let serialized_state_size = generate_serializable_state_size(&input.data); |
| 10 | + let serialize_logic = generate_serialize_logic(&input.data); |
| 11 | + let deserialize_logic = generate_deserialize_logic(&input.data); |
| 12 | + |
| 13 | + check_generics(&input.generics); |
| 14 | + |
| 15 | + let expanded = quote! { |
| 16 | + impl crypto_common::SerializableState for #struct_name { |
| 17 | + type SerializedStateSize = #serialized_state_size; |
| 18 | + |
| 19 | + fn serialize(&self) -> crypto_common::SerializedState<Self> { |
| 20 | + use crypto_common::generic_array::sequence::Concat; |
| 21 | + use crypto_common::SerializableState; |
| 22 | + |
| 23 | + #serialize_logic |
| 24 | + } |
| 25 | + |
| 26 | + fn deserialize(_serialized_state: &crypto_common::SerializedState<Self>) -> Result<Self, crypto_common::DeserializeStateError> { |
| 27 | + use crypto_common::generic_array::sequence::Split; |
| 28 | + use crypto_common::SerializableState; |
| 29 | + |
| 30 | + #deserialize_logic |
| 31 | + } |
| 32 | + } |
| 33 | + }; |
| 34 | + |
| 35 | + proc_macro::TokenStream::from(expanded) |
| 36 | +} |
| 37 | + |
| 38 | +fn check_generics(generics: &Generics) { |
| 39 | + if generics.params.iter().next().is_some() { |
| 40 | + panic!("Generics are not supported yet. Please implement SerializableState on your own.") |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +fn generate_serializable_state_size(data: &Data) -> TokenStream { |
| 45 | + match *data { |
| 46 | + Data::Struct(ref data) => match data.fields { |
| 47 | + Fields::Named(ref fields) => serializable_state_size_from_fields(fields.named.iter()), |
| 48 | + Fields::Unnamed(ref fields) => serializable_state_size_from_fields(fields.unnamed.iter()), |
| 49 | + Fields::Unit => quote! { crypto_common::typenum::U0 } |
| 50 | + }, |
| 51 | + Data::Enum(_) | Data::Union(_) => unimplemented!(), |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +fn generate_serialize_logic(data: &Data) -> TokenStream { |
| 56 | + match *data { |
| 57 | + Data::Struct(ref data) => match data.fields { |
| 58 | + Fields::Named(ref fields) => serialize_logic_from_fields(fields.named.iter()), |
| 59 | + Fields::Unnamed(ref fields) => serialize_logic_from_fields(fields.unnamed.iter()), |
| 60 | + Fields::Unit => quote! { crypto_common::SerializedState::<Self>::default() } |
| 61 | + }, |
| 62 | + Data::Enum(_) | Data::Union(_) => unimplemented!(), |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +fn generate_deserialize_logic(data: &Data) -> TokenStream { |
| 67 | + match *data { |
| 68 | + Data::Struct(ref data) => match data.fields { |
| 69 | + Fields::Named(ref fields) => deserialize_logic_from_fields(fields.named.iter(), true), |
| 70 | + Fields::Unnamed(ref fields) => deserialize_logic_from_fields(fields.unnamed.iter(), false), |
| 71 | + Fields::Unit => quote! { Ok(Self {}) } |
| 72 | + }, |
| 73 | + Data::Enum(_) | Data::Union(_) => unimplemented!(), |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +fn serializable_state_size_from_fields(mut fields: Iter<Field>) -> TokenStream { |
| 78 | + match fields.next() { |
| 79 | + None => quote! { crypto_common::typenum::U0 }, |
| 80 | + Some(first) => { |
| 81 | + let ty = &first.ty; |
| 82 | + let mut size = quote_spanned! { first.span() => <#ty as crypto_common::SerializableState>::SerializedStateSize }; |
| 83 | + fields.for_each(|field| { |
| 84 | + let ty = &field.ty; |
| 85 | + size = quote_spanned! { |
| 86 | + field.span() => crypto_common::typenum::Sum<<#ty as crypto_common::SerializableState>::SerializedStateSize, #size> |
| 87 | + }; |
| 88 | + }); |
| 89 | + size |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +fn serialize_logic_from_fields(mut fields: Iter<Field>) -> TokenStream { |
| 95 | + match fields.next() { |
| 96 | + None => quote! { crypto_common::SerializedState::<Self>::default() }, |
| 97 | + Some(first) => { |
| 98 | + let field_name = get_field_name(0, &first.ident, None); |
| 99 | + let mut code = quote! { self.#field_name.serialize() }; |
| 100 | + fields.enumerate().for_each(|(i, field)| { |
| 101 | + let field_name = get_field_name(i + 1, &field.ident, None); |
| 102 | + code = quote_spanned! { field.span() => #code.concat(self.#field_name.serialize()) }; |
| 103 | + }); |
| 104 | + code |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +fn deserialize_logic_from_fields(fields: Iter<Field>, named: bool) -> TokenStream { |
| 110 | + let mut skip_first = fields.clone(); |
| 111 | + match skip_first.next() { |
| 112 | + None => quote! { Ok(Self {}) }, |
| 113 | + Some(first) => { |
| 114 | + let mut code = quote!(); |
| 115 | + fields.enumerate().for_each(|(i, field)| { |
| 116 | + let field_name = get_field_name(i, &field.ident, Some("serialized_")); |
| 117 | + let ty = &field.ty; |
| 118 | + code = quote_spanned! { |
| 119 | + field.span() => |
| 120 | + #code |
| 121 | + let (#field_name, _serialized_state) = Split::<_, <#ty as SerializableState>::SerializedStateSize>::split(_serialized_state); |
| 122 | + let #field_name = <#ty>::deserialize(#field_name)?; |
| 123 | + }; |
| 124 | + }); |
| 125 | + let first = get_field_name(0, &first.ident, Some("serialized_")); |
| 126 | + let recurce = skip_first.enumerate().map(|(i, field)| { |
| 127 | + let field_name = get_field_name(i + 1, &field.ident, Some("serialized_")); |
| 128 | + quote_spanned! { field.span() => #field_name } |
| 129 | + }); |
| 130 | + |
| 131 | + let construction = if named { |
| 132 | + quote! { Self { #first #(, #recurce)* } } |
| 133 | + } else { |
| 134 | + quote! { Self ( #first #(, #recurce)* ) } |
| 135 | + }; |
| 136 | + |
| 137 | + quote! { |
| 138 | + #code |
| 139 | + |
| 140 | + Ok(#construction) |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +fn get_field_name(i: usize, ident: &Option<Ident>, unnamed_prefix: Option<&str>) -> TokenStream { |
| 147 | + match ident { |
| 148 | + Some(ident) => quote!{ #ident }, |
| 149 | + None => { |
| 150 | + match unnamed_prefix { |
| 151 | + None => { |
| 152 | + let index = Index::from(i); |
| 153 | + quote! { #index } |
| 154 | + }, |
| 155 | + Some(unnamed_prefix) => { |
| 156 | + let ident = format_ident!("{}{}", unnamed_prefix, i); |
| 157 | + quote! { #ident } |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | +} |
| 163 | + |
0 commit comments