diff --git a/src/habitmgr.rs b/src/habitmgr.rs index da5fda2..2581b37 100644 --- a/src/habitmgr.rs +++ b/src/habitmgr.rs @@ -16,6 +16,10 @@ * along with this program. If not, see . */ +use std::path::PathBuf; +use std::fs::File; +use std::io::{BufReader, BufRead}; + use crate::habit::Habit; pub struct HabitMgr @@ -25,11 +29,25 @@ pub struct HabitMgr impl HabitMgr { - pub fn new() -> Self + pub fn new(actives_p:&PathBuf) -> Self { + let actives_f = File::open(&actives_p) + .unwrap_or_else(|e| { + panic!("Error opening file {}:\n{}", actives_p.display(), e); + }); + let reader = BufReader::new(&actives_f); + //let imports:Vec = serde_json::from_reader(reader).unwrap(); + let mut imports:Vec = Vec::new(); + + for (_i, line) in reader.lines().enumerate() + { + let habit:Habit = serde_json::from_str(line.unwrap().as_str()).unwrap(); + imports.push(habit); + } + Self { - habits: Vec::new(), + habits: imports, } } diff --git a/src/main.rs b/src/main.rs index a121ee9..31cdb74 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,6 +21,7 @@ use structopt::clap::AppSettings; use std::env; use std::path::PathBuf; use std::fs; +use std::fs::File; mod habit; mod habitmgr; @@ -75,7 +76,7 @@ fn main() let data_dir = PathBuf::from( env::var("XDG_DATA_HOME") - .unwrap_or_else(|err| { panic!("Error: {}", err) })) + .unwrap_or_else(|e| { panic!("Error: {}", e) })) .join("htracker"); if data_dir.exists() && data_dir.is_file() @@ -86,11 +87,21 @@ fn main() else if !data_dir.exists() { println!("First run: files will be stored in {}", data_dir.display()); - fs::create_dir_all(data_dir) - .unwrap_or_else(|err| { panic!("Filesystem error: {}", err) }); + fs::create_dir_all(&data_dir) + .unwrap_or_else(|e| { panic!("Filesystem error: {}", e) }); } - let mut hmgr = HabitMgr::new(); + let actives_p:PathBuf = data_dir.clone().join("active_habits.json"); + if !actives_p.is_file() + { + File::create(&actives_p) + .unwrap_or_else(|e| { + panic!("Error creating file {}:\n{}", + actives_p.display(), e) + }); + } + + let mut hmgr = HabitMgr::new(&actives_p); match opts.cmd {