From 47d59e4fa58866df495cd28561a5f074d5de110d Mon Sep 17 00:00:00 2001 From: Luni-4 Date: Fri, 3 Apr 2020 01:05:04 +0200 Subject: [PATCH] Add an option to dump pretty json files --- src/main.rs | 9 +++++++++ src/metrics.rs | 16 +++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index ccc96ef8a..f794f7cc4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,6 +28,7 @@ struct Config { function: bool, metrics: bool, output: String, + pretty: bool, line_start: Option, line_end: Option, preproc_lock: Option>>, @@ -80,6 +81,7 @@ fn act_on_file(language: Option, path: PathBuf, cfg: Config) -> std::io::R } else if cfg.metrics { let cfg = MetricsCfg { path, + pretty: cfg.pretty, output_path: if cfg.output.is_empty() { None } else { @@ -309,6 +311,11 @@ fn main() { .default_value("") .takes_value(true), ) + .arg( + Arg::with_name("pretty") + .help("Dump a pretty json file") + .long("pr"), + ) .arg( Arg::with_name("output") .help("Output file/directory") @@ -430,6 +437,7 @@ fn main() { (None, None) }; + let pretty = matches.is_present("pretty"); let output = matches.value_of("output").unwrap().to_string(); let output_is_dir = PathBuf::from(output.clone()).is_dir(); if metrics && !output.is_empty() && !output_is_dir { @@ -468,6 +476,7 @@ fn main() { count_filter, function, metrics, + pretty, output: output.clone(), line_start, line_end, diff --git a/src/metrics.rs b/src/metrics.rs index b4c67942c..acff96a54 100644 --- a/src/metrics.rs +++ b/src/metrics.rs @@ -351,8 +351,17 @@ impl<'a> FuncSpace<'a> { writeln!(stdout, "{}", val) } - fn dump_json(&self, path: &PathBuf, output_path: &PathBuf) -> std::io::Result<()> { - let json_data = serde_json::to_string_pretty(&self).unwrap(); + fn dump_json( + &self, + path: &PathBuf, + output_path: &PathBuf, + pretty: bool, + ) -> std::io::Result<()> { + let json_data = if pretty { + serde_json::to_string_pretty(&self).unwrap() + } else { + serde_json::to_string(&self).unwrap() + }; let mut file = path.as_path().file_name().unwrap().to_os_string(); file.push(".json"); @@ -453,6 +462,7 @@ pub fn metrics<'a, T: TSParserTrait>(parser: &'a T, path: &'a PathBuf) -> Option pub struct MetricsCfg { pub path: PathBuf, + pub pretty: bool, pub output_path: Option, } @@ -465,7 +475,7 @@ impl Callback for Metrics { fn call(cfg: Self::Cfg, parser: &T) -> Self::Res { if let Some(space) = metrics(parser, &cfg.path) { if let Some(output_path) = cfg.output_path { - space.dump_json(&cfg.path, &output_path) + space.dump_json(&cfg.path, &output_path, cfg.pretty) } else { space.dump_root() }