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
use super::*; #[derive(Debug, Clone)] pub struct FieldSpec { pub modifiers: Modifiers, pub ty: Type, pub name: String, pub initialize: Option<Statement>, } impl FieldSpec { pub fn new<I>(modifiers: Modifiers, ty: I, name: &str) -> FieldSpec where I: Into<Type> { FieldSpec { modifiers: modifiers, ty: ty.into(), name: name.to_owned(), initialize: None, } } pub fn initialize<S>(&mut self, initialize: S) where S: Into<Statement> { self.initialize = Some(initialize.into()); } } impl<'a, T> From<&'a T> for FieldSpec where T: Into<FieldSpec> + Clone { fn from(value: &'a T) -> FieldSpec { value.clone().into() } } impl From<FieldSpec> for Variable { fn from(value: FieldSpec) -> Variable { Variable::Literal(value.name) } } impl From<FieldSpec> for Statement { fn from(value: FieldSpec) -> Statement { let mut s = Statement::new(); if !value.modifiers.is_empty() { s.push(value.modifiers); s.push(" "); } s.push(value.ty); s.push(" "); s.push(value.name); if let Some(initialize) = value.initialize { s.push(" = "); s.push(initialize); } s } }