diff --git a/src/habitmgr.rs b/src/habitmgr.rs index 5dc3e90..cf628ae 100644 --- a/src/habitmgr.rs +++ b/src/habitmgr.rs @@ -55,23 +55,27 @@ impl HabitMgr habits_path.display(), e); }); } - Self - { - habits: Vec::new(), - habits_path, - } - } - fn import_habits(&mut self) - { + /* + * The list of active habits is necessary for all functions, thus they're + * imported here, allowing member functions which only wish to access + * information to be constant. + */ let habits_file = OpenOptions::new() .read(true) - .open(&self.habits_path) + .open(&habits_path) .unwrap_or_else(|e| { - panic!("Error opening file {}:\n{}", self.habits_path.display(), e); + panic!("Error opening file {}:\n{}", habits_path.display(), e); }); let habits_reader = BufReader::new(&habits_file); - self.habits = serde_json::from_reader(habits_reader).unwrap(); + + let habits:Vec = serde_json::from_reader(habits_reader).unwrap(); + + Self + { + habits, + habits_path, + } } fn export_habits(&self) @@ -93,7 +97,6 @@ impl HabitMgr pub fn add(&mut self, name:String, bad:bool, weight:u8, _days:String) { - self.import_habits(); self.habits.push(Habit::new(name.clone(), bad, weight)); self.export_habits(); @@ -102,15 +105,13 @@ impl HabitMgr pub fn delete(&mut self, id:usize) { - self.import_habits(); let old_habit = self.habits.remove(id); self.export_habits(); println!("Removed habit {}", old_habit.get_name()); } - pub fn habit_info(&mut self, id:usize) + pub fn habit_info(&self, id:usize) { - self.import_habits(); let habit = &self.habits[id]; println!("Name: {}", habit.get_name()); println!("ID: {}", id); @@ -125,7 +126,6 @@ impl HabitMgr weight:Option, _days:Option) { - self.import_habits(); if name.is_some() { self.habits[id].set_name(name.unwrap()); @@ -142,9 +142,8 @@ impl HabitMgr self.export_habits(); } - pub fn list(&mut self, _all:bool) + pub fn list(&self, _all:bool) { - self.import_habits(); if self.habits.is_empty() { println!("There are no habits. Add one!");