Switch expansion to use new ast

This commit is contained in:
David Tolnay 2019-10-12 10:25:51 -07:00
parent 60987ffa76
commit 69052f88d8
No known key found for this signature in database
GPG key ID: F9BA143B95FF6D82

View file

@ -1,28 +1,23 @@
use crate::attr;
use crate::ast::{Enum, Field, Input, Struct};
use crate::attr::Attrs;
use proc_macro2::TokenStream;
use quote::{format_ident, quote, quote_spanned};
use syn::spanned::Spanned;
use syn::{
Data, DataEnum, DataStruct, DeriveInput, Error, Field, Fields, Ident, Index, Member, Result,
Type,
};
use syn::{DeriveInput, Error, Member, Result, Type};
pub fn derive(input: &DeriveInput) -> Result<TokenStream> {
match &input.data {
Data::Struct(data) => impl_struct(input, data),
Data::Enum(data) => impl_enum(input, data),
Data::Union(_) => Err(Error::new_spanned(
input,
"union as errors are not supported",
)),
pub fn derive(node: &DeriveInput) -> Result<TokenStream> {
let input = Input::from_syn(node)?;
match input {
Input::Struct(input) => impl_struct(input),
Input::Enum(input) => impl_enum(input),
}
}
fn impl_struct(input: &DeriveInput, data: &DataStruct) -> Result<TokenStream> {
fn impl_struct(input: Struct) -> Result<TokenStream> {
let ty = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let source = source_member(&data.fields)?;
let source = source_member(&input.fields);
let source_method = source.map(|source| {
let member = quote_spanned!(source.span()=> self.#source);
quote! {
@ -33,7 +28,7 @@ fn impl_struct(input: &DeriveInput, data: &DataStruct) -> Result<TokenStream> {
}
});
let backtrace = backtrace_member(&data.fields)?;
let backtrace = backtrace_member(&input.fields);
let backtrace_method = backtrace.map(|backtrace| {
quote! {
fn backtrace(&self) -> std::option::Option<&std::backtrace::Backtrace> {
@ -42,24 +37,13 @@ fn impl_struct(input: &DeriveInput, data: &DataStruct) -> Result<TokenStream> {
}
});
let struct_attrs = attr::get(&input.attrs)?;
let display = struct_attrs.display.map(|display| {
let pat = match &data.fields {
Fields::Named(fields) => {
let var = fields.named.iter().map(|field| &field.ident);
quote!(Self { #(#var),* })
}
Fields::Unnamed(fields) => {
let var = (0..fields.unnamed.len()).map(|i| format_ident!("_{}", i));
quote!(Self(#(#var),*))
}
Fields::Unit => quote!(_),
};
let display = input.attrs.display.as_ref().map(|display| {
let pat = fields_pat(&input.fields);
quote! {
impl #impl_generics std::fmt::Display for #ty #ty_generics #where_clause {
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
#[allow(unused_variables)]
let #pat = self;
let Self #pat = self;
#display
}
}
@ -75,30 +59,24 @@ fn impl_struct(input: &DeriveInput, data: &DataStruct) -> Result<TokenStream> {
})
}
fn impl_enum(input: &DeriveInput, data: &DataEnum) -> Result<TokenStream> {
fn impl_enum(input: Enum) -> Result<TokenStream> {
let ty = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let variant_fields: Vec<&Fields> = data
let sources: Vec<Option<&Member>> = input
.variants
.iter()
.map(|variant| &variant.fields)
.map(|variant| source_member(&variant.fields))
.collect();
let sources: Vec<Option<Member>> = variant_fields
let backtraces: Vec<Option<&Member>> = input
.variants
.iter()
.cloned()
.map(source_member)
.collect::<Result<_>>()?;
let backtraces: Vec<Option<Member>> = variant_fields
.iter()
.cloned()
.map(backtrace_member)
.collect::<Result<_>>()?;
.map(|variant| backtrace_member(&variant.fields))
.collect();
let source_method = if sources.iter().any(Option::is_some) {
let arms = data.variants.iter().zip(sources).map(|(variant, source)| {
let arms = input.variants.iter().zip(sources).map(|(variant, source)| {
let ident = &variant.ident;
match source {
Some(source) => quote! {
@ -122,7 +100,7 @@ fn impl_enum(input: &DeriveInput, data: &DataEnum) -> Result<TokenStream> {
};
let backtrace_method = if backtraces.iter().any(Option::is_some) {
let arms = data.variants.iter().zip(backtraces).map(|(variant, backtrace)| {
let arms = input.variants.iter().zip(backtraces).map(|(variant, backtrace)| {
let ident = &variant.ident;
match backtrace {
Some(backtrace) => quote! {
@ -144,31 +122,27 @@ fn impl_enum(input: &DeriveInput, data: &DataEnum) -> Result<TokenStream> {
None
};
let variant_attrs = data
let variant_attrs: Vec<&Attrs> = input
.variants
.iter()
.map(|variant| attr::get(&variant.attrs))
.collect::<Result<Vec<_>>>()?;
.map(|variant| &variant.attrs)
.collect();
let display = if variant_attrs.iter().any(|attrs| attrs.display.is_some()) {
let arms = data
let arms = input
.variants
.iter()
.zip(variant_attrs)
.map(|(variant, attrs)| {
let display = attrs.display.ok_or_else(|| {
Error::new_spanned(variant, "missing #[error(\"...\")] display attribute")
let display = attrs.display.as_ref().ok_or_else(|| {
Error::new_spanned(
&variant.ident,
"missing #[error(\"...\")] display attribute",
)
})?;
let ident = &variant.ident;
Ok(match &variant.fields {
Fields::Named(fields) => {
let var = fields.named.iter().map(|field| &field.ident);
quote!(#ty::#ident { #(#var),* } => #display)
}
Fields::Unnamed(fields) => {
let var = (0..fields.unnamed.len()).map(|i| format_ident!("_{}", i));
quote!(#ty::#ident(#(#var),*) => #display)
}
Fields::Unit => quote!(#ty::#ident => #display),
let pat = fields_pat(&variant.fields);
Ok(quote! {
#ty::#ident #pat => #display
})
})
.collect::<Result<Vec<_>>>()?;
@ -195,23 +169,22 @@ fn impl_enum(input: &DeriveInput, data: &DataEnum) -> Result<TokenStream> {
})
}
fn source_member<'a>(fields: impl IntoIterator<Item = &'a Field>) -> Result<Option<Member>> {
for (i, field) in fields.into_iter().enumerate() {
let attrs = attr::get(&field.attrs)?;
if attrs.source {
return Ok(Some(member(i, &field.ident)));
fn source_member<'a>(fields: &'a [Field]) -> Option<&'a Member> {
for field in fields {
if field.attrs.source {
return Some(&field.member);
}
}
Ok(None)
None
}
fn backtrace_member<'a>(fields: impl IntoIterator<Item = &'a Field>) -> Result<Option<Member>> {
for (i, field) in fields.into_iter().enumerate() {
fn backtrace_member<'a>(fields: &'a [Field]) -> Option<&'a Member> {
for field in fields {
if type_is_backtrace(&field.ty) {
return Ok(Some(member(i, &field.ident)));
return Some(&field.member);
}
}
Ok(None)
None
}
fn type_is_backtrace(ty: &Type) -> bool {
@ -224,9 +197,17 @@ fn type_is_backtrace(ty: &Type) -> bool {
last.ident == "Backtrace" && last.arguments.is_empty()
}
fn member(i: usize, ident: &Option<Ident>) -> Member {
match ident {
Some(ident) => Member::Named(ident.clone()),
None => Member::Unnamed(Index::from(i)),
fn fields_pat(fields: &[Field]) -> TokenStream {
let mut members = fields.iter().map(|field| &field.member).peekable();
match members.peek() {
Some(Member::Named(_)) => quote!({ #(#members),* }),
Some(Member::Unnamed(_)) => {
let vars = members.map(|member| match member {
Member::Unnamed(member) => format_ident!("_{}", member.index),
Member::Named(_) => unreachable!(),
});
quote!((#(#vars),*))
}
None => quote!({}),
}
}