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
//! derive for

extern crate proc_macro;
extern crate syn;
#[macro_use]
extern crate synstructure;
#[macro_use]
extern crate quote;

decl_derive!([ErrorKind, attributes(msg)] => error_kind_derive);

fn error_kind_derive(s: synstructure::Structure) -> quote::Tokens {
    let short_body = s.each_variant(|v| {
        if let Some(msg) = find_msg(&v.ast().attrs) {
            let metas = msg.nested;
            if metas.is_empty() {
                panic!("You have to implement `#[msg(short = \"your description\")]`");
            }
            let mut filterd = metas.iter().filter_map(process_short);
            if let Some(tokens) = filterd.next() {
                if filterd.next().is_some() {
                    panic!("Cannot implment short=.. multiple times");
                }
                tokens
            } else {
                panic!("You have to implement `short = ..` attribute")
            }
        } else {
            let par_name = s.ast().ident.as_ref();
            let variant_name = v.ast().ident.as_ref();
            if par_name != variant_name {
                quote!(concat!(#par_name, "::", #variant_name))
            } else {
                quote!(#par_name)
            }
        }
    });
    let mut has_detailed = false;
    let detailed_body = s.each_variant(|v| {
        if let Some(t) = process_detailed(v) {
            has_detailed = true;
            t
        } else {
            quote!(String::new())
        }
    });
    let mut res = s.bound_impl(
        quote!(::error_chain_mini::ErrorKind),
        quote! {
            fn short(&self) -> &str {
                match *self { #short_body }
            }
            fn detailed(&self) -> String {
                match *self { #detailed_body }
            }
        },
    );
    let display_body = if has_detailed {
        quote!(write!(f, "{} {{ {} }}", self.short(), self.detailed()))
    } else {
        quote!(write!(f, "{}", self.short()))
    };
    let display = s.bound_impl(
        quote!(::std::fmt::Display),
        quote! {
            fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
                #display_body
            }
        },
    );
    res.append_all(display);
    res
}

fn process_detailed(variant: &synstructure::VariantInfo) -> Option<quote::Tokens> {
    let msg = find_msg(&variant.ast().attrs)?;
    let nested = msg.nested;
    let mut iter = nested.iter().skip_while(|nested| match nested {
        syn::NestedMeta::Meta(syn::Meta::NameValue(nameval)) => nameval.ident != "detailed",
        _ => panic!("Invlaid Value"),
    });
    let detailed = iter.next()?;
    let s = if let syn::NestedMeta::Meta(syn::Meta::NameValue(nameval)) = detailed {
        if let syn::Lit::Str(ref lit) = nameval.lit {
            lit.value()
        } else {
            panic!("Only string is allowed after detailed=")
        }
    } else {
        unreachable!();
    };
    macro_rules! get_nth {
        ($id: expr) => {{
            let bi = variant.bindings().into_iter().nth($id);
            if let Some(bi) = bi {
                bi.binding
            } else {
                panic!("Invalid index {} for {:?}", $id, variant.prefix);
            }
        }};
    }
    let args = iter.take_while(|arg| {
        if let syn::NestedMeta::Meta(syn::Meta::NameValue(_)) = arg {
            false
        } else {
            true
        }
    }).map(|arg| match arg {
        syn::NestedMeta::Literal(syn::Lit::Int(int)) => {
            let idx = int.value() as usize;
            let bi = get_nth!(idx);
            quote!(#bi)
        }
        syn::NestedMeta::Meta(syn::Meta::Word(ident)) => {
            if ident.as_ref().starts_with("_") {
                if let Ok(idx) = ident.as_ref()[1..].parse::<usize>() {
                    let bi = get_nth!(idx);
                    return quote!(#bi);
                }
            }
            if let Some(bi) = variant
                .bindings()
                .into_iter()
                .find(|bi| bi.ast().ident.as_ref() == Some(ident))
            {
                let bi = bi.binding;
                quote!(#bi)
            } else {
                panic!(
                    "Invalid argument {} for {:?}",
                    ident.as_ref(),
                    variant.prefix
                );
            }
        }
        _ => panic!("Invalid argument to detailed=.."),
    });
    Some(quote!(format!(#s #(, #args)*)))
}

fn process_short(nested: &syn::NestedMeta) -> Option<quote::Tokens> {
    match nested {
        syn::NestedMeta::Meta(syn::Meta::NameValue(nameval)) => {
            if nameval.ident == "detailed" {
                return None;
            }
            if nameval.ident != "short" {
                panic!("only short=.. or detailed=.. is allowed");
            }
            if let syn::Lit::Str(ref lit_str) = nameval.lit {
                let val = lit_str.value();
                Some(quote!(#val))
            } else {
                panic!("Oly string literal is allowed after short")
            }
        }
        _ => None,
    }
}

fn find_msg(attrs: &[syn::Attribute]) -> Option<syn::MetaList> {
    let mut res = None;
    for attr in attrs {
        let meta = attr.interpret_meta();
        if let Some(syn::Meta::List(list)) = meta {
            if list.ident == "msg" {
                if res.is_some() {
                    panic!("Cannot have multiple `msg` attributes");
                }
                res = Some(list);
            }
        }
    }
    res
}