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
use syn::{
    punctuated::Punctuated, spanned::Spanned, token::Comma, Attribute, Error, Ident, Lit, Meta,
    MetaList, NestedMeta, Path, Result,
};

// Parses an attribute meta into Options
macro_rules! meta_parser {
    {
        $list:expr => [$($key:literal => $var:ident,)*]
    } => {
        $(let mut $var = None;)*
        for element in $list {
            match element {
                NestedMeta::Meta(meta) => match meta {
                    Meta::NameValue(nv) => {
                        let name = nv.path.get_ident()
                            .ok_or(Error::new(nv.path.span(), "unexpected key"))?
                            .to_string();
                        match name.as_str() {
                            $(
                                $key => {
                                    if $var.is_none() {
                                        $var = Some(&nv.lit);
                                    } else {
                                        return Err(Error::new(nv.path.span(), "key already provided"));
                                    }
                                }
                            )*
                            _ => return Err(Error::new(nv.path.span(), "unexpected key")),
                        };
                    }
                    _ => return Err(Error::new(meta.span(), "expected name-value pair")),
                }
                NestedMeta::Lit(lit) => return Err(Error::new(
                    lit.span(),
                    "unexpected literal, expected name-value pair",
                )),
            }
        }
    }
}

pub(crate) fn parse_attributes<I, F>(attrs: I, mut f: F) -> Result<Vec<Attribute>>
where
    I: Iterator<Item = Attribute>,
    F: FnMut(&Ident, &Punctuated<NestedMeta, Comma>) -> Result<bool>,
{
    let mut retained = Vec::new();
    for attr in attrs {
        // if not in meta list form, ignore the attribute
        let MetaList { path, nested, .. } = if let Ok(Meta::List(list)) = attr.parse_meta() {
            list
        } else {
            retained.push(attr);
            continue;
        };

        // if meta path isn't just an ident, ignore the attribute
        let path = if let Some(ident) = path.get_ident() {
            ident
        } else {
            retained.push(attr);
            continue;
        };

        if !f(path, &nested)? {
            retained.push(attr);
        }
    }
    Ok(retained)
}

pub(crate) fn parse_crate_path(nested: &Punctuated<NestedMeta, Comma>) -> Result<Path> {
    meta_parser! {
        nested => [
            "path" => crate_path,
        ]
    }
    if let Lit::Str(crate_path) =
        crate_path.ok_or_else(|| Error::new(nested.span(), "expected key 'path'"))?
    {
        Ok(crate_path.parse()?)
    } else {
        Err(Error::new(crate_path.span(), "expected literal string"))
    }
}