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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use syn::{Attribute, Error, Item, ItemMod, Result};

/// Extension for [syn::Item]
pub trait ItemExt {
    /// Returns reference of inner attrs if not verbatim; otherwise `Err`
    fn attrs(&self) -> Result<&[Attribute]>;
    /// Returns mutable reference of inner attrs if not verbatim; otherwise `Err`
    fn attrs_mut(&mut self) -> Result<&mut Vec<Attribute>>;
}

impl ItemExt for Item {
    fn attrs(&self) -> Result<&[Attribute]> {
        use syn::Item::*;
        use syn::*;
        let attrs = match self {
            Const(ItemConst { ref attrs, .. }) => attrs,
            Enum(ItemEnum { ref attrs, .. }) => attrs,
            ExternCrate(ItemExternCrate { ref attrs, .. }) => attrs,
            Fn(ItemFn { ref attrs, .. }) => attrs,
            ForeignMod(ItemForeignMod { ref attrs, .. }) => attrs,
            Impl(ItemImpl { ref attrs, .. }) => attrs,
            Macro(ItemMacro { ref attrs, .. }) => attrs,
            Macro2(ItemMacro2 { ref attrs, .. }) => attrs,
            Mod(ItemMod { ref attrs, .. }) => attrs,
            Static(ItemStatic { ref attrs, .. }) => attrs,
            Struct(ItemStruct { ref attrs, .. }) => attrs,
            Trait(ItemTrait { ref attrs, .. }) => attrs,
            TraitAlias(ItemTraitAlias { ref attrs, .. }) => attrs,
            Type(ItemType { ref attrs, .. }) => attrs,
            Union(ItemUnion { ref attrs, .. }) => attrs,
            Use(ItemUse { ref attrs, .. }) => attrs,
            other => {
                return Err(Error::new_spanned(
                    other,
                    "this kind of item doesn't have attrs",
                ))
            }
        };
        Ok(attrs)
    }

    fn attrs_mut(&mut self) -> Result<&mut Vec<Attribute>> {
        use syn::Item::*;
        use syn::*;
        let attrs = match self {
            Const(ItemConst { ref mut attrs, .. }) => attrs,
            Enum(ItemEnum { ref mut attrs, .. }) => attrs,
            ExternCrate(ItemExternCrate { ref mut attrs, .. }) => attrs,
            Fn(ItemFn { ref mut attrs, .. }) => attrs,
            ForeignMod(ItemForeignMod { ref mut attrs, .. }) => attrs,
            Impl(ItemImpl { ref mut attrs, .. }) => attrs,
            Macro(ItemMacro { ref mut attrs, .. }) => attrs,
            Macro2(ItemMacro2 { ref mut attrs, .. }) => attrs,
            Mod(ItemMod { ref mut attrs, .. }) => attrs,
            Static(ItemStatic { ref mut attrs, .. }) => attrs,
            Struct(ItemStruct { ref mut attrs, .. }) => attrs,
            Trait(ItemTrait { ref mut attrs, .. }) => attrs,
            TraitAlias(ItemTraitAlias { ref mut attrs, .. }) => attrs,
            Type(ItemType { ref mut attrs, .. }) => attrs,
            Union(ItemUnion { ref mut attrs, .. }) => attrs,
            Use(ItemUse { ref mut attrs, .. }) => attrs,
            other => {
                return Err(Error::new_spanned(
                    other,
                    "this kind of item doesn't have attrs",
                ))
            }
        };
        Ok(attrs)
    }
}

/// Extension for [syn::ItemMod]
pub trait ItemModExt {
    /// Returns reference of content items without braces unless a declaration
    fn unbraced_content(&self) -> Result<&[Item]>;
    /// Returns reference of content items without braces unless a declaration
    fn unbraced_content_mut(&mut self) -> Result<&mut Vec<Item>>;
}

impl ItemModExt for ItemMod {
    fn unbraced_content(&self) -> Result<&[Item]> {
        if let Some((_, content)) = self.content.as_ref() {
            Ok(content)
        } else {
            Err(Error::new_spanned(
                self,
                "module declaration doesn't have content",
            ))
        }
    }
    fn unbraced_content_mut(&mut self) -> Result<&mut Vec<Item>> {
        if self.content.is_some() {
            let (_, content) = self.content.as_mut().unwrap();
            Ok(content)
        } else {
            Err(Error::new_spanned(
                self,
                "module declaration doesn't have content",
            ))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::assert_quote_eq;
    use quote::quote;
    use syn::parse_quote;

    #[test]
    fn test_attrs() {
        let mut item: Item = parse_quote!(
            #[test]
            type A = u32;
        );
        let expected: Attribute = parse_quote!(#[test]);
        {
            let attr = &item.attrs().unwrap()[0];
            assert_quote_eq!(attr, expected);
        }
        {
            let attr = &item.attrs_mut().unwrap()[0];
            assert_quote_eq!(attr, expected);
        }
    }

    #[test]
    fn test_unbraced_content() {
        let module: ItemMod = parse_quote!(
            mod m {
                static x: usize = 0;
                fn f() {}
            }
        );
        let content = module.unbraced_content().unwrap();
        assert!(matches!(content[0], Item::Static(_)));
        assert!(matches!(content[1], Item::Fn(_)));
    }

    #[test]
    fn test_unbraced_content_decl() {
        let module: ItemMod = parse_quote!(
            mod m;
        );
        assert!(module.unbraced_content().is_err());
    }
}