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
150
151

use prelude::*;
use syn::{ Attribute, FnArg, GenericArgument, Ident, Item, Pat, Path, Type, TypeReference };

/// Extract the underlying Type from various AST types.
pub trait GetType {

    /// Gets the Type from the AST element.
    fn get_ty( &self ) -> Result<Type, String>;
}

impl GetType for FnArg {

    fn get_ty( &self ) -> Result<Type, String>
    {
        Ok( match *self {
            FnArg::Captured( ref c ) => c.ty.clone(),
            FnArg::Ignored( ref ty ) => ty.clone(),
            FnArg::SelfRef( ref s )
                => Type::Reference( TypeReference {
                        and_token: parse_quote!( & ),
                        lifetime: s.lifetime.clone(),
                        mutability: s.mutability,
                        elem: Box::new( parse_quote!( Self ) )
                } ),
            FnArg::SelfValue(_) => self_ty(),
            FnArg::Inferred(_)
                => return Err( "Inferred arguments not supported".to_string() ),
        } )
    }
}

impl GetType for GenericArgument {

    fn get_ty( &self ) -> Result<Type, String>
    {
        match *self {
            GenericArgument::Type( ref ty ) => Ok( ty.clone() ),
            _ => Err( "Expected type parameter".to_string() )
        }
    }
}

pub trait GetIdent {

    /// Gets the Ident from the AST element.
    fn get_ident( &self ) -> Result<Ident, String>;
}

impl GetIdent for FnArg {

    fn get_ident( &self ) -> Result<Ident, String> {

        Ok( match *self {
            FnArg::SelfRef(..) | FnArg::SelfValue(..)
                => Ident::new( "self", Span::call_site() ),
            FnArg::Captured( ref c ) => match c.pat {
                Pat::Ident( ref i ) => i.ident.clone(),
                _ => return Err( format!( "Unsupported argument: {:?}", self ) ),
            },
            FnArg::Ignored(..) => Ident::new( "_", Span::call_site() ),
            FnArg::Inferred(_)
                => return Err( "Inferred arguments not supported".to_string() ),
        } )
    }
}

impl GetIdent for Path {

    fn get_ident( &self ) -> Result<Ident, String> {

        self.segments.last().map( |l| l.value().ident.clone() )
                .ok_or_else( || "Empty path".to_owned() )
    }
}

impl GetIdent for Type {

    fn get_ident( &self ) -> Result<Ident, String> {

        match *self {
            Type::Path( ref p ) => p.path.get_ident(),
            _ => Err( format!( "Cannot get Ident for {:?}", self ) )
        }
    }
}

impl GetIdent for Item {
    fn get_ident( &self ) -> Result<Ident, String>
    {
        Ok( match *self {
            Item::ExternCrate( ref i ) => i.ident.clone(),
            Item::Static( ref i ) => i.ident.clone(),
            Item::Const( ref i ) => i.ident.clone(),
            Item::Fn( ref i ) => i.ident.clone(),
            Item::Mod( ref i ) => i.ident.clone(),
            Item::Type( ref i ) => i.ident.clone(),
            Item::Struct( ref i ) => i.ident.clone(),
            Item::Enum( ref i ) => i.ident.clone(),
            Item::Union( ref i ) => i.ident.clone(),
            Item::Trait( ref i ) => i.ident.clone(),
            Item::Impl( ref i ) => return i.self_ty.get_ident(),
            Item::Macro( ref m ) => return m.mac.path.get_ident(),
            Item::Macro2( ref i ) => i.ident.clone(),
            Item::Existential( ref i ) => i.ident.clone(),
            Item::TraitAlias( ref i ) => i.ident.clone(),

            Item::Use( .. )
                | Item::ForeignMod( .. )
                | Item::Verbatim( .. )
                => return Err( "Item type not supported for Ident".to_string() ),
        } )
    }
}

pub trait GetAttributes {

    /// Gets the Attributes from the AST element.
    fn get_attributes( &self ) -> Result<Vec<Attribute>, String>;
}

impl GetAttributes for Item {

    fn get_attributes( &self ) -> Result<Vec<Attribute>, String>
    {
        Ok( match *self {
            Item::ExternCrate( ref i ) => i.attrs.clone(),
            Item::Static( ref i ) => i.attrs.clone(),
            Item::Const( ref i ) => i.attrs.clone(),
            Item::Fn( ref i ) => i.attrs.clone(),
            Item::Mod( ref i ) => i.attrs.clone(),
            Item::Type( ref i ) => i.attrs.clone(),
            Item::Struct( ref i ) => i.attrs.clone(),
            Item::Enum( ref i ) => i.attrs.clone(),
            Item::Union( ref i ) => i.attrs.clone(),
            Item::Trait( ref i ) => i.attrs.clone(),
            Item::Impl( ref i ) => i.attrs.clone(),
            Item::Macro( ref i ) => i.attrs.clone(),
            Item::Macro2( ref i ) => i.attrs.clone(),
            Item::Use( ref i ) => i.attrs.clone(),
            Item::ForeignMod( ref i ) => i.attrs.clone(),
            Item::Existential( ref i ) => i.attrs.clone(),
            Item::TraitAlias( ref i ) => i.attrs.clone(),
            Item::Verbatim( .. ) => vec![],
        } )
    }
}

fn self_ty() -> Type {
    parse_quote!( Self )
}