From ac3b111053c4028474496f8fb0e9ce0265b7b449 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Tue, 19 Jul 2022 14:45:44 +0200 Subject: [PATCH] Add modify to habitmgr. --- src/habitmgr.rs | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/habitmgr.rs b/src/habitmgr.rs index 989ea34..54bae8d 100644 --- a/src/habitmgr.rs +++ b/src/habitmgr.rs @@ -74,7 +74,7 @@ impl HabitMgr self.habits = serde_json::from_reader(habits_reader).unwrap(); } - fn write_habits(&self) + fn export_habits(&self) { let habits_file = OpenOptions::new() .truncate(true) @@ -95,7 +95,7 @@ impl HabitMgr { self.import_habits(); self.habits.push(Habit::new(name.clone(), bad, weight)); - self.write_habits(); + self.export_habits(); println!("New habit {} added.", &name); } @@ -104,10 +104,32 @@ impl HabitMgr { self.import_habits(); let old_habit = self.habits.remove(id); - self.write_habits(); + self.export_habits(); println!("Removed habit {}", old_habit.get_name()); } + pub fn modify(&mut self, id:usize, + name:Option, + bad:Option, + weight:Option, + _days:Option) + { + self.import_habits(); + if name.is_some() + { + self.habits[id].set_name(name.unwrap()); + } + if bad.is_some() + { + self.habits[id].set_bad(bad.unwrap()); + } + if weight.is_some() + { + self.habits[id].set_weight(weight.unwrap()); + } + self.export_habits(); + } + pub fn list(&mut self, _all:bool, _verbose:bool) { self.import_habits();