Import habits from JSON file.
This commit is contained in:
parent
7c6cf077fe
commit
e65563c824
@ -16,6 +16,10 @@
|
|||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::{BufReader, BufRead};
|
||||||
|
|
||||||
use crate::habit::Habit;
|
use crate::habit::Habit;
|
||||||
|
|
||||||
pub struct HabitMgr
|
pub struct HabitMgr
|
||||||
@ -25,11 +29,25 @@ pub struct HabitMgr
|
|||||||
|
|
||||||
impl 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<Habit> = serde_json::from_reader(reader).unwrap();
|
||||||
|
let mut imports:Vec<Habit> = 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
|
Self
|
||||||
{
|
{
|
||||||
habits: Vec::new(),
|
habits: imports,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
19
src/main.rs
19
src/main.rs
@ -21,6 +21,7 @@ use structopt::clap::AppSettings;
|
|||||||
use std::env;
|
use std::env;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
use std::fs::File;
|
||||||
|
|
||||||
mod habit;
|
mod habit;
|
||||||
mod habitmgr;
|
mod habitmgr;
|
||||||
@ -75,7 +76,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(|err| { panic!("Error: {}", err) }))
|
.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()
|
||||||
@ -86,11 +87,21 @@ fn main()
|
|||||||
else if !data_dir.exists()
|
else if !data_dir.exists()
|
||||||
{
|
{
|
||||||
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(|err| { panic!("Filesystem error: {}", err) });
|
.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
|
match opts.cmd
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user