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
#![crate_type = "proc-macro"]

#![recursion_limit = "192"]

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

use proc_macro::TokenStream;

#[proc_macro_derive(new, attributes(new))]
pub fn derive(input: TokenStream) -> TokenStream {
    let input: String = input.to_string();
    let ast = syn::parse_macro_input(&input).expect("Couldn't parse item");
    let result = match ast.body {
        syn::Body::Enum(ref variants) => new_for_enum(&ast, &variants),
        syn::Body::Struct(ref variant_data) => new_for_struct(&ast, &variant_data, None),
    };
    result.to_string().parse().expect("Couldn't parse string to tokens")
}


fn new_for_struct(ast: &syn::MacroInput, variant_data: &syn::VariantData,
                  variant: Option<&syn::Ident>) -> quote::Tokens
{
    match *variant_data {
        syn::VariantData::Struct(ref fields) => {
            new_impl(&ast, Some(&fields), true, variant)
        },
        syn::VariantData::Unit => {
            new_impl(&ast, None, false, variant)
        },
        syn::VariantData::Tuple(ref fields) => {
            new_impl(&ast, Some(&fields), false, variant)
        },
    }
}

fn new_for_enum(ast: &syn::MacroInput, variants: &[syn::Variant]) -> quote::Tokens {
    if variants.is_empty() {
        panic!("#[derive(new)] cannot be implemented for enums with zero variants");
    }
    let impls = variants.iter().map(|v| {
        if v.discriminant.is_some() {
            panic!("#[derive(new)] cannot be implemented for enums with discriminants");
        }
        new_for_struct(ast, &v.data, Some(&v.ident))
    });
    quote!(#(#impls)*)
}

fn new_impl(ast: &syn::MacroInput, fields: Option<&[syn::Field]>,
            named: bool, variant: Option<&syn::Ident>) -> quote::Tokens
{
    let name = &ast.ident;
    let unit = fields.is_none();
    let fields: Vec<_> = fields.unwrap_or(&[]).iter()
        .enumerate().map(|(i, f)| FieldExt::new(f, i, named)).collect();
    let args = fields.iter()
        .filter(|f| f.needs_arg()).map(|f| f.as_arg());
    let inits = fields.iter()
        .map(|f| f.as_init());
    let inits = if unit {
        quote!()
    } else if named {
        quote![{ #(#inits),* }]
    } else {
        quote![( #(#inits),* )]
    };
    let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
    let (new, qual, doc) = match variant {
        None => (
            syn::Ident::new("new"),
            quote!(),
            format!("Constructs a new `{}`.", name),
        ),
        Some(ref variant) => (
            syn::Ident::new(format!("new_{}", to_snake_case(variant.as_ref()))),
            quote!(::#variant),
            format!("Constructs a new `{}::{}`.", name, variant),
        ),
    };
    let lint_attrs = collect_parent_lint_attrs(&ast.attrs);
    let lint_attrs = quote![#(#lint_attrs),*];
    quote! {
        impl #impl_generics #name #ty_generics #where_clause {
            #[doc = #doc]
            #lint_attrs
            pub fn #new(#(#args),*) -> Self {
                #name #qual #inits
            }
        }
    }
}

fn collect_parent_lint_attrs(attrs: &[syn::Attribute]) -> Vec<syn::Attribute> {
    fn is_lint(item: &syn::MetaItem) -> bool {
        if let syn::MetaItem::List(ref ident, _) = *item {
            match ident.as_ref() {
                "allow" | "deny" | "forbid" | "warn" => return true,
                _ => (),
            }
        }
        false
    }

    fn is_cfg_attr_lint(item: &syn::MetaItem) -> bool {
        if let syn::MetaItem::List(ref ident, ref items) = *item {
            if ident.as_ref() == "cfg_attr" && items.len() == 2 {
                if let syn::NestedMetaItem::MetaItem(ref item) = items[1] {
                    return is_lint(item);
                }
            }
        }
        false
    }

    attrs.iter().filter(|attr| {
        is_lint(&attr.value) || is_cfg_attr_lint(&attr.value)
    }).cloned().collect()
}

enum FieldAttr {
    Default,
    Value(syn::Expr),
}

impl FieldAttr {
    pub fn as_tokens(&self) -> quote::Tokens {
        match *self {
            FieldAttr::Default => quote!(::std::default::Default::default()),
            FieldAttr::Value(ref s) => quote!(#s),
        }
    }

    pub fn parse(attrs: &[syn::Attribute]) -> Option<FieldAttr> {
        use syn::{MetaItem, NestedMetaItem, AttrStyle};

        let mut result = None;
        for attr in attrs.iter() {
            if attr.style == AttrStyle::Outer && attr.value.name() == "new" {
                if let MetaItem::List(_, ref items) = attr.value {
                    for item in items {
                        if result.is_some() {
                            panic!("Expected at most one #[new] attribute");
                        }
                        match *item {
                            NestedMetaItem::MetaItem(MetaItem::Word(ref ident)) => {
                                if ident.as_ref() == "default" {
                                    result = Some(FieldAttr::Default);
                                } else {
                                    panic!("Invalid #[new] attribute: #[new({})]", ident);
                                }
                            },
                            NestedMetaItem::MetaItem(MetaItem::NameValue(ref ident, ref lit)) => {
                                if let syn::Lit::Str(ref s, _) = *lit {
                                    if ident.as_ref() == "value" {
                                        let expr = syn::parse_expr(s.as_ref()).ok()
                                            .expect(&format!(
                                                "Invalid expression in #[new]: `{}`", s));
                                        result = Some(FieldAttr::Value(expr));
                                    } else {
                                        panic!("Invalid #[new] attribute: #[new({} = ..)]", ident);
                                    }
                                } else {
                                    panic!("Non-string literal value in #[new] attribute");
                                }
                            },
                            NestedMetaItem::MetaItem(MetaItem::List(ref ident, _)) => {
                                panic!("Invalid #[new] attribute: #[new({}(..))]", ident);
                            },
                            NestedMetaItem::Literal(_) => {
                                panic!("Invalid #[new] attribute: literal value in #[new(..)]");
                            }
                        }
                    }

                } else {
                    panic!("Invalid #[new] attribute, expected #[new(..)]");
                }
            }
        }
        result
    }

}

struct FieldExt<'a> {
    ty: &'a syn::Ty,
    attr: Option<FieldAttr>,
    ident: syn::Ident,
    named: bool,
}

impl<'a> FieldExt<'a> {
    pub fn new(field: &'a syn::Field, idx: usize, named: bool) -> FieldExt<'a> {
        FieldExt {
            ty: &field.ty,
            attr: FieldAttr::parse(&field.attrs),
            ident: if named {
                field.ident.clone().unwrap()
            } else {
                syn::Ident::new(format!("f{}", idx))
            },
            named: named,
        }
    }

    pub fn has_attr(&self) -> bool {
        self.attr.is_some()
    }

    pub fn is_phantom_data(&self) -> bool {
        match *self.ty {
            syn::Ty::Path(None, ref path) => {
                path.segments.as_slice().last()
                    .map(|x| x.ident.as_ref() == "PhantomData").unwrap_or(false)
            },
            _ => false,
        }
    }

    pub fn needs_arg(&self) -> bool {
        !self.has_attr() && !self.is_phantom_data()
    }

    pub fn as_arg(&self) -> quote::Tokens {
        let f_name = &self.ident;
        let ty = &self.ty;
        quote!(#f_name: #ty)
    }

    pub fn as_init(&self) -> quote::Tokens {
        let f_name = &self.ident;
        let init = if self.is_phantom_data() {
            quote!(::std::marker::PhantomData)
        } else {
            match self.attr {
                None => quote!(#f_name),
                Some(ref attr) => attr.as_tokens(),
            }
        };
        if self.named {
            quote!(#f_name: #init)
        } else {
            quote!(#init)
        }
    }
}

fn to_snake_case(s: &str) -> String {
    let (ch, next, mut acc): (Option<char>, Option<char>, String) =
        s.chars().fold((None, None, String::new()), |(prev, ch, mut acc), next| {
            if let Some(ch) = ch {
                if let Some(prev) = prev {
                    if ch.is_uppercase() {
                        if prev.is_lowercase() || prev.is_numeric() ||
                            (prev.is_uppercase() && next.is_lowercase())
                        {
                            acc.push('_');
                        }
                    }
                }
                acc.extend(ch.to_lowercase());
            }
            (ch, Some(next), acc)
        });
    if let Some(next) = next {
        if let Some(ch) = ch {
            if (ch.is_lowercase() || ch.is_numeric()) && next.is_uppercase() {
                acc.push('_');
            }
        }
        acc.extend(next.to_lowercase());
    }
    acc
}


#[test]
fn test_to_snake_case() {
    assert_eq!(to_snake_case(""), "");
    assert_eq!(to_snake_case("a"), "a");
    assert_eq!(to_snake_case("B"), "b");
    assert_eq!(to_snake_case("BC"), "bc");
    assert_eq!(to_snake_case("Bc"), "bc");
    assert_eq!(to_snake_case("bC"), "b_c");
    assert_eq!(to_snake_case("Fred"), "fred");
    assert_eq!(to_snake_case("CARGO"), "cargo");
    assert_eq!(to_snake_case("_Hello"), "_hello");
    assert_eq!(to_snake_case("QuxBaz"), "qux_baz");
    assert_eq!(to_snake_case("FreeBSD"), "free_bsd");
    assert_eq!(to_snake_case("specialK"), "special_k");
    assert_eq!(to_snake_case("hello1World"), "hello1_world");
    assert_eq!(to_snake_case("Keep_underscore"), "keep_underscore");
    assert_eq!(to_snake_case("ThisISNotADrill"), "this_is_not_a_drill");
}