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

use std::ops::Index;
use super::super::syn::Field as SynField;
use super::super::Ty as Ty;
type SynFields = Vec<SynField>;

#[derive(Clone,Debug)]
pub struct Field {
    pub id: Option<String>,
    pub ty: Ty
}
impl Field {

    /// Build a field from the `syn` crate field type
    pub fn from_syn_field(x: &SynField) -> Field {
        Field {
            id: match &x.ident {
                &Option::None => None,
                &Option::Some(ref x) => Some(x.to_string())
            },
            ty: x.ty.clone()
        }
    }
    pub fn get_name<'a>(&'a self) -> Option<&'a str> {
        match &self.id {
            &Option::Some(ref s) => Some(s),
            _ => None
        }
    }
}

/// A collect of individual field
#[derive(Clone,Debug)]
pub struct Fields(pub Vec<Field>);
impl From<SynFields> for Fields {
    /// Bulk conversion of several fields at once
    fn from(x: SynFields) -> Fields {
        let mut v = Vec::with_capacity(x.len());
        for item in x {
            v.push(Field::from_syn_field(&item));
        }
        Fields(v)
    }
}
impl<'a> From<&'a SynFields> for Fields {
    /// Bulk conversion of several fields at once
    fn from(x: &'a SynFields) -> Fields {
        let mut v = Vec::with_capacity(x.len());
        for item in x {
            v.push(Field::from_syn_field(&item));
        }
        Fields(v)
    }

}
impl Index<usize> for Fields {
    type Output = Field;
    #[inline(always)]
    fn index(&self, x: usize) -> &Self::Output {
        self.0.index(x)
    }
}
impl Fields {
    /// Get number of args
    #[inline(always)]
    pub fn len(&self) -> usize {
        self.0.len()
    }
    /// Get id of an argument based on a position
    ///
    /// This returns the name of an arg. If the ID doesn't
    /// exist (which means we're working with a tuple)
    pub fn get_id(&self, x: usize) -> String {
        match self[x].id {
            Option::Some(ref i) => i.to_string(),
            Option::None => x.to_string()
        }
    }
    /// Get Type of an argument
    ///
    /// Return's the Type of an arg.
    #[inline(always)]
    pub fn get_ty<'a>(&'a self, x: usize) -> &'a Ty {
        &self[x].ty
    }
}