|
1 | 1 | use JsonValue; |
2 | 2 |
|
3 | | -pub struct Generator { |
4 | | - pub minify: bool, |
5 | | - code: String, |
6 | | - dent: u16, |
7 | | - spaces_per_indent: u16, |
8 | | -} |
| 3 | +pub trait Generator { |
| 4 | + fn new_line(&mut self) {} |
9 | 5 |
|
10 | | -impl Generator { |
11 | | - pub fn new(minify: bool, spaces: u16) -> Self { |
12 | | - Generator { |
13 | | - minify: minify, |
14 | | - code: String::with_capacity(1024), |
15 | | - dent: 0, |
16 | | - spaces_per_indent: spaces |
17 | | - } |
18 | | - } |
| 6 | + fn write(&mut self, slice: &str); |
19 | 7 |
|
20 | | - pub fn new_line(&mut self) { |
21 | | - if !self.minify { |
22 | | - self.code.push('\n'); |
23 | | - for _ in 0..(self.dent * self.spaces_per_indent) { |
24 | | - self.code.push(' '); |
25 | | - } |
26 | | - } |
27 | | - } |
| 8 | + fn write_min(&mut self, slice: &str, minslice: &str); |
| 9 | + |
| 10 | + fn write_char(&mut self, ch: char); |
28 | 11 |
|
29 | | - pub fn write_json(&mut self, json: &JsonValue) { |
| 12 | + fn indent(&mut self) {} |
| 13 | + |
| 14 | + fn dedent(&mut self) {} |
| 15 | + |
| 16 | + fn write_json(&mut self, json: &JsonValue) { |
30 | 17 | match *json { |
31 | 18 | JsonValue::String(ref string) => { |
32 | 19 | self.write_char('"'); |
@@ -92,31 +79,84 @@ impl Generator { |
92 | 79 | } |
93 | 80 | } |
94 | 81 |
|
95 | | - pub fn write(&mut self, slice: &str) { |
| 82 | + fn consume(self) -> String; |
| 83 | +} |
| 84 | + |
| 85 | +pub struct DumpGenerator { |
| 86 | + code: String, |
| 87 | +} |
| 88 | + |
| 89 | +impl DumpGenerator { |
| 90 | + pub fn new() -> Self { |
| 91 | + DumpGenerator { |
| 92 | + code: String::with_capacity(1024), |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +impl Generator for DumpGenerator { |
| 98 | + fn write(&mut self, slice: &str) { |
96 | 99 | self.code.push_str(slice); |
97 | 100 | } |
98 | 101 |
|
99 | | - pub fn write_min(&mut self, slice: &str, minslice: &str) { |
100 | | - if self.minify { |
101 | | - self.write(minslice); |
102 | | - } else { |
103 | | - self.write(slice); |
| 102 | + fn write_min(&mut self, _: &str, minslice: &str) { |
| 103 | + self.write(minslice); |
| 104 | + } |
| 105 | + |
| 106 | + fn write_char(&mut self, ch: char) { |
| 107 | + self.code.push(ch); |
| 108 | + } |
| 109 | + |
| 110 | + fn consume(self) -> String { |
| 111 | + self.code |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +pub struct PrettyGenerator { |
| 116 | + code: String, |
| 117 | + dent: u16, |
| 118 | + spaces_per_indent: u16, |
| 119 | +} |
| 120 | + |
| 121 | +impl PrettyGenerator { |
| 122 | + pub fn new(spaces: u16) -> Self { |
| 123 | + PrettyGenerator { |
| 124 | + code: String::with_capacity(1024), |
| 125 | + dent: 0, |
| 126 | + spaces_per_indent: spaces |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +impl Generator for PrettyGenerator { |
| 132 | + fn new_line(&mut self) { |
| 133 | + self.code.push('\n'); |
| 134 | + for _ in 0..(self.dent * self.spaces_per_indent) { |
| 135 | + self.code.push(' '); |
104 | 136 | } |
105 | 137 | } |
106 | 138 |
|
107 | | - pub fn write_char(&mut self, ch: char) { |
| 139 | + fn write(&mut self, slice: &str) { |
| 140 | + self.code.push_str(slice); |
| 141 | + } |
| 142 | + |
| 143 | + fn write_min(&mut self, slice: &str, _: &str) { |
| 144 | + self.write(slice); |
| 145 | + } |
| 146 | + |
| 147 | + fn write_char(&mut self, ch: char) { |
108 | 148 | self.code.push(ch); |
109 | 149 | } |
110 | 150 |
|
111 | | - pub fn indent(&mut self) { |
| 151 | + fn indent(&mut self) { |
112 | 152 | self.dent += 1; |
113 | 153 | } |
114 | 154 |
|
115 | | - pub fn dedent(&mut self) { |
| 155 | + fn dedent(&mut self) { |
116 | 156 | self.dent -= 1; |
117 | 157 | } |
118 | 158 |
|
119 | | - pub fn consume(self) -> String { |
| 159 | + fn consume(self) -> String { |
120 | 160 | self.code |
121 | 161 | } |
122 | 162 | } |
0 commit comments