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
use super::*;

#[derive(Debug, Clone)]
pub struct ClassSpec {
    pub modifiers: Modifiers,
    pub name: String,
    pub annotations: Vec<AnnotationSpec>,
    pub fields: Vec<FieldSpec>,
    pub constructors: Vec<ConstructorSpec>,
    pub elements: Elements,
    pub extends: Option<ClassType>,
    pub implements: Vec<ClassType>,
}

impl ClassSpec {
    pub fn new(modifiers: Modifiers, name: &str) -> ClassSpec {
        ClassSpec {
            modifiers: modifiers,
            name: name.to_owned(),
            annotations: Vec::new(),
            fields: Vec::new(),
            constructors: Vec::new(),
            elements: Elements::new(),
            extends: None,
            implements: Vec::new(),
        }
    }

    pub fn extends<T>(&mut self, ty: T)
        where T: Into<ClassType>
    {
        self.extends = Some(ty.into());
    }
}

impl From<ClassSpec> for Element {
    fn from(value: ClassSpec) -> Element {
        let mut elements = Elements::new();

        for a in &value.annotations {
            elements.push(a);
        }

        let mut open = Statement::new();

        if !value.modifiers.is_empty() {
            open.push(value.modifiers);
            open.push(" ");
        }

        open.push("class ");
        open.push(&value.name);

        if let Some(ref extends) = value.extends {
            open.push(" extends ");
            open.push(extends);
        }

        implements(&value.implements, &mut open);

        open.push(" {");

        elements.push(open);

        let mut class_body = Elements::new();

        if !value.fields.is_empty() {
            let mut fields = Elements::new();

            for field in &value.fields {
                let mut field: Statement = field.into();
                field.push(";");
                fields.push(field);
            }

            class_body.push(fields);
        }

        for constructor in value.constructors {
            class_body.push(constructor.as_element(&value.name));
        }

        for element in &value.elements.elements {
            class_body.push(element);
        }

        elements.push_nested(class_body.join(Spacing));
        elements.push("}");

        elements.into()
    }
}

impl From<ClassType> for Variable {
    fn from(value: ClassType) -> Variable {
        Variable::Type(value.into())
    }
}