From cdddd205f600c844ef1bae3038bb1da48c382b11 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 24 Jul 2020 21:03:49 +0200 Subject: Fix metrics --- xtask/src/metrics.rs | 30 ++++++++++++++++++++---------- 1 file 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<()> { run!("git -c user.name=Bot -c user.email=dummy@example.com commit --message 📈")?; run!("git push origin master")?; } - eprintln!("{:#?}\n", metrics); - eprintln!("{}", metrics.json()); + eprintln!("{:#?}", metrics); Ok(()) } @@ -152,26 +151,32 @@ impl Host { } } +struct State { + obj: bool, + first: bool, +} + #[derive(Default)] struct Json { - object_comma: bool, - array_comma: bool, + stack: Vec, buf: String, } impl Json { fn begin_object(&mut self) { - self.object_comma = false; + self.stack.push(State { obj: true, first: true }); self.buf.push('{'); } fn end_object(&mut self) { + self.stack.pop(); self.buf.push('}') } fn begin_array(&mut self) { - self.array_comma = false; + self.stack.push(State { obj: false, first: true }); self.buf.push('['); } fn end_array(&mut self) { + self.stack.pop(); self.buf.push(']') } fn field(&mut self, name: &str) { @@ -194,17 +199,22 @@ impl Json { } fn array_comma(&mut self) { - if self.array_comma { + let state = self.stack.last_mut().unwrap(); + if state.obj { + return; + } + if !state.first { self.buf.push(','); } - self.array_comma = true; + state.first = false; } fn object_comma(&mut self) { - if self.object_comma { + let state = self.stack.last_mut().unwrap(); + if !state.first { self.buf.push(','); } - self.object_comma = true; + state.first = false; } } -- cgit v1.2.3