Sfoglia il codice sorgente

create `file` module in src/util contain helper functions for dealing with files

ghassmo 4 anni fa
parent
commit
f73217d966

+ 7 - 5
bin/tau/taud/src/month_tasks.rs

@@ -7,12 +7,14 @@ use chrono::{TimeZone, Utc};
 use log::debug;
 use serde::{Deserialize, Serialize};
 
-use darkfi::util::Timestamp;
+use darkfi::util::{
+    file::{load_json_file, save_json_file},
+    Timestamp,
+};
 
 use crate::{
     error::{TaudError, TaudResult},
     task_info::TaskInfo,
-    util::{load, save},
 };
 
 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
@@ -63,7 +65,7 @@ impl MonthTasks {
 
     pub fn save(&self, dataset_path: &Path) -> TaudResult<()> {
         debug!(target: "tau", "MonthTasks::save()");
-        save::<Self>(&Self::get_path(&self.created_at, dataset_path), self)
+        save_json_file::<Self>(&Self::get_path(&self.created_at, dataset_path), self)
             .map_err(TaudError::Darkfi)
     }
 
@@ -94,7 +96,7 @@ impl MonthTasks {
         // if a date is given we load that date's month tasks
         // if not, we load tasks from all months
         match date {
-            Some(date) => match load::<Self>(&Self::get_path(date, dataset_path)) {
+            Some(date) => match load_json_file::<Self>(&Self::get_path(date, dataset_path)) {
                 Ok(mt) => Ok(mt),
                 Err(_) => Self::create(date, dataset_path),
             },
@@ -107,7 +109,7 @@ impl MonthTasks {
                 let mut loaded_mt = Self::new(&[]);
 
                 for path in path_all {
-                    let mt = load::<Self>(&path)?;
+                    let mt = load_json_file::<Self>(&path)?;
                     loaded_mt.created_at = mt.created_at;
                     for tks in mt.task_tks {
                         if !loaded_mt.task_tks.contains(&tks) {

+ 4 - 3
bin/tau/taud/src/task_info.rs

@@ -7,6 +7,7 @@ use log::debug;
 use serde::{Deserialize, Serialize};
 
 use darkfi::util::{
+    file::{load_json_file, save_json_file},
     serial::{Decodable, Encodable, SerialDecodable, SerialEncodable, VarInt},
     Timestamp,
 };
@@ -14,7 +15,7 @@ use darkfi::util::{
 use crate::{
     error::{TaudError, TaudResult},
     month_tasks::MonthTasks,
-    util::{find_free_id, load, random_ref_id, save},
+    util::{find_free_id, random_ref_id},
 };
 
 #[derive(Clone, Debug, Serialize, Deserialize, SerialEncodable, SerialDecodable, PartialEq, Eq)]
@@ -120,13 +121,13 @@ impl TaskInfo {
 
     pub fn load(ref_id: &str, dataset_path: &Path) -> TaudResult<Self> {
         debug!(target: "tau", "TaskInfo::load()");
-        let task = load::<Self>(&Self::get_path(ref_id, dataset_path))?;
+        let task = load_json_file::<Self>(&Self::get_path(ref_id, dataset_path))?;
         Ok(task)
     }
 
     pub fn save(&self, dataset_path: &Path) -> TaudResult<()> {
         debug!(target: "tau", "TaskInfo::save()");
-        save::<Self>(&Self::get_path(&self.ref_id, dataset_path), self)
+        save_json_file::<Self>(&Self::get_path(&self.ref_id, dataset_path), self)
             .map_err(TaudError::Darkfi)?;
 
         if self.get_state() == "stop" {

+ 1 - 20
bin/tau/taud/src/util.rs

@@ -1,13 +1,8 @@
-use std::{
-    fs::File,
-    io::BufReader,
-    path::{Path, PathBuf},
-};
+use std::path::PathBuf;
 
 use fxhash::FxHashMap;
 use log::info;
 use rand::{distributions::Alphanumeric, thread_rng, Rng};
-use serde::{de::DeserializeOwned, Serialize};
 
 use darkfi::Result;
 
@@ -66,20 +61,6 @@ pub fn find_free_id(task_ids: &[u32]) -> u32 {
     1
 }
 
-pub fn load<T: DeserializeOwned>(path: &Path) -> Result<T> {
-    let file = File::open(path)?;
-    let reader = BufReader::new(file);
-
-    let value: T = serde_json::from_reader(reader)?;
-    Ok(value)
-}
-
-pub fn save<T: Serialize>(path: &Path, value: &T) -> Result<()> {
-    let file = File::create(path)?;
-    serde_json::to_writer_pretty(file, value)?;
-    Ok(())
-}
-
 #[cfg(test)]
 mod tests {
     use super::*;

+ 19 - 0
src/util/file.rs

@@ -0,0 +1,19 @@
+use std::{fs::File, io::BufReader, path::Path};
+
+use serde::{de::DeserializeOwned, Serialize};
+
+use crate::Result;
+
+pub fn load_json_file<T: DeserializeOwned>(path: &Path) -> Result<T> {
+    let file = File::open(path)?;
+    let reader = BufReader::new(file);
+
+    let value: T = serde_json::from_reader(reader)?;
+    Ok(value)
+}
+
+pub fn save_json_file<T: Serialize>(path: &Path, value: &T) -> Result<()> {
+    let file = File::create(path)?;
+    serde_json::to_writer_pretty(file, value)?;
+    Ok(())
+}

+ 1 - 0
src/util/mod.rs

@@ -5,6 +5,7 @@ pub mod async_util;
 
 pub mod cli;
 pub mod endian;
+pub mod file;
 pub mod net_name;
 pub mod parse;
 pub mod path;