1use crate::Generator;
2use crate::Indentation;
3
4#[derive(Debug)]
5pub enum Element {
6 Tag(String, String),
7 Text(String),
8 EmptyLine,
9}
10
11#[derive(Debug)]
12pub struct Document {
13 pub elements: Vec<Element>,
14}
15
16impl Document {
17 pub fn new() -> Self {
18 Self { elements: vec![] }
19 }
20
21 pub fn add_element(mut self, element: Element) -> Self {
22 self.elements.push(element);
23
24 self
25 }
26
27 pub fn empty_line(mut self) -> Self {
28 self.elements.push(Element::EmptyLine);
29
30 self
31 }
32
33 pub fn tag<T: ToString>(mut self, tag: T, description: T) -> Self {
34 self.elements
35 .push(Element::Tag(tag.to_string(), description.to_string()));
36
37 self
38 }
39
40 pub fn simple_tag<T: ToString>(mut self, tag: T) -> Self {
41 self.elements
42 .push(Element::Tag(tag.to_string(), String::new()));
43
44 self
45 }
46
47 pub fn text<T: ToString>(mut self, text: T) -> Self {
48 self.elements
49 .push(Element::Text(format!("{}\n", text.to_string())));
50
51 self
52 }
53}
54
55impl Generator for Document {
56 fn generate(&self, indentation: Indentation, level: usize) -> String {
57 let mut code = String::new();
58
59 code.push_str(&format!("{}/**\n", indentation.value(level)));
60
61 for element in &self.elements {
62 let element = element.generate(indentation, level);
63 if element.is_empty() {
64 code.push_str(&format!("{} *\n", indentation.value(level)));
65
66 continue;
67 }
68
69 for line in element.lines() {
70 code.push_str(&format!(
71 "{} * {}\n",
72 indentation.value(level),
73 line.trim_end()
74 ));
75 }
76 }
77
78 code.push_str(&format!("{} */\n", indentation.value(level)));
79
80 code
81 }
82}
83
84impl Generator for Element {
85 fn generate(&self, _: Indentation, _: usize) -> String {
86 match self {
87 Element::Tag(tag, description) => {
88 if description.is_empty() {
89 format!("@{}", tag)
90 } else {
91 format!("@{} {}", tag, description)
92 }
93 }
94 Element::Text(text) => text.to_string(),
95 Element::EmptyLine => String::new(),
96 }
97 }
98}
99
100impl Default for Document {
101 fn default() -> Self {
102 Self::new()
103 }
104}