diff options
Diffstat (limited to 'src/state.rs')
| -rw-r--r-- | src/state.rs | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/src/state.rs b/src/state.rs index a04412f..425e5d7 100644 --- a/src/state.rs +++ b/src/state.rs @@ -3,11 +3,12 @@ use directories::ProjectDirs; use expanduser::expanduser; use serde::{Deserialize, Serialize}; use walkdir::WalkDir; +use zstd; use crate::{config::Config, learner::Learner, trajectory::Trajectory}; fn default_state_path() -> String { - let name = "mood.bin"; + let name = "mood.bin.zstd"; ProjectDirs::from("qualifier", "organisation", "mood") .and_then(|pd| pd.config_dir().join(name).to_str().map(|x| x.to_string())) .unwrap_or(format!("~/.config/mood/{name}")) @@ -57,7 +58,8 @@ impl State { expanduser(default_state_path()) .map_err(|e| e.to_string()) .and_then(|p| fs::read(p).map_err(|e| e.to_string())) - .and_then(|contents| bincode::deserialize(&contents).map_err(|e| e.to_string())) + .and_then(|contents| zstd::decode_all(&contents[..]).map_err(|e| e.to_string())) + .and_then(|decompressed| bincode::deserialize(&decompressed).map_err(|e| e.to_string())) } pub fn try_save(&self) -> Result<(), String> { @@ -66,7 +68,9 @@ impl State { _ = path.parent().map(std::fs::create_dir_all); let serialised = bincode::serialize(self).map_err(|e| format!("Failed to serialize state: {}", e))?; - fs::write(&path, serialised) + let compressed = zstd::encode_all(serialised.as_slice(), 3) + .map_err(|e| format!("Failed to compress state: {}", e))?; + fs::write(&path, compressed) .map_err(|e| format!("Failed to write state file at {}: {}", str_path, e))?; Ok(()) } |
