codama_syn_helpers/extensions/
item.rs

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

pub trait ItemExtension {
    fn get_self(&self) -> &Item;

    fn attributes(&self) -> Option<&Vec<syn::Attribute>> {
        match self.get_self() {
            Item::Const(i) => Some(&i.attrs),
            Item::Enum(i) => Some(&i.attrs),
            Item::ExternCrate(i) => Some(&i.attrs),
            Item::Fn(i) => Some(&i.attrs),
            Item::ForeignMod(i) => Some(&i.attrs),
            Item::Impl(i) => Some(&i.attrs),
            Item::Macro(i) => Some(&i.attrs),
            Item::Mod(i) => Some(&i.attrs),
            Item::Static(i) => Some(&i.attrs),
            Item::Struct(i) => Some(&i.attrs),
            Item::Trait(i) => Some(&i.attrs),
            Item::TraitAlias(i) => Some(&i.attrs),
            Item::Type(i) => Some(&i.attrs),
            Item::Union(i) => Some(&i.attrs),
            Item::Use(i) => Some(&i.attrs),
            _ => None,
        }
    }
}

impl ItemExtension for Item {
    fn get_self(&self) -> &Item {
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn attributes() {
        let r#struct: Item = syn::parse_quote! {
            #[derive(Debug)]
            struct Foo(u32);
        };
        assert!(matches!(
            r#struct.attributes().unwrap().as_slice(),
            [syn::Attribute { .. }]
        ));
    }
}