codeviz_java/
field_spec.rs

1use super::*;
2
3#[derive(Debug, Clone)]
4pub struct FieldSpec {
5    pub modifiers: Modifiers,
6    pub ty: Type,
7    pub name: String,
8    pub annotations: Vec<AnnotationSpec>,
9    pub initialize: Option<Statement>,
10}
11
12impl FieldSpec {
13    pub fn new<I>(modifiers: Modifiers, ty: I, name: &str) -> FieldSpec
14    where
15        I: Into<Type>,
16    {
17        FieldSpec {
18            modifiers: modifiers,
19            ty: ty.into(),
20            name: name.to_owned(),
21            annotations: Vec::new(),
22            initialize: None,
23        }
24    }
25
26    pub fn push_annotation<A>(&mut self, annotation: A)
27    where
28        A: Into<AnnotationSpec>,
29    {
30        self.annotations.push(annotation.into());
31    }
32
33    pub fn initialize<S>(&mut self, initialize: S)
34    where
35        S: Into<Statement>,
36    {
37        self.initialize = Some(initialize.into());
38    }
39}
40
41impl<'a, T> From<&'a T> for FieldSpec
42where
43    T: Into<FieldSpec> + Clone,
44{
45    fn from(value: &'a T) -> FieldSpec {
46        value.clone().into()
47    }
48}
49
50impl From<FieldSpec> for Variable {
51    fn from(value: FieldSpec) -> Variable {
52        Variable::Literal(value.name)
53    }
54}
55
56impl From<FieldSpec> for Statement {
57    fn from(value: FieldSpec) -> Statement {
58        let mut elements = Elements::new();
59
60        for a in value.annotations {
61            elements.push(a);
62        }
63
64        let mut s = Statement::new();
65
66        if !value.modifiers.is_empty() {
67            s.push(value.modifiers);
68            s.push(" ");
69        }
70
71        s.push(value.ty);
72        s.push(" ");
73        s.push(value.name);
74
75        if let Some(initialize) = value.initialize {
76            s.push(" = ");
77            s.push(initialize);
78        }
79
80        elements.push(s);
81
82        let mut out = Statement::new();
83        let element: Element = elements.into();
84        out.push(element);
85        out
86    }
87}