1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//! Derive basic error type infrastruture for enum types.
//!
//! Supports unnamed and unit enum variants, and uses the type definition
//!  to derive `std::fmt::Display` and `std::error:Error` for the error type,
//!  as well as `std::from::From<T>` for any unnamed variant with one parameter
//!  inferred to be an error type (currently determined by whether it's type
//!  name is Error).
//!
//! Default behaviour can be overridden with the auto_error attribute
//!  - format_str takes a string which becomes the format string for that
//!    variant
//!  - make_from forces derivation of std::from::From when set to true
//!  - err forces the std::error::Error implementation to return the inner
//!    type during calls to source, or in other words to treat the inner
//!    type as an error type.
//!
//! From derivation and source returning work only for variants with a single field.
//!
//! # Example
//!
//! ```
//! #[derive(AutoError)]
//! use autoerror::AutoError;
//!
//! enum Error {
//!     #[auto_error(format_str="Document not found")]
//!     NotFound,
//!     IO(std::io::Error),
//!     #[auto_error(make_from=true)]
//!     Other(String),
//! }
//! ```

use proc_macro::TokenStream;
use syn::{parse_macro_input, DeriveInput};
use quote::{quote, format_ident};

// Infer whether wrapped type is an error
//  by applying a name based heuristic (type path
//  last segment is Error)
fn infer_is_error(variant: &syn::Variant) -> bool {
    if let syn::Fields::Named(_) = variant.fields {
        return false;
    }

    if variant.fields.len() != 1 {
        return false;
    }
    let field = variant.fields.iter().next().unwrap();

    if let syn::Type::Path(path) = &field.ty {
        if path.path.segments.len() == 0 {
            return false;
        }
        if path.path.segments.last().unwrap().ident == "Error" {
            return true;
        }
    }

    false
}

// Auto-generate a basic format string for a variant.
fn infer_format_str(variant: &syn::Variant) -> String {
    let mut result = "".to_string();
    let mut first = true;
    for _var in variant.fields.iter() {
        if first {
            result += "{}";
            first = false;
        } else {
            result += " {}";
        }
    }
    result
}

struct ErrorVariant<'a> {
    err: bool,
    make_from: bool,
    format_str: String,
    variant: &'a syn::Variant,
}

// Parse a single variant in the enum
fn parse_variant(variant: &syn::Variant) -> Result<ErrorVariant, TokenStream> {
    // validate fields are unnamed (but present!)
    match variant.fields {
        syn::Fields::Named(_) => { return Err(TokenStream::from(syn::Error::new_spanned(variant, "Named fields not supported").to_compile_error())); }
        syn::Fields::Unnamed(_) => {}
        syn::Fields::Unit => { }
    }

    let mut attr: Option<_> = None;
    for attr_cand in variant.attrs.iter() {
        if attr_cand.path.is_ident("auto_error") {
            if attr != None {
                return Err(TokenStream::from(syn::Error::new_spanned(&attr_cand, "Duplicate occurence of auto_error attribute").to_compile_error()));
            }
            attr = Some(attr_cand);
        }
    }

    let mut result = ErrorVariant {
        err: infer_is_error(variant),
        make_from: infer_is_error(variant),
        format_str: infer_format_str(variant),
        variant,
    };

    if let Some(attr) = attr {
        let meta = attr.parse_meta().map_err(|e| e.to_compile_error())?;
        let meta = match meta {
            syn::Meta::List(list) => list,
            _ => { return Err(TokenStream::from(syn::Error::new_spanned(&meta, "Incorrect auto_error arguments").to_compile_error())); },
        };

        for arg in meta.nested.iter() {
            let arg = match arg {
                syn::NestedMeta::Meta(arg) => arg,
                _ => { return Err(TokenStream::from(syn::Error::new_spanned(arg, "Incorrect auto_error arguments").to_compile_error())); },
            };
            let arg = match arg {
                syn::Meta::NameValue(arg) => arg,
                _ => { return Err(TokenStream::from(syn::Error::new_spanned(arg, "Incorrect auto_error arguments").to_compile_error())); },
            };
            if arg.path.is_ident("err") {
                result.err = match &arg.lit {
                    syn::Lit::Bool(v) => v.value,
                    _ => { return Err(TokenStream::from(syn::Error::new_spanned(&arg.lit, "Incorrect value for err, expected bool").to_compile_error())); },
                };
            } else if arg.path.is_ident("format_str") {
                result.format_str = match &arg.lit {
                    syn::Lit::Str(v) => v.value(),
                    _ => { return Err(TokenStream::from(syn::Error::new_spanned(&arg.lit, "Incorrect value for format_str, expected string").to_compile_error())); },
                };
            } else if arg.path.is_ident("make_from") {
                result.make_from = match &arg.lit {
                    syn::Lit::Bool(v) => v.value,
                    _ => { return Err(TokenStream::from(syn::Error::new_spanned(&arg.lit, "Incorrect value for make_from, expected bool").to_compile_error())); },
                };
            } else {
                return Err(TokenStream::from(syn::Error::new_spanned(variant, "Unknown parameter").to_compile_error()));
            }
        }
    }

    if result.err && result.variant.fields.len() != 1 {
        return Err(TokenStream::from(syn::Error::new_spanned(variant, "Wrapped errors should have exactly 1 argument").to_compile_error()));
    }

    if result.make_from && result.variant.fields.len() != 1 {
        return Err(TokenStream::from(syn::Error::new_spanned(variant, "Can only derive from for variants with 1 field").to_compile_error()));
    }

    Ok(result)
}

/// Derive basic error type infrastruture for enum types.
///
/// Supports unnamed and unit enum variants, and uses the type definition
///  to derive `std::fmt::Display` and `std::error:Error` for the error type,
///  as well as `std::from::From<T>` for any unnamed variant with one parameter
///  inferred to be an error type (currently determined by whether it's type
///  name is Error).
///
/// Default behaviour can be overridden with the auto_error attribute
///  - format_str takes a string which becomes the format string for that
///    variant
///  - make_from forces derivation of std::from::From when set to true
///  - err forces the std::error::Error implementation to return the inner
///    type during calls to source, or in other words to treat the inner
///    type as an error type.
///
/// From derivation and source returning work only for variants with a single field.
///
/// # Example
///
/// ```
/// #[derive(AutoError)]
/// use autoerror::AutoError;
///
/// enum Error {
///     #[auto_error(format_str="Document not found")]
///     NotFound,
///     IO(std::io::Error),
///     #[auto_error(make_from=true)]
///     Other(String),
/// }
/// ```
#[proc_macro_derive(AutoError, attributes(auto_error))]
pub fn derive(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);

    let enumdecl = if let syn::Data::Enum(e) = input.data {
        e
    } else {
        return TokenStream::from(syn::Error::new_spanned(&input.ident, "AutoError only supports enums").to_compile_error());
    };

    let error_ident = input.ident;
    let error_variants: Result<Vec<_>, TokenStream> = enumdecl.variants.iter().map(|v| parse_variant(v)).collect();
    let error_variants = match error_variants {
        Ok(v) => v,
        Err(e) => {return e}
    };

    let from_impls = error_variants.iter().map(|var| {
        if !var.make_from {
            return None;
        }

        let sourcetype = &var.variant.fields.iter().next().unwrap().ty;
        let curvar = &var.variant.ident;

        Some(quote!{
            impl ::std::convert::From<#sourcetype> for #error_ident {
                fn from (e: #sourcetype) -> Self {
                    Self::#curvar(e)
                }
            }
        })
    });

    let display_branches = error_variants.iter().map(|var| {
        let format_str = &var.format_str;
        let curvar = &var.variant.ident;
        let params: Vec<_> = var.variant.fields.iter().enumerate().map(|(i, _field)| {
            format_ident!("f{}", i)
        }).collect();
        match var.variant.fields {
            syn::Fields::Unnamed(_) => quote!{
                Self::#curvar(#(#params),*) => f.write_fmt(format_args!(#format_str #(,#params)*)),
            },
            syn::Fields::Unit => quote!{
                Self::#curvar => f.write_fmt(format_args!(#format_str)),
            },
            _ => panic!("Internal error (AutoError)")
        }
        
    });

    let source_branches = error_variants.iter().map(|var| {
        if !var.err {
            return None;
        }
        let curvar = &var.variant.ident;
        Some(quote!{
            Self::#curvar(e) => Some(e),
        })
    });

    TokenStream::from(quote! {
        #(#from_impls)*

        impl ::std::fmt::Display for #error_ident {
            fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
                match self {
                    #(#display_branches)*
                }
            }
        }

        impl ::std::error::Error for #error_ident {
            fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
                match self {
                    #(#source_branches)*
                    _ => None,
                }
            }
        }
    })
}