Complete add functionality.

This commit is contained in:
Nicolás A. Ortega Froysa 2022-07-13 10:29:54 +02:00
parent 5a7bdf0c57
commit 6961e4e3f3
2 changed files with 8 additions and 3 deletions

View File

@ -39,7 +39,7 @@ impl HabitMgr
File::create(&habits_path) File::create(&habits_path)
.unwrap_or_else(|e| { .unwrap_or_else(|e| {
panic!("Error creating file {}:\n{}", panic!("Error creating file {}:\n{}",
habits_path.display(), e) habits_path.display(), e);
}); });
} }
Self Self
@ -78,6 +78,11 @@ impl HabitMgr
}); });
let habit = Habit::new(name, bad, weight); 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) pub fn list(&mut self, all:bool, verbose:bool)

View File

@ -75,7 +75,7 @@ fn main()
let data_dir = let data_dir =
PathBuf::from( PathBuf::from(
env::var("XDG_DATA_HOME") env::var("XDG_DATA_HOME")
.unwrap_or_else(|e| { panic!("Error: {}", e) })) .unwrap_or_else(|e| { panic!("Error: {}", e); }))
.join("htracker"); .join("htracker");
if data_dir.exists() && data_dir.is_file() 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()); println!("First run: files will be stored in {}", data_dir.display());
fs::create_dir_all(&data_dir) 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); let mut hmgr = HabitMgr::new(&data_dir);