codama_syn_helpers/extensions/
item.rs1use syn::Item;
2
3pub trait ItemExtension {
4 fn get_self(&self) -> &Item;
5
6 fn attributes(&self) -> Option<&Vec<syn::Attribute>> {
7 match self.get_self() {
8 Item::Const(i) => Some(&i.attrs),
9 Item::Enum(i) => Some(&i.attrs),
10 Item::ExternCrate(i) => Some(&i.attrs),
11 Item::Fn(i) => Some(&i.attrs),
12 Item::ForeignMod(i) => Some(&i.attrs),
13 Item::Impl(i) => Some(&i.attrs),
14 Item::Macro(i) => Some(&i.attrs),
15 Item::Mod(i) => Some(&i.attrs),
16 Item::Static(i) => Some(&i.attrs),
17 Item::Struct(i) => Some(&i.attrs),
18 Item::Trait(i) => Some(&i.attrs),
19 Item::TraitAlias(i) => Some(&i.attrs),
20 Item::Type(i) => Some(&i.attrs),
21 Item::Union(i) => Some(&i.attrs),
22 Item::Use(i) => Some(&i.attrs),
23 _ => None,
24 }
25 }
26}
27
28impl ItemExtension for Item {
29 fn get_self(&self) -> &Item {
30 self
31 }
32}
33
34#[cfg(test)]
35mod tests {
36 use super::*;
37
38 #[test]
39 fn attributes() {
40 let r#struct: Item = syn::parse_quote! {
41 #[derive(Debug)]
42 struct Foo(u32);
43 };
44 assert!(matches!(
45 r#struct.attributes().unwrap().as_slice(),
46 [syn::Attribute { .. }]
47 ));
48 }
49}