diff --git a/pttodoer/src/main.rs b/pttodoer/src/main.rs index 3d599c0..5fa68d2 100644 --- a/pttodoer/src/main.rs +++ b/pttodoer/src/main.rs @@ -1,4 +1,5 @@ use serde_yaml; +use serde::ser::{Serialize}; use serde; use chrono::{DateTime, Local}; use chrono::naive::NaiveDateTime; @@ -8,13 +9,14 @@ use clap::Parser; fn main() { let flags = Flags::new(); - println!("{:?}", flags); + let db = DB::new(flags.path).unwrap(); + println!("{}", db.due().to_string()); } #[derive(Debug, Parser)] struct Flags { - #[arg(short = 'f', long = "file", default_value="$PTTODO_FILE")] - file: String, + #[arg(short = 'f', long = "path", default_value="$PTTODO_FILE")] + path: String, #[arg(short = 'a', long = "add")] add: Option, @@ -30,10 +32,10 @@ impl Flags { fn new() -> Flags { let mut result = Flags::parse(); - if result.file.get(..1) == Some("$") { - result.file = std::env::var( - result.file.get(1..).unwrap() - ).expect(format!("'{}' unset", result.file).as_str()); + if result.path.get(..1) == Some("$") { + result.path = std::env::var( + result.path.get(1..).unwrap() + ).expect(format!("'{}' unset", result.path).as_str()); } result @@ -222,6 +224,32 @@ impl Tasks { } } + pub fn to_string(&self) -> String { + let mut buffer = Vec::new(); + let mut serializer = serde_yaml::Serializer::new(&mut buffer); + _ = serde_yaml::Value::Sequence( + self.0.iter() + .map(|x| { + let mut x = x.clone(); + if x.is_due() { + x.unset("ts".to_string()); + } + x + }) + .map(|x| { + let is = x.get_value("is".to_string()); + if x.0.len() == 1 && is.is_some() { + return is.unwrap(); + } + serde_yaml::Value::from(x.0.clone()) + }) + .map(|x| serde_yaml::Value::from(x.clone())) + .collect() + ).serialize(&mut serializer) + .expect("failed to serialize"); + String::from_utf8(buffer).expect("illegal utf8 characters found") + } + pub fn len(&self) -> usize { self.0.len() } @@ -379,6 +407,10 @@ impl Task { } } + fn unset(&mut self, k: String) { + self.0.remove(serde_yaml::Value::String(k)); + } + #[allow(dead_code)] // used in test fn set(&mut self, k: String, v: String) { self.0.insert(