codgenhelp 0.0.1

Simplifies structure and enum parsing in Macros1.1
Documentation

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
    }
}