| 1 |
use crate::utils::{ |
| 2 |
add_extra_type_param_bound_op_output, named_to_vec, unnamed_to_vec, |
| 3 |
}; |
| 4 |
use proc_macro2::TokenStream; |
| 5 |
use quote::{format_ident, quote, ToTokens}; |
| 6 |
use std::iter; |
| 7 |
use syn::{Data, DataEnum, DeriveInput, Field, Fields, Ident, Index}; |
| 8 |
|
| 9 |
pub fn expand(input: &DeriveInput, trait_name: &str) -> TokenStream { |
| 10 |
let trait_ident = format_ident!("{trait_name}"); |
| 11 |
let method_name = trait_name.to_lowercase(); |
| 12 |
let method_ident = format_ident!("{method_name}"); |
| 13 |
let input_type = &input.ident; |
| 14 |
|
| 15 |
let generics = add_extra_type_param_bound_op_output(&input.generics, &trait_ident); |
| 16 |
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); |
| 17 |
|
| 18 |
let (output_type, block) = match input.data { |
| 19 |
Data::Struct(ref data_struct) => match data_struct.fields { |
| 20 |
Fields::Unnamed(ref fields) => ( |
| 21 |
quote! { #input_type #ty_generics }, |
| 22 |
tuple_content(input_type, &unnamed_to_vec(fields), &method_ident), |
| 23 |
), |
| 24 |
Fields::Named(ref fields) => ( |
| 25 |
quote! { #input_type #ty_generics }, |
| 26 |
struct_content(input_type, &named_to_vec(fields), &method_ident), |
| 27 |
), |
| 28 |
_ => panic!("Unit structs cannot use derive({trait_name})"), |
| 29 |
}, |
| 30 |
Data::Enum(ref data_enum) => { |
| 31 |
enum_output_type_and_content(input, data_enum, &method_ident) |
| 32 |
} |
| 33 |
|
| 34 |
_ => panic!("Only structs and enums can use derive({trait_name})"), |
| 35 |
}; |
| 36 |
|
| 37 |
quote! { |
| 38 |
#[automatically_derived] |
| 39 |
impl #impl_generics ::core::ops::#trait_ident for #input_type #ty_generics #where_clause { |
| 40 |
type Output = #output_type; |
| 41 |
|
| 42 |
#[inline] |
| 43 |
fn #method_ident(self) -> #output_type { |
| 44 |
#block |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
fn tuple_content<T: ToTokens>( |
| 51 |
input_type: &T, |
| 52 |
fields: &[&Field], |
| 53 |
method_ident: &Ident, |
| 54 |
) -> TokenStream { |
| 55 |
let mut exprs = vec![]; |
| 56 |
|
| 57 |
for i in 0..fields.len() { |
| 58 |
let i = Index::from(i); |
| 59 |
// generates `self.0.add()` |
| 60 |
let expr = quote! { self.#i.#method_ident() }; |
| 61 |
exprs.push(expr); |
| 62 |
} |
| 63 |
|
| 64 |
quote! { #input_type(#(#exprs),*) } |
| 65 |
} |
| 66 |
|
| 67 |
fn struct_content( |
| 68 |
input_type: &Ident, |
| 69 |
fields: &[&Field], |
| 70 |
method_ident: &Ident, |
| 71 |
) -> TokenStream { |
| 72 |
let mut exprs = vec![]; |
| 73 |
|
| 74 |
for field in fields { |
| 75 |
// It's safe to unwrap because struct fields always have an identifier |
| 76 |
let field_id = field.ident.as_ref(); |
| 77 |
// generates `x: self.x.not()` |
| 78 |
let expr = quote! { #field_id: self.#field_id.#method_ident() }; |
| 79 |
exprs.push(expr) |
| 80 |
} |
| 81 |
|
| 82 |
quote! { #input_type{#(#exprs),*} } |
| 83 |
} |
| 84 |
|
| 85 |
fn enum_output_type_and_content( |
| 86 |
input: &DeriveInput, |
| 87 |
data_enum: &DataEnum, |
| 88 |
method_ident: &Ident, |
| 89 |
) -> (TokenStream, TokenStream) { |
| 90 |
let input_type = &input.ident; |
| 91 |
let (_, ty_generics, _) = input.generics.split_for_impl(); |
| 92 |
let mut matches = vec![]; |
| 93 |
let mut method_iter = iter::repeat(method_ident); |
| 94 |
// If the enum contains unit types that means it can error. |
| 95 |
let has_unit_type = data_enum.variants.iter().any(|v| v.fields == Fields::Unit); |
| 96 |
|
| 97 |
for variant in &data_enum.variants { |
| 98 |
let subtype = &variant.ident; |
| 99 |
let subtype = quote! { #input_type::#subtype }; |
| 100 |
|
| 101 |
match variant.fields { |
| 102 |
Fields::Unnamed(ref fields) => { |
| 103 |
// The pattern that is outputted should look like this: |
| 104 |
// (Subtype(vars)) => Ok(TypePath(exprs)) |
| 105 |
let size = unnamed_to_vec(fields).len(); |
| 106 |
let vars: &Vec<_> = |
| 107 |
&(0..size).map(|i| format_ident!("__{i}")).collect(); |
| 108 |
let method_iter = method_iter.by_ref(); |
| 109 |
let mut body = quote! { #subtype(#(#vars.#method_iter()),*) }; |
| 110 |
if has_unit_type { |
| 111 |
body = quote! { ::core::result::Result::Ok(#body) } |
| 112 |
} |
| 113 |
let matcher = quote! { |
| 114 |
#subtype(#(#vars),*) => { |
| 115 |
#body |
| 116 |
} |
| 117 |
}; |
| 118 |
matches.push(matcher); |
| 119 |
} |
| 120 |
Fields::Named(ref fields) => { |
| 121 |
// The pattern that is outputted should look like this: |
| 122 |
// (Subtype{a: __l_a, ...} => { |
| 123 |
// Ok(Subtype{a: __l_a.neg(__r_a), ...}) |
| 124 |
// } |
| 125 |
let field_vec = named_to_vec(fields); |
| 126 |
let size = field_vec.len(); |
| 127 |
let field_names: &Vec<_> = &field_vec |
| 128 |
.iter() |
| 129 |
.map(|f| f.ident.as_ref().unwrap()) |
| 130 |
.collect(); |
| 131 |
let vars: &Vec<_> = |
| 132 |
&(0..size).map(|i| format_ident!("__{i}")).collect(); |
| 133 |
let method_iter = method_iter.by_ref(); |
| 134 |
let mut body = quote! { |
| 135 |
#subtype{#(#field_names: #vars.#method_iter()),*} |
| 136 |
}; |
| 137 |
if has_unit_type { |
| 138 |
body = quote! { ::core::result::Result::Ok(#body) } |
| 139 |
} |
| 140 |
let matcher = quote! { |
| 141 |
#subtype{#(#field_names: #vars),*} => { |
| 142 |
#body |
| 143 |
} |
| 144 |
}; |
| 145 |
matches.push(matcher); |
| 146 |
} |
| 147 |
Fields::Unit => { |
| 148 |
let operation_name = method_ident.to_string(); |
| 149 |
matches.push(quote! { |
| 150 |
#subtype => ::core::result::Result::Err( |
| 151 |
::derive_more::ops::UnitError::new(#operation_name) |
| 152 |
) |
| 153 |
}); |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
let body = quote! { |
| 159 |
match self { |
| 160 |
#(#matches),* |
| 161 |
} |
| 162 |
}; |
| 163 |
|
| 164 |
let output_type = if has_unit_type { |
| 165 |
quote! { ::core::result::Result<#input_type #ty_generics, ::derive_more::ops::UnitError> } |
| 166 |
} else { |
| 167 |
quote! { #input_type #ty_generics } |
| 168 |
}; |
| 169 |
|
| 170 |
(output_type, body) |
| 171 |
} |