From 1004b78ece1cd5311b63fcea194d93ea17ed8b29 Mon Sep 17 00:00:00 2001 From: parazyd Date: Mon, 31 Jul 2023 19:00:24 +0200 Subject: [PATCH] util: Support conditional pretty-writing of JSON files. --- bin/darkirc/src/main.rs | 2 +- bin/tau/taud/src/month_tasks.rs | 2 +- bin/tau/taud/src/task_info.rs | 2 +- src/event_graph/model.rs | 2 +- src/util/file.rs | 10 ++++++++-- 5 files changed, 12 insertions(+), 6 deletions(-) diff --git a/bin/darkirc/src/main.rs b/bin/darkirc/src/main.rs index e47d4c8cc4b8..56f8c6730b3c 100644 --- a/bin/darkirc/src/main.rs +++ b/bin/darkirc/src/main.rs @@ -112,7 +112,7 @@ async fn realmain(settings: Args, executor: Arc>) -> Result<( if settings.output.is_some() { let datastore = expand_path(&settings.output.unwrap())?; - save_json_file(&datastore, &kp)?; + save_json_file(&datastore, &kp, false)?; } else { println!("Generated keypair:\n{}", kp); } diff --git a/bin/tau/taud/src/month_tasks.rs b/bin/tau/taud/src/month_tasks.rs index 30c22355b7f7..5e743581ee80 100644 --- a/bin/tau/taud/src/month_tasks.rs +++ b/bin/tau/taud/src/month_tasks.rs @@ -98,7 +98,7 @@ impl MonthTasks { pub fn save(&self, dataset_path: &Path) -> TaudResult<()> { debug!(target: "tau", "MonthTasks::save()"); - save_json_file::(&Self::get_path(&self.created_at, dataset_path), self) + save_json_file::(&Self::get_path(&self.created_at, dataset_path), self, true) .map_err(TaudError::Darkfi) } diff --git a/bin/tau/taud/src/task_info.rs b/bin/tau/taud/src/task_info.rs index 9a4306f67325..ba3a3c3b7353 100644 --- a/bin/tau/taud/src/task_info.rs +++ b/bin/tau/taud/src/task_info.rs @@ -153,7 +153,7 @@ impl TaskInfo { pub fn save(&self, dataset_path: &Path) -> TaudResult<()> { debug!(target: "tau", "TaskInfo::save()"); - save_json_file::(&Self::get_path(&self.ref_id, dataset_path), self) + save_json_file::(&Self::get_path(&self.ref_id, dataset_path), self, true) .map_err(TaudError::Darkfi)?; if self.get_state() == "stop" { diff --git a/src/event_graph/model.rs b/src/event_graph/model.rs index b67927f8e014..8cda88859c8f 100644 --- a/src/event_graph/model.rs +++ b/src/event_graph/model.rs @@ -102,7 +102,7 @@ where let tree = self.event_map.clone(); let ser_tree = serialize(&tree); - save_json_file(&path, &ser_tree)?; + save_json_file(&path, &ser_tree, false)?; info!("Tree is saved to disk"); diff --git a/src/util/file.rs b/src/util/file.rs index 0597b920a698..33c7d3540c72 100644 --- a/src/util/file.rs +++ b/src/util/file.rs @@ -48,8 +48,14 @@ pub fn load_json_file(path: &Path) -> Result { Ok(value) } -pub fn save_json_file(path: &Path, value: &T) -> Result<()> { +pub fn save_json_file(path: &Path, value: &T, pretty: bool) -> Result<()> { let file = File::create(path)?; - serde_json::to_writer_pretty(file, value)?; + + if pretty { + serde_json::to_writer_pretty(file, value)?; + } else { + serde_json::to_writer(file, value)?; + } + Ok(()) }