1#![deny(clippy::pedantic)]
2
3use itertools::Itertools;
4use proc_macro::TokenStream;
5use proc_macro2::{Ident, TokenStream as TokenStream2};
6use quote::{format_ident, quote};
7use syn::punctuated::Punctuated;
8use syn::token::Comma;
9use syn::{
10 Attribute, Data, DataEnum, DataStruct, DeriveInput, Fields, Index, Lit, Token, Variant,
11 parse_macro_input,
12};
13
14fn is_default_variant_enforce_valid(variant: &Variant) -> bool {
15 let is_default = variant
16 .attrs
17 .iter()
18 .any(|attr| attr.path().is_ident("encodable_default"));
19
20 if is_default {
21 assert_eq!(
22 variant.ident.to_string(),
23 "Default",
24 "Default variant should be called `Default`"
25 );
26 let two_fields = variant.fields.len() == 2;
27 let field_names = variant
28 .fields
29 .iter()
30 .filter_map(|field| field.ident.as_ref().map(ToString::to_string))
31 .sorted()
32 .collect::<Vec<_>>();
33 let correct_fields = field_names == vec!["bytes".to_string(), "variant".to_string()];
34
35 assert!(
36 two_fields && correct_fields,
37 "The default variant should have exactly two field: `variant: u64` and `bytes: Vec<u8>`"
38 );
39 }
40
41 is_default
42}
43
44#[proc_macro_derive(Encodable, attributes(encodable_default, encodable))]
47pub fn derive_encodable(input: TokenStream) -> TokenStream {
48 let DeriveInput {
49 ident,
50 data,
51 generics,
52 ..
53 } = parse_macro_input!(input);
54
55 let encode_inner = match data {
56 Data::Struct(DataStruct { fields, .. }) => derive_struct_encode(&fields),
57 Data::Enum(DataEnum { variants, .. }) => derive_enum_encode(&ident, &variants),
58 Data::Union(_) => error("Encodable can't be derived for unions"),
59 };
60 let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
61
62 let output = quote! {
63 impl #impl_generics ::fedimint_core::encoding::Encodable for #ident #ty_generics #where_clause {
64 #[allow(deprecated)]
65 fn consensus_encode<W: std::io::Write>(&self, mut writer: &mut W) -> std::result::Result<(), std::io::Error> {
66 #encode_inner
67 }
68 }
69 };
70
71 output.into()
72}
73
74fn derive_struct_encode(fields: &Fields) -> TokenStream2 {
75 if is_tuple_struct(fields) {
76 let field_names = fields
78 .iter()
79 .enumerate()
80 .map(|(idx, _)| Index::from(idx))
81 .collect::<Vec<_>>();
82 quote! {
83 #(::fedimint_core::encoding::Encodable::consensus_encode(&self.#field_names, writer)?;)*
84 Ok(())
85 }
86 } else {
87 let field_names = fields
89 .iter()
90 .map(|field| field.ident.clone().unwrap())
91 .collect::<Vec<_>>();
92 quote! {
93 #(::fedimint_core::encoding::Encodable::consensus_encode(&self.#field_names, writer)?;)*
94 Ok(())
95 }
96 }
97}
98
99fn parse_index_attribute(attributes: &[Attribute]) -> Option<u64> {
102 attributes.iter().find_map(|attr| {
103 if attr.path().is_ident("encodable") {
104 attr.parse_args_with(|input: syn::parse::ParseStream| {
105 input.parse::<syn::Ident>()?.span(); input.parse::<Token![=]>()?; if let Lit::Int(lit_int) = input.parse::<Lit>()? {
108 lit_int.base10_parse()
109 } else {
110 Err(input.error("Expected an integer for 'index'"))
111 }
112 })
113 .ok()
114 } else {
115 None
116 }
117 })
118}
119
120fn extract_variants_with_indices(input_variants: Vec<Variant>) -> Vec<(Option<u64>, Variant)> {
123 input_variants
124 .into_iter()
125 .map(|variant| {
126 let index = parse_index_attribute(&variant.attrs);
127 (index, variant)
128 })
129 .collect()
130}
131
132fn non_default_variant_indices(variants: &Punctuated<Variant, Comma>) -> Vec<(u64, Variant)> {
133 let non_default_variants = variants
134 .into_iter()
135 .filter(|variant| !is_default_variant_enforce_valid(variant))
136 .cloned()
137 .collect::<Vec<_>>();
138
139 let attr_indices = extract_variants_with_indices(non_default_variants.clone());
140
141 let all_have_index = attr_indices.iter().all(|(idx, _)| idx.is_some());
142 let none_have_index = attr_indices.iter().all(|(idx, _)| idx.is_none());
143
144 assert!(
145 all_have_index || none_have_index,
146 "Either all or none of the variants should have an index annotation"
147 );
148
149 if all_have_index {
150 attr_indices
151 .into_iter()
152 .map(|(idx, variant)| (idx.expect("We made sure everything has an index"), variant))
153 .collect()
154 } else {
155 non_default_variants
156 .into_iter()
157 .enumerate()
158 .map(|(idx, variant)| (idx as u64, variant))
159 .collect()
160 }
161}
162
163fn derive_enum_encode(ident: &Ident, variants: &Punctuated<Variant, Comma>) -> TokenStream2 {
164 if variants.is_empty() {
165 return quote! {
166 match *self {}
167 };
168 }
169
170 let non_default_match_arms =
171 non_default_variant_indices(variants)
172 .into_iter()
173 .map(|(variant_idx, variant)| {
174 let variant_ident = variant.ident.clone();
175
176 if is_tuple_struct(&variant.fields) {
177 let variant_fields = variant
178 .fields
179 .iter()
180 .enumerate()
181 .map(|(idx, _)| format_ident!("bound_{}", idx))
182 .collect::<Vec<_>>();
183 let variant_encode_block =
184 derive_enum_variant_encode_block(variant_idx, &variant_fields);
185 quote! {
186 #ident::#variant_ident(#(#variant_fields,)*) => {
187 #variant_encode_block
188 }
189 }
190 } else {
191 let variant_fields = variant
192 .fields
193 .iter()
194 .map(|field| field.ident.clone().unwrap())
195 .collect::<Vec<_>>();
196 let variant_encode_block =
197 derive_enum_variant_encode_block(variant_idx, &variant_fields);
198 quote! {
199 #ident::#variant_ident { #(#variant_fields,)*} => {
200 #variant_encode_block
201 }
202 }
203 }
204 });
205
206 let default_match_arm = variants
207 .iter()
208 .find(|variant| is_default_variant_enforce_valid(variant))
209 .map(|_variant| {
210 quote! {
211 #ident::Default { variant, bytes } => {
212 ::fedimint_core::encoding::Encodable::consensus_encode(variant, writer)?;
213 ::fedimint_core::encoding::Encodable::consensus_encode(bytes, writer)?;
214 }
215 }
216 });
217
218 let match_arms = non_default_match_arms.chain(default_match_arm);
219
220 quote! {
221 match self {
222 #(#match_arms)*
223 }
224 Ok(())
225 }
226}
227
228fn derive_enum_variant_encode_block(idx: u64, fields: &[Ident]) -> TokenStream2 {
229 quote! {
230 ::fedimint_core::encoding::Encodable::consensus_encode(&(#idx), writer)?;
231
232 let mut bytes = Vec::<u8>::new();
233 #(::fedimint_core::encoding::Encodable::consensus_encode(#fields, &mut bytes)?;)*
234
235 ::fedimint_core::encoding::Encodable::consensus_encode(&bytes, writer)?;
236 }
237}
238
239#[proc_macro_derive(Decodable)]
240pub fn derive_decodable(input: TokenStream) -> TokenStream {
241 let DeriveInput { ident, data, .. } = parse_macro_input!(input);
242
243 let decode_inner = match data {
244 Data::Struct(DataStruct { fields, .. }) => derive_struct_decode(&ident, &fields),
245 syn::Data::Enum(DataEnum { variants, .. }) => derive_enum_decode(&ident, &variants),
246 syn::Data::Union(_) => error("Encodable can't be derived for unions"),
247 };
248
249 let output = quote! {
250 #[allow(deprecated)]
251 impl ::fedimint_core::encoding::Decodable for #ident {
252 fn consensus_decode_partial_from_finite_reader<D: std::io::Read>(d: &mut D, modules: &::fedimint_core::module::registry::ModuleDecoderRegistry) -> std::result::Result<Self, ::fedimint_core::encoding::DecodeError> {
253 #decode_inner
254 }
255 }
256 };
257
258 output.into()
259}
260
261fn error(message: &str) -> TokenStream2 {
262 panic!("{message}")
263}
264
265fn derive_struct_decode(ident: &Ident, fields: &Fields) -> TokenStream2 {
266 let decode_block =
267 derive_tuple_or_named_decode_block(ident, "e! { #ident }, "e! { d }, fields);
268
269 quote! {
270 Ok(#decode_block)
271 }
272}
273
274fn derive_enum_decode(ident: &Ident, variants: &Punctuated<Variant, Comma>) -> TokenStream2 {
275 if variants.is_empty() {
276 return quote! {
277 Err(::fedimint_core::encoding::DecodeError::custom(
278 "Enum without variants can't be instantiated",
279 ))
280 };
281 }
282
283 let non_default_match_arms = non_default_variant_indices(variants).into_iter()
284 .map(|(variant_idx, variant)| {
285 let variant_ident = variant.ident.clone();
286 let decode_block = derive_tuple_or_named_decode_block(
287 ident,
288 "e! { #ident::#variant_ident },
289 "e! { &mut cursor },
290 &variant.fields,
291 );
292
293 quote! {
295 #variant_idx => {
296 let bytes: Vec<u8> = ::fedimint_core::encoding::DecodeContext::context(
298 ::fedimint_core::encoding::Decodable::consensus_decode_partial_from_finite_reader(d, modules),
299 concat!(
300 "Decoding bytes of ",
301 stringify!(#ident),
302 ),
303 )?;
304 let mut cursor = ::std::io::Cursor::new(&bytes);
305
306 let decoded = ::fedimint_core::encoding::DecodeContext::context(
307 (|| -> ::std::result::Result<_, ::fedimint_core::encoding::DecodeError> {
308 Ok(#decode_block)
309 })(),
310 concat!(
311 "Decoding variant ",
312 stringify!(#variant_ident),
313 " (idx: ",
314 #variant_idx,
315 ")",
316 ),
317 )?;
318
319 let read_bytes = cursor.position();
320 let total_bytes = bytes.len() as u64;
321 if read_bytes != total_bytes {
322 return Err(::fedimint_core::encoding::DecodeError::custom(::std::format!(
323 "Partial read: got {total_bytes} bytes but only read {read_bytes} when decoding {}",
324 concat!(
325 stringify!(#ident),
326 "::",
327 stringify!(#variant),
328 ),
329 )));
330 }
331
332 decoded
333 }
334 }
335 });
336
337 let default_match_arm = if variants.iter().any(is_default_variant_enforce_valid) {
338 quote! {
339 variant => {
340 let bytes: Vec<u8> = ::fedimint_core::encoding::DecodeContext::context(
341 ::fedimint_core::encoding::Decodable::consensus_decode_partial_from_finite_reader(d, modules),
342 concat!(
343 "Decoding default variant of ",
344 stringify!(#ident),
345 ),
346 )?;
347
348 #ident::Default {
349 variant,
350 bytes
351 }
352 }
353 }
354 } else {
355 quote! {
356 variant => {
357 return Err(::fedimint_core::encoding::DecodeError::InvalidVariant {
358 variant,
359 type_name: stringify!(#ident),
360 });
361 }
362 }
363 };
364
365 quote! {
366 let variant = ::fedimint_core::encoding::DecodeContext::context(
367 <u64 as ::fedimint_core::encoding::Decodable>::consensus_decode_partial_from_finite_reader(d, modules),
368 concat!(
369 "Decoding variant index of ",
370 stringify!(#ident),
371 ),
372 )?;
373
374 let decoded = match variant {
375 #(#non_default_match_arms)*
376 #default_match_arm
377 };
378 Ok(decoded)
379 }
380}
381
382fn is_tuple_struct(fields: &Fields) -> bool {
383 fields.iter().any(|field| field.ident.is_none())
384}
385
386fn derive_tuple_or_named_decode_block(
391 ident: &Ident,
392 constructor: &TokenStream2,
393 reader: &TokenStream2,
394 fields: &Fields,
395) -> TokenStream2 {
396 if is_tuple_struct(fields) {
397 derive_tuple_decode_block(ident, constructor, reader, fields)
398 } else {
399 derive_named_decode_block(ident, constructor, reader, fields)
400 }
401}
402
403fn derive_tuple_decode_block(
404 ident: &Ident,
405 constructor: &TokenStream2,
406 reader: &TokenStream2,
407 fields: &Fields,
408) -> TokenStream2 {
409 let field_names = fields
410 .iter()
411 .enumerate()
412 .map(|(idx, _)| format_ident!("field_{}", idx))
413 .collect::<Vec<_>>();
414 quote! {
415 {
416 #(
417 let #field_names = ::fedimint_core::encoding::DecodeContext::context(
418 ::fedimint_core::encoding::Decodable::consensus_decode_partial_from_finite_reader(#reader, modules),
419 concat!(
420 "Decoding tuple block ",
421 stringify!(#ident),
422 " field ",
423 stringify!(#field_names),
424 ),
425 )?;
426 )*
427 #constructor(#(#field_names,)*)
428 }
429 }
430}
431
432fn derive_named_decode_block(
433 ident: &Ident,
434 constructor: &TokenStream2,
435 reader: &TokenStream2,
436 fields: &Fields,
437) -> TokenStream2 {
438 let variant_fields = fields
439 .iter()
440 .map(|field| field.ident.clone().unwrap())
441 .collect::<Vec<_>>();
442 quote! {
443 {
444 #(
445 let #variant_fields = ::fedimint_core::encoding::DecodeContext::context(
446 ::fedimint_core::encoding::Decodable::consensus_decode_partial_from_finite_reader(#reader, modules),
447 concat!(
448 "Decoding named block field: ",
449 stringify!(#ident),
450 "{ ... ",
451 stringify!(#variant_fields),
452 " ... }",
453 ),
454 )?;
455 )*
456 #constructor{
457 #(#variant_fields,)*
458 }
459 }
460 }
461}