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
/*! 
Elasticsearch Core Types Codegen

This crate contains the internals for `elastic_types`-related codegen.
*/

#![recursion_limit = "128"]

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

#[macro_use]
extern crate quick_error;

#[macro_use]
extern crate nom;

extern crate serde;
extern crate serde_derive_internals;
extern crate serde_json;

extern crate chrono;

pub mod elastic_type;
pub mod date_format;

fn get_elastic_meta_items(attr: &syn::Attribute) -> Option<&[syn::NestedMetaItem]> {
    match attr.value {
        // Get elastic meta items
        syn::MetaItem::List(ref name, ref items) if name == &"elastic" => Some(items),
        _ => None,
    }
}

// Get the mapping ident supplied by an #[elastic()] attribute or create a default one
fn get_elastic_attr_name_value<'a>(name: &str, item: &'a syn::MacroInput) -> Option<&'a syn::Lit> {
    for meta_items in item.attrs.iter().filter_map(get_elastic_meta_items) {
        for meta_item in meta_items {
            match *meta_item {
                // Parse `#[elastic({name}="foo")]`
                syn::NestedMetaItem::MetaItem(syn::MetaItem::NameValue(ref key, ref lit)) if key == name => {
                    return Some(lit);
                }
                _ => (),
            }
        }
    }

    None
}

fn get_ident_from_lit(lit: &syn::Lit) -> Result<syn::Ident, &'static str> {
    get_str_from_lit(lit).map(|s| syn::Ident::from(s))
}

fn get_str_from_lit<'a>(lit: &'a syn::Lit) -> Result<&'a str, &'static str> {
    match *lit {
        syn::Lit::Str(ref s, _) => Ok(s.as_str()),
        _ => {
            return Err("Unable to get str from lit");
        }
    }
}