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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
extern crate proc_macro2;
extern crate proc_macro;
#[macro_use]
extern crate quote;
extern crate syn;

use proc_macro::TokenStream;

/// See the crate-level documentation for SNAFU which contains tested
/// examples of this macro.

#[cfg_attr(not(feature = "unstable_display_attribute"),
           proc_macro_derive(Snafu, attributes(snafu_display)))]
#[cfg_attr(feature = "unstable_display_attribute",
           proc_macro_derive(Snafu, attributes(snafu::display, snafu_display)))]
pub fn snafu_derive(input: TokenStream) -> TokenStream {
    let ast = syn::parse(input).expect("Could not parse type to derive Error for");

    impl_hello_macro(ast)
}

struct EnumInfo {
    name: syn::Ident,
    variants: Vec<VariantInfo>,
}

struct VariantInfo {
    name: syn::Ident,
    source_field: Option<Field>,
    user_fields: Vec<Field>,
    display_format: Option<DisplayFormat>,
}

enum DisplayFormat {
    Direct(Box<quote::ToTokens>),
    Stringified(Vec<Box<quote::ToTokens>>),
}

struct Field {
    name: syn::Ident,
    ty: syn::Type,
}

fn impl_hello_macro(ty: syn::DeriveInput) -> TokenStream {
    let info = parse_snafu_information(ty);
    generate_snafu(info).into()
}

fn parse_snafu_information(ty: syn::DeriveInput) -> EnumInfo {
    use syn::{Data, Fields, Meta};

    let enum_ = match ty.data {
        Data::Enum(enum_) => enum_,
        _ => panic!("Can only derive Error for an enum"),
    };

    let name = ty.ident;

    let variants = enum_.variants.into_iter().map(|variant| {
        let name = variant.ident;

        let display_format = variant.attrs.into_iter().map(|attr| {
            if is_snafu_display(&attr.path) {
                parse_snafu_display_beautiful(attr)
            } else if attr.path.is_ident("snafu_display") {
                let meta = attr.parse_meta().expect("`snafu_display` attribute is malformed");
                match meta {
                    Meta::List(list) => parse_snafu_display_nested(list),
                    Meta::NameValue(nv) => parse_snafu_display_nested_name_value(nv),
                    _ => panic!("Only supports a list"),
                }
            } else {
                panic!("Unknown attribute type");
            }
        }).next();

        let fields = match variant.fields {
            Fields::Named(f) => f.named.into_iter().collect(),
            Fields::Unnamed(_) => panic!("Tuple variants are not supported"),
            Fields::Unit => vec![],
        };

        let mut user_fields = Vec::new();
        let mut source_fields = Vec::new();

        for field in fields {
            let name = field.ident.expect("Must have a named field");
            let field = Field { name, ty: field.ty };

            if field.name == "source" {
                source_fields.push(field);
            } else {
                user_fields.push(field);
            }
        }

        let source_field = source_fields.pop();
        // Report a warning if there are multiple?

        VariantInfo { name, source_field, user_fields, display_format }
    }).collect();

    EnumInfo { name, variants }
}

fn is_snafu_display(p: &syn::Path) -> bool{
    let parts = ["snafu", "display"];
    p.segments.iter().zip(&parts).map(|(a, b)| a.ident == b).all(|b| b)
}

fn parse_snafu_display_beautiful(attr: syn::Attribute) -> DisplayFormat {
    use syn::Expr;

    let expr: Expr = syn::parse2(attr.tts).expect("Need expression");
    let expr: Box<quote::ToTokens> = match expr {
        Expr::Tuple(expr_tuple) => Box::new(expr_tuple.elems),
        Expr::Paren(expr_paren) => Box::new(expr_paren.expr),
        _ => panic!("Requires a parenthesized format string and optional values"),
    };
    DisplayFormat::Direct(expr)
}

fn parse_snafu_display_nested(meta: syn::MetaList) -> DisplayFormat {
    use syn::{Expr, NestedMeta, Lit};

    let mut nested = meta.nested.into_iter().map(|nested| {
        let nested = match nested {
            NestedMeta::Literal(lit) => lit,
            _ => panic!("Only supports a list of literals"),
        };
        match nested {
            Lit::Str(s) => s,
            _ => panic!("Only supports a list of literal strings"),
        }
    });

    let fmt_str = nested.next().map(|x| Box::new(x) as Box<quote::ToTokens>);

    let fmt_args = nested.map(|nested| {
        nested.parse::<Expr>().expect("Strings after the first must be parsable as expressions")
    }).map(|x| Box::new(x) as Box<quote::ToTokens>);

    let nested = fmt_str.into_iter().chain(fmt_args).collect();

    DisplayFormat::Stringified(nested)
}

fn parse_snafu_display_nested_name_value(nv: syn::MetaNameValue) -> DisplayFormat {
    use syn::{Lit, Expr};

    let s = match nv.lit {
        Lit::Str(s) => s,
        _ => panic!("Only supports a litera strings"),
    };

    let expr = s.parse::<Expr>().expect("Must be a parsable as an expression");

    let expr: Box<quote::ToTokens> = match expr {
        Expr::Tuple(expr_tuple) => Box::new(expr_tuple.elems),
        Expr::Paren(expr_paren) => Box::new(expr_paren.expr),
        _ => panic!("Requires a parenthesized format string and optional values"),
    };

    DisplayFormat::Direct(expr)
}

fn generate_snafu(enum_info: EnumInfo) -> proc_macro2::TokenStream {
    use syn::Ident;
    use proc_macro2::Span;

    let enum_name = enum_info.name;

    let generated_variant_support = enum_info.variants.iter().map(|variant| {
        let VariantInfo { name: ref variant_name, ref source_field, ref user_fields, .. } = *variant;

        let generic_names: Vec<_> = (0..)
            .map(|i| Ident::new(&format!("T{}", i), Span::call_site()))
            .take(user_fields.len())
            .collect();
        let generic_names = &generic_names;

        let generics_list = quote! { <#(#generic_names),*> } ;
        let selector_name = quote! { #variant_name#generics_list };

        let names: Vec<_> = user_fields.iter().map(|f| f.name.clone()).collect();
        let names = &names;
        let types = generic_names;

        let variant_selector_struct = {
            if user_fields.is_empty() {
                quote! {
                    struct #selector_name;
                }
            } else {
                quote! {
                    struct #selector_name {
                        #( #names: #types ),*
                    }
                }
            }
        };

        let where_clauses: Vec<_> = generic_names.iter().zip(user_fields).map(|(gen_ty, f)| {
            let Field { ref ty, .. } = *f;
            quote! { #gen_ty: std::convert::Into<#ty> }
        }).collect();
        let where_clauses = &where_clauses;

        let inherent_impl = if source_field.is_none() {
            let names2 = names;
            quote! {
                impl#generics_list #selector_name
                where
                    #(#where_clauses),*
                {
                    fn fail<T>(self) -> std::result::Result<T, #enum_name> {
                        let Self { #(#names),* } = self;
                        let error = #enum_name::#variant_name {
                            #( #names: std::convert::Into::into(#names2) ),*
                        };
                        std::result::Result::Err(error)
                    }
                }
            }
        } else {
            quote! {}
        };

        let enum_from_variant_selector_impl = match *source_field {
            Some(ref source_field) => {
                let Field { name: ref source_name, ty: ref source_ty } = *source_field;

                let other_ty = quote! {
                    snafu::Context<#source_ty, #selector_name>
                };

                let user_fields = user_fields.iter().map(|f| {
                    let Field { ref name, .. } = *f;
                    quote! { #name: other.context.#name.into() }
                });

                quote! {
                    impl#generics_list std::convert::From<#other_ty> for #enum_name
                    where
                        #(#where_clauses),*
                    {
                        fn from(other: #other_ty) -> Self {
                            #enum_name::#variant_name {
                                #source_name: other.error,
                                #(#user_fields),*
                            }
                        }
                    }
                }
            }
            None => {
                quote! {}
            }
        };

        quote! {
            #variant_selector_struct
            #inherent_impl
            #enum_from_variant_selector_impl
        }
    });

    let variants = enum_info.variants.iter().map(|variant| {
        let VariantInfo { name: ref variant_name, ref user_fields, ref source_field, ref display_format, .. } = *variant;

        let format = match *display_format {
            Some(DisplayFormat::Stringified(ref fmt)) => {
                quote! { #(#fmt),* }
            }
            Some(DisplayFormat::Direct(ref fmt)) => {
                quote! { #fmt }
            }
            None => quote! { stringify!(#variant_name) },
        };


        let field_names = user_fields.iter().chain(source_field).map(|f| &f.name);
        let field_names = quote! { #(ref #field_names),* };

        quote! {
            #enum_name::#variant_name { #field_names } => {
                write!(f, #format)
            }
        }
    });

    let display_impl = quote! {
        impl std::fmt::Display for #enum_name {
            fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
                #[allow(unused_variables)]
                match *self {
                    #(#variants)*
                }
            }
        }
    };

    let variants = enum_info.variants.iter().map(|variant| {
        let VariantInfo { name: ref variant_name, .. } = *variant;
        quote! {
            #enum_name::#variant_name { .. } => stringify!(#enum_name::#variant_name),
        }
    });

    let description_fn = quote! {
        fn description(&self) -> &str {
            match *self {
                #(#variants)*
            }
        }
    };

    let variants: Vec<_> = enum_info.variants.iter().map(|variant| {
        let VariantInfo { name: ref variant_name, ref source_field, .. } = *variant;

        match *source_field {
            Some(ref source_field) => {
                let Field { name: ref field_name, .. } = *source_field;
                quote! {
                    #enum_name::#variant_name { ref #field_name, .. } => {
                        Some(std::borrow::Borrow::borrow(#field_name))
                    }
                }
            }
            None => {
                quote! {
                    #enum_name::#variant_name { .. } => { None }
                }
            }
        }
    }).collect();
    let variants = &variants;

    let cause_fn = quote! {
        fn cause(&self) -> Option<&std::error::Error> {
            match *self {
                #(#variants)*
            }
        }
    };

    let source_fn = if cfg!(feature = "rust_1_30") {
        quote! {
            fn source(&self) -> Option<&(std::error::Error + 'static)> {
                match *self {
                    #(#variants)*
                }
            }
        }
    } else {
        quote! {}
    };

    let error_impl = quote! {
        impl std::error::Error for #enum_name {
            #description_fn
            #cause_fn
            #source_fn
        }
    };

    quote! {
        #(#generated_variant_support)*
        #display_impl
        #error_impl
    }
}