|
| 1 | +mod abs_diff_eq; |
| 2 | +mod relative_eq; |
| 3 | + |
| 4 | +use darling::{ |
| 5 | + ast::{Data, Fields, Style}, |
| 6 | + util::Flag, |
| 7 | + FromField, FromVariant, |
| 8 | +}; |
| 9 | +use itertools::Itertools; |
| 10 | +use proc_macro2::{Literal, Span, TokenStream}; |
| 11 | +use quote::{format_ident, quote, ToTokens}; |
| 12 | +use syn::{parse_macro_input, DeriveInput, Ident}; |
| 13 | + |
| 14 | +#[derive(FromVariant)] |
| 15 | +#[darling(attributes(approx))] |
| 16 | +struct Variant { |
| 17 | + ident: Ident, |
| 18 | + fields: Fields<Field>, |
| 19 | +} |
| 20 | + |
| 21 | +#[derive(FromField)] |
| 22 | +#[darling(attributes(approx), and_then = Self::validate)] |
| 23 | +struct Field { |
| 24 | + ident: Option<Ident>, |
| 25 | + skip: Flag, |
| 26 | + approximate: Flag, |
| 27 | +} |
| 28 | + |
| 29 | +impl Field { |
| 30 | + fn validate(self) -> darling::Result<Self> { |
| 31 | + if self.skip.is_present() && self.approximate.is_present() { |
| 32 | + Err( |
| 33 | + darling::Error::custom("Cannot both skip and use approximate equality") |
| 34 | + .with_span(&self.approximate.span()), |
| 35 | + ) |
| 36 | + } else { |
| 37 | + Ok(self) |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +#[proc_macro_derive(AbsDiffEq, attributes(approx))] |
| 43 | +pub fn abs_diff_eq(item: proc_macro::TokenStream) -> proc_macro::TokenStream { |
| 44 | + let item = parse_macro_input!(item as DeriveInput); |
| 45 | + convert(abs_diff_eq::derive(item)) |
| 46 | +} |
| 47 | + |
| 48 | +#[proc_macro_derive(RelativeEq, attributes(approx))] |
| 49 | +pub fn relative_eq(item: proc_macro::TokenStream) -> proc_macro::TokenStream { |
| 50 | + let item = parse_macro_input!(item as DeriveInput); |
| 51 | + convert(relative_eq::derive(item)) |
| 52 | +} |
| 53 | + |
| 54 | +fn convert(tokens: syn::Result<TokenStream>) -> proc_macro::TokenStream { |
| 55 | + tokens.unwrap_or_else(syn::Error::into_compile_error).into() |
| 56 | +} |
| 57 | + |
| 58 | +fn comparisons(data: Data<Variant, Field>, f: &str, arg: Option<&str>) -> TokenStream { |
| 59 | + let comparisons = data |
| 60 | + .map_enum_variants(|variant| { |
| 61 | + let ident = variant.ident; |
| 62 | + let fields = variant.fields; |
| 63 | + match fields.style { |
| 64 | + Style::Tuple => { |
| 65 | + let (comps, self_extractors, other_extractors): (Vec<_>, Vec<_>, Vec<_>) = |
| 66 | + fields |
| 67 | + .fields |
| 68 | + .into_iter() |
| 69 | + .enumerate() |
| 70 | + .map(|(i, field)| { |
| 71 | + if field.skip.is_present() { |
| 72 | + (None, format_ident!("_"), format_ident!("_")) |
| 73 | + } else { |
| 74 | + let one = format_ident!("_{}", i); |
| 75 | + let other = format_ident!("other_{}", i); |
| 76 | + ( |
| 77 | + Some(compare(&one, &other, field.approximate, f, arg)), |
| 78 | + one, |
| 79 | + other, |
| 80 | + ) |
| 81 | + } |
| 82 | + }) |
| 83 | + .multiunzip(); |
| 84 | + let comps = comps.iter().flatten(); |
| 85 | + quote! { |
| 86 | + Self::#ident(#(#self_extractors),*) => match other { |
| 87 | + Self::#ident(#(#other_extractors),*) => #(#comps)&&*, |
| 88 | + _ => false |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + Style::Struct => { |
| 93 | + let (comps, self_extractors, other_extractors): (Vec<_>, Vec<_>, Vec<_>) = |
| 94 | + fields |
| 95 | + .fields |
| 96 | + .into_iter() |
| 97 | + .filter_map(|field| { |
| 98 | + if field.skip.is_present() { |
| 99 | + None |
| 100 | + } else { |
| 101 | + let one = field.ident.clone().unwrap(); |
| 102 | + let other = format_ident!("other_{}", one); |
| 103 | + Some(( |
| 104 | + compare(&one, &other, field.approximate, f, arg), |
| 105 | + one.clone(), |
| 106 | + quote! { #one: #other }, |
| 107 | + )) |
| 108 | + } |
| 109 | + }) |
| 110 | + .multiunzip(); |
| 111 | + quote! { |
| 112 | + Self::#ident { #(#self_extractors),*, .. } => match other { |
| 113 | + Self::#ident {#(#other_extractors),*, ..} => #(#comps)&&*, |
| 114 | + _ => false |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + Style::Unit => quote! { Self::#ident => self == other }, |
| 119 | + } |
| 120 | + }) |
| 121 | + .map_struct(|fields| { |
| 122 | + Fields::<TokenStream>::from(( |
| 123 | + fields.style, |
| 124 | + fields |
| 125 | + .into_iter() |
| 126 | + .enumerate() |
| 127 | + .filter_map(|(i, field)| { |
| 128 | + if field.skip.is_present() { |
| 129 | + None |
| 130 | + } else { |
| 131 | + let ident = match field.ident { |
| 132 | + None => Literal::usize_unsuffixed(i).to_token_stream(), |
| 133 | + Some(ident) => quote! { #ident }, |
| 134 | + }; |
| 135 | + Some(compare( |
| 136 | + quote! { self.#ident }, |
| 137 | + quote! { other.#ident }, |
| 138 | + field.approximate, |
| 139 | + f, |
| 140 | + arg, |
| 141 | + )) |
| 142 | + } |
| 143 | + }) |
| 144 | + .collect::<Vec<_>>(), |
| 145 | + )) |
| 146 | + }); |
| 147 | + |
| 148 | + if comparisons.is_enum() { |
| 149 | + let comparisons = comparisons.take_enum().unwrap(); |
| 150 | + quote! { |
| 151 | + match self { |
| 152 | + #(#comparisons),* |
| 153 | + } |
| 154 | + } |
| 155 | + } else { |
| 156 | + let comparisons = comparisons.take_struct().unwrap().fields.into_iter(); |
| 157 | + quote!(#(#comparisons)&&*) |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +fn compare<One, Other>( |
| 162 | + one: One, |
| 163 | + other: Other, |
| 164 | + approximate: Flag, |
| 165 | + f: &str, |
| 166 | + arg: Option<&str>, |
| 167 | +) -> TokenStream |
| 168 | +where |
| 169 | + One: ToTokens, |
| 170 | + Other: ToTokens, |
| 171 | +{ |
| 172 | + if approximate.is_present() { |
| 173 | + let q = |s| Ident::new(s, Span::call_site()); |
| 174 | + let arg = arg.map(|arg| { |
| 175 | + let arg = q(arg); |
| 176 | + quote! {, #arg} |
| 177 | + }); |
| 178 | + let f = q(f); |
| 179 | + quote! { #one.#f(&#other, epsilon #arg) } |
| 180 | + } else { |
| 181 | + quote! { #one == #other } |
| 182 | + } |
| 183 | +} |
0 commit comments