| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
use proc_macro2::TokenStream; |
| 6 |
use quote::{format_ident, quote}; |
| 7 |
use syn::{Attribute, Data, DeriveInput, Expr, ExprLit, Fields, Index, Lit, Meta}; |
| 8 |
|
| 9 |
fn has_ignore_attribute(attrs: &[Attribute]) -> bool { |
| 10 |
attrs.iter().any(|attr| { |
| 11 |
if attr.path().is_ident("checksum_ignore") { |
| 12 |
if let Meta::List(_) | Meta::NameValue(_) = &attr.meta { |
| 13 |
panic!("#[checksum_ignore] doesn't accept extra information"); |
| 14 |
} |
| 15 |
true |
| 16 |
} else { |
| 17 |
false |
| 18 |
} |
| 19 |
}) |
| 20 |
} |
| 21 |
|
| 22 |
pub fn expand_derive(input: DeriveInput) -> TokenStream { |
| 23 |
let name = input.ident; |
| 24 |
|
| 25 |
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); |
| 26 |
|
| 27 |
let code = match input.data { |
| 28 |
Data::Enum(enum_) |
| 29 |
if enum_.variants.len() == 1 |
| 30 |
&& enum_ |
| 31 |
.variants |
| 32 |
.iter() |
| 33 |
.all(|variant| matches!(variant.fields, Fields::Unit)) => |
| 34 |
{ |
| 35 |
quote!() |
| 36 |
} |
| 37 |
Data::Enum(enum_) => { |
| 38 |
let mut next_discriminant = 0u64; |
| 39 |
let match_inner = enum_.variants.iter().map(|variant| { |
| 40 |
let ident = &variant.ident; |
| 41 |
if has_ignore_attribute(&variant.attrs) { |
| 42 |
panic!("#[checksum_ignore] is not supported in enums"); |
| 43 |
} |
| 44 |
match &variant.discriminant { |
| 45 |
Some((_, Expr::Lit(ExprLit { lit: Lit::Int(value), .. }))) => { |
| 46 |
next_discriminant = value.base10_parse::<u64>().unwrap(); |
| 47 |
} |
| 48 |
Some(_) => { |
| 49 |
panic!("#[derive(Checksum)] doesn't support non-numeric explicit discriminants in enums"); |
| 50 |
} |
| 51 |
None => {} |
| 52 |
} |
| 53 |
let discriminant = quote! { state.write(&#next_discriminant.to_le_bytes()) }; |
| 54 |
next_discriminant += 1; |
| 55 |
match &variant.fields { |
| 56 |
Fields::Unnamed(fields) => { |
| 57 |
let field_idents = fields |
| 58 |
.unnamed |
| 59 |
.iter() |
| 60 |
.enumerate() |
| 61 |
.map(|(num, _)| format_ident!("__self_{}", num)); |
| 62 |
let field_stmts = field_idents |
| 63 |
.clone() |
| 64 |
.map(|ident| quote! { Checksum::checksum(#ident, state); }); |
| 65 |
quote! { |
| 66 |
Self::#ident(#(#field_idents,)*) => { |
| 67 |
#discriminant; |
| 68 |
#(#field_stmts)* |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
Fields::Named(fields) => { |
| 73 |
let field_idents = fields |
| 74 |
.named |
| 75 |
.iter() |
| 76 |
.map(|field| field.ident.as_ref().unwrap()); |
| 77 |
let field_stmts = fields.named.iter() |
| 78 |
.filter(|field| !has_ignore_attribute(&field.attrs)) |
| 79 |
.map(|field| { |
| 80 |
let ident = field.ident.as_ref().unwrap(); |
| 81 |
quote! { Checksum::checksum(#ident, state); } |
| 82 |
}); |
| 83 |
quote! { |
| 84 |
Self::#ident { #(#field_idents,)* } => { |
| 85 |
#discriminant; |
| 86 |
#(#field_stmts)* |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
Fields::Unit => quote! { Self::#ident => #discriminant, }, |
| 91 |
} |
| 92 |
}); |
| 93 |
quote! { |
| 94 |
match self { |
| 95 |
#(#match_inner)* |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
Data::Struct(struct_) => { |
| 100 |
let stmts = struct_ |
| 101 |
.fields |
| 102 |
.iter() |
| 103 |
.enumerate() |
| 104 |
.filter(|&(_num, field)| (!has_ignore_attribute(&field.attrs))) |
| 105 |
.map(|(num, field)| match field.ident.as_ref() { |
| 106 |
Some(ident) => quote! { Checksum::checksum(&self.#ident, state); }, |
| 107 |
None => { |
| 108 |
let i = Index::from(num); |
| 109 |
quote! { Checksum::checksum(&self.#i, state); } |
| 110 |
} |
| 111 |
}); |
| 112 |
quote! { |
| 113 |
#(#stmts)* |
| 114 |
} |
| 115 |
} |
| 116 |
Data::Union(_) => { |
| 117 |
panic!("#[derive(Checksum)] is not supported for unions"); |
| 118 |
} |
| 119 |
}; |
| 120 |
|
| 121 |
quote! { |
| 122 |
#[automatically_derived] |
| 123 |
impl #impl_generics Checksum for #name #ty_generics #where_clause { |
| 124 |
fn checksum<__H: ::core::hash::Hasher>(&self, state: &mut __H) { |
| 125 |
#code |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
} |