1use crate::attribute::AttributeGroup;
2use crate::comment::Document;
3use crate::constant::ClassConstant;
4use crate::method::Method;
5use crate::modifiers::Modifier;
6use crate::property::Property;
7use crate::usage::Usage;
8use crate::Generator;
9use crate::Indentation;
10
11#[derive(Debug)]
12pub struct Class {
13 pub documentation: Option<Document>,
14 pub attributes: Vec<AttributeGroup>,
15 pub modifiers: Vec<Modifier>,
16 pub name: String,
17 pub extends: Option<String>,
18 pub implements: Vec<String>,
19 pub usages: Vec<Usage>,
20 pub constants: Vec<ClassConstant>,
21 pub properties: Vec<Property>,
22 pub methods: Vec<Method>,
23}
24
25impl Class {
26 pub fn new<T: ToString>(name: T) -> Self {
27 Self {
28 documentation: None,
29 attributes: vec![],
30 modifiers: vec![],
31 name: name.to_string(),
32 extends: None,
33 implements: vec![],
34 usages: vec![],
35 constants: vec![],
36 properties: vec![],
37 methods: vec![],
38 }
39 }
40
41 pub fn document(mut self, documentation: Document) -> Self {
42 self.documentation = Some(documentation);
43
44 self
45 }
46
47 pub fn attributes(mut self, attributes: AttributeGroup) -> Self {
48 self.attributes.push(attributes);
49
50 self
51 }
52
53 pub fn modifier(mut self, modifier: Modifier) -> Self {
54 self.modifiers.push(modifier);
55
56 self
57 }
58
59 pub fn extends<T: ToString>(mut self, extends: T) -> Self {
60 self.extends = Some(extends.to_string());
61
62 self
63 }
64
65 pub fn implements<T: ToString>(mut self, implements: T) -> Self {
66 self.implements.push(implements.to_string());
67
68 self
69 }
70
71 pub fn using<T: Into<Usage>>(mut self, usage: T) -> Self {
72 self.usages.push(usage.into());
73
74 self
75 }
76
77 pub fn constant<T: Into<ClassConstant>>(mut self, constant: T) -> Self {
78 self.constants.push(constant.into());
79
80 self
81 }
82
83 pub fn property(mut self, property: Property) -> Self {
84 self.properties.push(property);
85
86 self
87 }
88
89 pub fn method(mut self, method: Method) -> Self {
90 self.methods.push(method);
91
92 self
93 }
94}
95
96impl Generator for Class {
97 fn generate(&self, indentation: Indentation, level: usize) -> String {
98 let mut code = String::new();
99
100 if let Some(documentation) = &self.documentation {
101 code.push_str(&documentation.generate(indentation, level));
102 }
103
104 for attribute in &self.attributes {
105 code.push_str(&attribute.generate(indentation, level));
106 }
107
108 for modifier in &self.modifiers {
109 code.push_str(&format!("{} ", modifier.generate(indentation, level)));
110 }
111
112 code.push_str(&format!("class {}", self.name));
113
114 if let Some(extends) = &self.extends {
115 code.push_str(&format!(" extends {}", extends));
116 }
117
118 if !self.implements.is_empty() {
119 code.push_str(&format!(
120 " implements {}",
121 self.implements
122 .iter()
123 .map(|implement| implement.to_string())
124 .collect::<Vec<String>>()
125 .join(", ")
126 ));
127 }
128
129 code.push_str("\n{\n");
130
131 code.push_str(self.usages.generate(indentation, level + 1).as_str());
132 code.push_str(self.constants.generate(indentation, level + 1).as_str());
133 code.push_str(self.properties.generate(indentation, level + 1).as_str());
134 code.push_str(self.methods.generate(indentation, level + 1).as_str());
135
136 code = code.trim_end().to_string();
137 code.push_str("\n}\n");
138
139 code
140 }
141}
142
143impl Generator for Vec<Class> {
144 fn generate(&self, indentation: Indentation, level: usize) -> String {
145 let mut code = String::new();
146 if self.is_empty() {
147 return code;
148 }
149
150 for class in self {
151 code.push_str(class.generate(indentation, level).as_str());
152 code.push('\n');
153 }
154
155 code
156 }
157}