diff options
-rw-r--r-- | xtask/src/metrics.rs | 30 |
1 files changed, 20 insertions, 10 deletions
diff --git a/xtask/src/metrics.rs b/xtask/src/metrics.rs index bb3044f41..4c8249b8e 100644 --- a/xtask/src/metrics.rs +++ b/xtask/src/metrics.rs | |||
@@ -29,8 +29,7 @@ pub fn run_metrics() -> Result<()> { | |||
29 | run!("git -c user.name=Bot -c [email protected] commit --message 📈")?; | 29 | run!("git -c user.name=Bot -c [email protected] commit --message 📈")?; |
30 | run!("git push origin master")?; | 30 | run!("git push origin master")?; |
31 | } | 31 | } |
32 | eprintln!("{:#?}\n", metrics); | 32 | eprintln!("{:#?}", metrics); |
33 | eprintln!("{}", metrics.json()); | ||
34 | Ok(()) | 33 | Ok(()) |
35 | } | 34 | } |
36 | 35 | ||
@@ -152,26 +151,32 @@ impl Host { | |||
152 | } | 151 | } |
153 | } | 152 | } |
154 | 153 | ||
154 | struct State { | ||
155 | obj: bool, | ||
156 | first: bool, | ||
157 | } | ||
158 | |||
155 | #[derive(Default)] | 159 | #[derive(Default)] |
156 | struct Json { | 160 | struct Json { |
157 | object_comma: bool, | 161 | stack: Vec<State>, |
158 | array_comma: bool, | ||
159 | buf: String, | 162 | buf: String, |
160 | } | 163 | } |
161 | 164 | ||
162 | impl Json { | 165 | impl Json { |
163 | fn begin_object(&mut self) { | 166 | fn begin_object(&mut self) { |
164 | self.object_comma = false; | 167 | self.stack.push(State { obj: true, first: true }); |
165 | self.buf.push('{'); | 168 | self.buf.push('{'); |
166 | } | 169 | } |
167 | fn end_object(&mut self) { | 170 | fn end_object(&mut self) { |
171 | self.stack.pop(); | ||
168 | self.buf.push('}') | 172 | self.buf.push('}') |
169 | } | 173 | } |
170 | fn begin_array(&mut self) { | 174 | fn begin_array(&mut self) { |
171 | self.array_comma = false; | 175 | self.stack.push(State { obj: false, first: true }); |
172 | self.buf.push('['); | 176 | self.buf.push('['); |
173 | } | 177 | } |
174 | fn end_array(&mut self) { | 178 | fn end_array(&mut self) { |
179 | self.stack.pop(); | ||
175 | self.buf.push(']') | 180 | self.buf.push(']') |
176 | } | 181 | } |
177 | fn field(&mut self, name: &str) { | 182 | fn field(&mut self, name: &str) { |
@@ -194,17 +199,22 @@ impl Json { | |||
194 | } | 199 | } |
195 | 200 | ||
196 | fn array_comma(&mut self) { | 201 | fn array_comma(&mut self) { |
197 | if self.array_comma { | 202 | let state = self.stack.last_mut().unwrap(); |
203 | if state.obj { | ||
204 | return; | ||
205 | } | ||
206 | if !state.first { | ||
198 | self.buf.push(','); | 207 | self.buf.push(','); |
199 | } | 208 | } |
200 | self.array_comma = true; | 209 | state.first = false; |
201 | } | 210 | } |
202 | 211 | ||
203 | fn object_comma(&mut self) { | 212 | fn object_comma(&mut self) { |
204 | if self.object_comma { | 213 | let state = self.stack.last_mut().unwrap(); |
214 | if !state.first { | ||
205 | self.buf.push(','); | 215 | self.buf.push(','); |
206 | } | 216 | } |
207 | self.object_comma = true; | 217 | state.first = false; |
208 | } | 218 | } |
209 | } | 219 | } |
210 | 220 | ||