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

use syn::*;

/// Extract the underlying Ty from various AST types.
pub trait GetTy {

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

impl GetTy for FnArg {

    fn get_ty( &self ) -> Result<Ty, String>
    {
        Ok( match *self {
            FnArg::Captured( _, ref ty )
                | FnArg::Ignored( ref ty )
                => ty.clone(),
            FnArg::SelfRef( ref life, m )
                => Ty::Rptr( life.clone(), Box::new( MutTy {
                    mutability: m,
                    ty: self_ty()
                } ) ),
            FnArg::SelfValue(_)
                => self_ty(),
        } )
    }
}

impl GetTy for FunctionRetTy {

    fn get_ty( &self ) -> Result<Ty, String>
    {
        Ok( match *self {
            FunctionRetTy::Ty( ref ty ) => ty.clone(),
            FunctionRetTy::Default => unit_ty(),
        } )
    }
}

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::from( "self" ),
            FnArg::Captured( ref pat, _ ) => match *pat {
                Pat::Ident( _, ref i, _ ) => i.clone(),
                _ => Err( format!( "Unsupported argument: {:?}", self ) )?,
            },
            FnArg::Ignored(..) => Ident::from( "_" ),
        } )
    }
}

impl GetIdent for Path {

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

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

impl<'a> GetIdent for ::utils::AttrParam<'a> {

    fn get_ident( &self ) -> Result<Ident, String>
    {
        match *self {
            ::utils::AttrParam::Word( ident )
                => Ok( ident.clone() ),
            _ => Err( format!( "Unsupported AttrParam kind: {:?}", self ) ),
        }
    }
}

impl GetIdent for NestedMetaItem {

    fn get_ident( &self ) -> Result<Ident, String>
    {
        match *self {
            NestedMetaItem::MetaItem( ref mi ) => mi.get_ident(),
            _ => Err( format!( "Unsupported meta item kind: {:?}", self ) ),
        }
    }
}

impl GetIdent for MetaItem {
    fn get_ident( &self ) -> Result<Ident, String>
    {
        match *self {
            MetaItem::Word( ref i )
                | MetaItem::List( ref i, .. )
                | MetaItem::NameValue( ref i, .. )
                => Ok( i.clone() )
        }
    }
}

fn self_ty() -> Ty {
    Ty::Path(
        None,
        Path::from( PathSegment::from( Ident::from( "Self" ) ) )
    )
}

fn unit_ty() -> Ty {
    Ty::Tup( vec![] )
}