Skip to content

Commit

Permalink
util: Support conditional pretty-writing of JSON files.
Browse files Browse the repository at this point in the history
  • Loading branch information
parazyd committed Jul 31, 2023
1 parent c1a36c1 commit 1004b78
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion bin/darkirc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ async fn realmain(settings: Args, executor: Arc<smol::Executor<'_>>) -> 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);
}
Expand Down
2 changes: 1 addition & 1 deletion bin/tau/taud/src/month_tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl MonthTasks {

pub fn save(&self, dataset_path: &Path) -> TaudResult<()> {
debug!(target: "tau", "MonthTasks::save()");
save_json_file::<Self>(&Self::get_path(&self.created_at, dataset_path), self)
save_json_file::<Self>(&Self::get_path(&self.created_at, dataset_path), self, true)
.map_err(TaudError::Darkfi)
}

Expand Down
2 changes: 1 addition & 1 deletion bin/tau/taud/src/task_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl TaskInfo {

pub fn save(&self, dataset_path: &Path) -> TaudResult<()> {
debug!(target: "tau", "TaskInfo::save()");
save_json_file::<Self>(&Self::get_path(&self.ref_id, dataset_path), self)
save_json_file::<Self>(&Self::get_path(&self.ref_id, dataset_path), self, true)
.map_err(TaudError::Darkfi)?;

if self.get_state() == "stop" {
Expand Down
2 changes: 1 addition & 1 deletion src/event_graph/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
10 changes: 8 additions & 2 deletions src/util/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,14 @@ pub fn load_json_file<T: DeserializeOwned>(path: &Path) -> Result<T> {
Ok(value)
}

pub fn save_json_file<T: Serialize>(path: &Path, value: &T) -> Result<()> {
pub fn save_json_file<T: Serialize>(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(())
}

0 comments on commit 1004b78

Please sign in to comment.