From 6961e4e3f3690175fe28a5f0d3a135afcd1dd164 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Wed, 13 Jul 2022 10:29:54 +0200 Subject: [PATCH] Complete add functionality. --- src/habitmgr.rs | 7 ++++++- src/main.rs | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/habitmgr.rs b/src/habitmgr.rs index 5489f50..391bacf 100644 --- a/src/habitmgr.rs +++ b/src/habitmgr.rs @@ -39,7 +39,7 @@ impl HabitMgr File::create(&habits_path) .unwrap_or_else(|e| { panic!("Error creating file {}:\n{}", - habits_path.display(), e) + habits_path.display(), e); }); } Self @@ -78,6 +78,11 @@ impl HabitMgr }); let habit = Habit::new(name, bad, weight); + serde_json::to_writer(habits_file, &habit) + .unwrap_or_else(|e| { + panic!("Error writing to file {}:\n{}", + self.habits_path.display(), e); + }) } pub fn list(&mut self, all:bool, verbose:bool) diff --git a/src/main.rs b/src/main.rs index 7b98e7d..6c702ac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -75,7 +75,7 @@ fn main() let data_dir = PathBuf::from( env::var("XDG_DATA_HOME") - .unwrap_or_else(|e| { panic!("Error: {}", e) })) + .unwrap_or_else(|e| { panic!("Error: {}", e); })) .join("htracker"); if data_dir.exists() && data_dir.is_file() @@ -87,7 +87,7 @@ fn main() { println!("First run: files will be stored in {}", data_dir.display()); fs::create_dir_all(&data_dir) - .unwrap_or_else(|e| { panic!("Filesystem error: {}", e) }); + .unwrap_or_else(|e| { panic!("Filesystem error: {}", e); }); } let mut hmgr = HabitMgr::new(&data_dir);