|
| 1 | +use darling::ast::Data; |
| 2 | +use darling::{FromDeriveInput, FromField, FromVariant}; |
| 3 | +use proc_macro2::{Ident, Span, TokenStream}; |
| 4 | +use quote::quote; |
| 5 | +use syn::{ |
| 6 | + AngleBracketedGenericArguments, DeriveInput, Error, GenericArgument, PathArguments, Type, |
| 7 | + TypePath, |
| 8 | +}; |
| 9 | + |
| 10 | +#[derive(Debug, FromDeriveInput)] |
| 11 | +#[darling(attributes(record))] |
| 12 | +struct SerializationOpts { |
| 13 | + ident: Ident, |
| 14 | + data: Data<SerializationVariantOpts, SerializationFieldOpt>, |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Debug, FromVariant)] |
| 18 | +#[darling(attributes(record))] |
| 19 | +struct SerializationVariantOpts { |
| 20 | + ident: Ident, |
| 21 | + fields: darling::ast::Fields<SerializationFieldOpt>, |
| 22 | +} |
| 23 | + |
| 24 | +#[derive(Debug, FromField)] |
| 25 | +#[darling(attributes(record))] |
| 26 | +struct SerializationFieldOpt { |
| 27 | + ident: Option<Ident>, |
| 28 | + ty: Type, |
| 29 | +} |
| 30 | + |
| 31 | +fn process_type(ty: &Type) -> TokenStream { |
| 32 | + if let Type::Path(TypePath { path, .. }) = ty { |
| 33 | + let ident = &path.segments.last().unwrap().ident; |
| 34 | + |
| 35 | + match ident.to_string().as_str() { |
| 36 | + "Vec" | "Option" | "Arc" | "Box" | "PhantomData" | "Bound" | "CountMinSketch" => { |
| 37 | + if let PathArguments::AngleBracketed(AngleBracketedGenericArguments { |
| 38 | + args, .. |
| 39 | + }) = &path.segments.last().unwrap().arguments |
| 40 | + { |
| 41 | + if let Some(GenericArgument::Type(inner_ty)) = args.first() { |
| 42 | + let inner_processed = process_type(inner_ty); |
| 43 | + |
| 44 | + return quote! { |
| 45 | + #ident::<#inner_processed> |
| 46 | + }; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + _ => {} |
| 51 | + } |
| 52 | + |
| 53 | + quote! { #ty } |
| 54 | + } else { |
| 55 | + quote! { #ty } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +pub(crate) fn handle(ast: DeriveInput) -> Result<TokenStream, Error> { |
| 60 | + let record_opts: SerializationOpts = SerializationOpts::from_derive_input(&ast)?; |
| 61 | + let struct_name = &record_opts.ident; |
| 62 | + |
| 63 | + Ok(match record_opts.data { |
| 64 | + Data::Struct(data_struct) => { |
| 65 | + let mut encode_fields: Vec<TokenStream> = Vec::new(); |
| 66 | + let mut decode_fields: Vec<TokenStream> = Vec::new(); |
| 67 | + let mut init_fields: Vec<TokenStream> = Vec::new(); |
| 68 | + let mut is_tuple = false; |
| 69 | + |
| 70 | + for (i, field_opts) in data_struct.fields.into_iter().enumerate() { |
| 71 | + is_tuple = is_tuple || field_opts.ident.is_none(); |
| 72 | + |
| 73 | + let field_name = field_opts |
| 74 | + .ident |
| 75 | + .unwrap_or_else(|| Ident::new(&format!("filed_{}", i), Span::call_site())); |
| 76 | + let ty = process_type(&field_opts.ty); |
| 77 | + |
| 78 | + encode_fields.push(quote! { |
| 79 | + #field_name.encode(writer, is_direct, reference_tables)?; |
| 80 | + }); |
| 81 | + decode_fields.push(quote! { |
| 82 | + let #field_name = #ty::decode(reader, drive, reference_tables)?; |
| 83 | + }); |
| 84 | + init_fields.push(quote! { |
| 85 | + #field_name, |
| 86 | + }) |
| 87 | + } |
| 88 | + let init_stream = if is_tuple { |
| 89 | + quote! { #struct_name ( #(#init_fields)* ) } |
| 90 | + } else { |
| 91 | + quote! { #struct_name { #(#init_fields)* } } |
| 92 | + }; |
| 93 | + |
| 94 | + quote! { |
| 95 | + impl crate::serdes::ReferenceSerialization for #struct_name { |
| 96 | + fn encode<W: std::io::Write>( |
| 97 | + &self, |
| 98 | + writer: &mut W, |
| 99 | + is_direct: bool, |
| 100 | + reference_tables: &mut crate::serdes::ReferenceTables, |
| 101 | + ) -> Result<(), crate::errors::DatabaseError> { |
| 102 | + let #init_stream = self; |
| 103 | + |
| 104 | + #(#encode_fields)* |
| 105 | + |
| 106 | + Ok(()) |
| 107 | + } |
| 108 | + |
| 109 | + fn decode<T: crate::storage::Transaction, R: std::io::Read>( |
| 110 | + reader: &mut R, |
| 111 | + drive: Option<(&T, &crate::storage::TableCache)>, |
| 112 | + reference_tables: &crate::serdes::ReferenceTables, |
| 113 | + ) -> Result<Self, crate::errors::DatabaseError> { |
| 114 | + #(#decode_fields)* |
| 115 | + |
| 116 | + Ok(#init_stream) |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + Data::Enum(data_enum) => { |
| 122 | + let mut variant_encode_fields: Vec<TokenStream> = Vec::new(); |
| 123 | + let mut variant_decode_fields: Vec<TokenStream> = Vec::new(); |
| 124 | + |
| 125 | + for (i, variant_opts) in data_enum.into_iter().enumerate() { |
| 126 | + let i = i as u8; |
| 127 | + let mut encode_fields: Vec<TokenStream> = Vec::new(); |
| 128 | + let mut decode_fields: Vec<TokenStream> = Vec::new(); |
| 129 | + let mut init_fields: Vec<TokenStream> = Vec::new(); |
| 130 | + let enum_name = variant_opts.ident; |
| 131 | + let mut is_tuple = false; |
| 132 | + |
| 133 | + for (i, field_opts) in variant_opts.fields.into_iter().enumerate() { |
| 134 | + is_tuple = is_tuple || field_opts.ident.is_none(); |
| 135 | + |
| 136 | + let field_name = field_opts |
| 137 | + .ident |
| 138 | + .unwrap_or_else(|| Ident::new(&format!("filed_{}", i), Span::call_site())); |
| 139 | + let ty = process_type(&field_opts.ty); |
| 140 | + |
| 141 | + encode_fields.push(quote! { |
| 142 | + #field_name.encode(writer, is_direct, reference_tables)?; |
| 143 | + }); |
| 144 | + decode_fields.push(quote! { |
| 145 | + let #field_name = #ty::decode(reader, drive, reference_tables)?; |
| 146 | + }); |
| 147 | + init_fields.push(quote! { |
| 148 | + #field_name, |
| 149 | + }) |
| 150 | + } |
| 151 | + |
| 152 | + let init_stream = if is_tuple { |
| 153 | + quote! { #struct_name::#enum_name ( #(#init_fields)* ) } |
| 154 | + } else { |
| 155 | + quote! { #struct_name::#enum_name { #(#init_fields)* } } |
| 156 | + }; |
| 157 | + variant_encode_fields.push(quote! { |
| 158 | + #init_stream => { |
| 159 | + std::io::Write::write_all(writer, &[#i])?; |
| 160 | + |
| 161 | + #(#encode_fields)* |
| 162 | + } |
| 163 | + }); |
| 164 | + variant_decode_fields.push(quote! { |
| 165 | + #i => { |
| 166 | + #(#decode_fields)* |
| 167 | + |
| 168 | + #init_stream |
| 169 | + } |
| 170 | + }); |
| 171 | + } |
| 172 | + |
| 173 | + quote! { |
| 174 | + impl crate::serdes::ReferenceSerialization for #struct_name { |
| 175 | + fn encode<W: std::io::Write>( |
| 176 | + &self, |
| 177 | + writer: &mut W, |
| 178 | + is_direct: bool, |
| 179 | + reference_tables: &mut crate::serdes::ReferenceTables, |
| 180 | + ) -> Result<(), crate::errors::DatabaseError> { |
| 181 | + match self { |
| 182 | + #(#variant_encode_fields)* |
| 183 | + } |
| 184 | + |
| 185 | + Ok(()) |
| 186 | + } |
| 187 | + |
| 188 | + fn decode<T: crate::storage::Transaction, R: std::io::Read>( |
| 189 | + reader: &mut R, |
| 190 | + drive: Option<(&T, &crate::storage::TableCache)>, |
| 191 | + reference_tables: &crate::serdes::ReferenceTables, |
| 192 | + ) -> Result<Self, crate::errors::DatabaseError> { |
| 193 | + let mut type_bytes = [0u8; 1]; |
| 194 | + std::io::Read::read_exact(reader, &mut type_bytes)?; |
| 195 | + |
| 196 | + Ok(match type_bytes[0] { |
| 197 | + #(#variant_decode_fields)* |
| 198 | + _ => unreachable!(), |
| 199 | + }) |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + }) |
| 205 | +} |
0 commit comments