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
use syn::*;

pub struct CfgAttrs {
    pub name: Option<String>,
    pub docs: bool,
}

impl CfgAttrs {
    pub fn new(attrs: &[Attribute]) -> CfgAttrs {
        let cfg_attrs = filter_attrs(attrs);

        let mut cfg = CfgAttrs {
            name: None,
            docs: false,
        };

        // Parse the cfg attrs
        for attr in cfg_attrs {
            if let NestedMetaItem::MetaItem(ref attr) = *attr {
                match attr.name() {
                    "name" if cfg.name.is_some()    => panic!("Multiple `name` attributes"),
                    "name"                          => cfg.name = project_name(attr),
                    "generate_docs" if cfg.docs     => panic!("Multiple `generate_docs` attributes"),
                    "generate_docs"                 => cfg.docs = gen_docs(attr),
                    unknown                         => {
                        panic!("Unrecognized configure attribute `{}`", unknown)
                    }
                }
            } else { panic!("Unrecognized configure attribute literal") }
        }

        cfg
    }
}

pub struct FieldAttrs {
    pub docs: Option<String>,
}

impl FieldAttrs {
    pub fn new(field: &Field) -> FieldAttrs {

        let mut cfg = FieldAttrs { docs: None };

        let cfg_attrs = filter_attrs(&field.attrs);

        for attr in cfg_attrs {
            if let NestedMetaItem::MetaItem(ref attr) = *attr {
                match attr.name() {
                    "docs" if cfg.docs.is_some()    => {
                        let name = field.ident.as_ref().unwrap();
                        panic!("Multiple `docs` attributes on one field: `{}`.", name)
                    }
                    "docs"                          => {
                        cfg.docs = Some(field_docs(attr))
                    }
                    unknown                         => {
                        panic!("Unrecognized configure attribute `{}`", unknown)
                    }
                }
            } else { panic!("Unrecognized configure attribute literal") }
        }

        if cfg.docs.is_none() {
            cfg.docs = desugared_docs(&field.attrs);
        }

        cfg
    }
}

fn filter_attrs(attrs: &[Attribute]) -> Vec<&NestedMetaItem> {
    let mut cfg_attrs = vec![];
    for attr in attrs {
        match attr.value {
            MetaItem::List(ref name, ref members) if name.as_ref() == "configure"   => {
                cfg_attrs.extend(members);
            }
            _   => continue
        }
    }

    cfg_attrs
}

fn project_name(attr: &MetaItem) -> Option<String> {
    if let MetaItem::NameValue(_, ref name) = *attr {
        if let Lit::Str(ref string, _) = *name {
            return Some(string.clone())
        }
    }
    panic!("Unsupported `configure(name)` attribute; only supported form is #[configure(name = \"$NAME\")]")
}

fn gen_docs(attr: &MetaItem) -> bool {
    if let MetaItem::Word(_) = *attr {
        return true
    } else  {
        panic!("Unsupported `configure(docs)` attribute; only supported form is #[configure(docs)]")
    }
}

fn field_docs(attr: &MetaItem) -> String {
    if let MetaItem::NameValue(_, ref name) = *attr {
        if let Lit::Str(ref string, _) = *name {
            return string.clone()
        }
    }
    panic!("Unsupported `configure(docs)` attribute; only supported form is #[configure(docs = \"$NAME\")]")
}

fn desugared_docs(attrs: &[Attribute]) -> Option<String> {
    if let Some(attr) = attrs.iter().find(|attr| attr.is_sugared_doc) {
        if let MetaItem::NameValue(_, ref name) = attr.value {
            if let Lit::Str(ref string, _) = *name {
                return Some(string.trim_left_matches('/').to_owned())
            }
        }
    }
    None
}