diff options
author | Vincent Breitmoser <look@my.amazin.horse> | 2019-01-12 15:17:23 +0100 |
---|---|---|
committer | Vincent Breitmoser <look@my.amazin.horse> | 2019-01-12 16:33:39 +0100 |
commit | 5216a30ac92716aa16b8760bd9048820a7466f33 (patch) | |
tree | 1411be204a546433d3273d5169c53dfcf071a6e1 | |
parent | e53d9eda3c19a3a209f9470a0307083f06e4e69c (diff) |
introduce KhLine struct for khaleesi lines
-rw-r--r-- | src/actions/copy.rs | 3 | ||||
-rw-r--r-- | src/actions/index/bucketable.rs | 7 | ||||
-rw-r--r-- | src/actions/list.rs | 9 | ||||
-rw-r--r-- | src/actions/new.rs | 8 | ||||
-rw-r--r-- | src/actions/select.rs | 4 | ||||
-rw-r--r-- | src/icalwrap/icalvcalendar.rs | 8 | ||||
-rw-r--r-- | src/icalwrap/icalvevent.rs | 7 | ||||
-rw-r--r-- | src/khline.rs | 47 | ||||
-rw-r--r-- | src/lib.rs | 1 |
9 files changed, 68 insertions, 26 deletions
diff --git a/src/actions/copy.rs b/src/actions/copy.rs index 5ed7aef..d982ef7 100644 --- a/src/actions/copy.rs +++ b/src/actions/copy.rs @@ -1,3 +1,4 @@ +use khline::KhLine; use utils::fileutil; use utils::misc; @@ -33,5 +34,5 @@ pub fn do_copy(lines: &mut Iterator<Item = String>, _args: &[String]) { }, } - println!("{}", new_cal.get_principal_event().get_khaleesi_line().unwrap()); + println!("{}", KhLine::from(&new_cal.get_principal_event()).unwrap()); } diff --git a/src/actions/index/bucketable.rs b/src/actions/index/bucketable.rs index e0f743a..1215f73 100644 --- a/src/actions/index/bucketable.rs +++ b/src/actions/index/bucketable.rs @@ -4,6 +4,7 @@ use std::{hash, cmp}; use icalwrap::{IcalVEvent, IcalVCalendar}; use utils::misc; +use khline::KhLine; pub trait Bucketable { fn get_buckets(&self) -> Result<HashMap<String, Vec<String>>, String>; @@ -34,11 +35,13 @@ impl Bucketable for IcalVEvent { } let buckets = Self::buckets_for_interval(start_date, end_date); + let khline = KhLine::from(self) + .ok_or_else(|| format!("Failed to build khline in {}", self.get_uid()))?; for bucketid in buckets { result .entry(bucketid) - .and_modify(|items| items.push(self.get_khaleesi_line().unwrap())) - .or_insert_with(|| vec!(self.get_khaleesi_line().unwrap())); + .and_modify(|items| items.push(khline.to_string())) + .or_insert_with(|| vec!(khline.to_string())); } if self.has_recur() { diff --git a/src/actions/list.rs b/src/actions/list.rs index 01289f6..7a1e741 100644 --- a/src/actions/list.rs +++ b/src/actions/list.rs @@ -1,5 +1,6 @@ use selectors::SelectFilters; -use utils::fileutil as utils; +use utils::fileutil; +use khline::KhLine; pub fn list_by_args(filenames: &mut Iterator<Item = String>, args: &[String]) { let filters = match SelectFilters::parse_from_args_with_range(args) { @@ -7,7 +8,7 @@ pub fn list_by_args(filenames: &mut Iterator<Item = String>, args: &[String]) { Ok(parsed_filters) => parsed_filters, }; - let cals = utils::read_calendars_from_files(filenames).unwrap(); + let cals = fileutil::read_calendars_from_files(filenames).unwrap(); let events = cals.into_iter() .map(|cal| cal.get_principal_event()) @@ -15,8 +16,8 @@ pub fn list_by_args(filenames: &mut Iterator<Item = String>, args: &[String]) { .filter(|(index, event)| filters.is_selected_index(*index, event)); for (_, event) in events { - if let Some(line) = event.get_khaleesi_line() { - println!("{}", line); + if let Some(khline) = KhLine::from(&event) { + println!("{}", khline); } } } diff --git a/src/actions/new.rs b/src/actions/new.rs index b834015..f3ca64a 100644 --- a/src/actions/new.rs +++ b/src/actions/new.rs @@ -1,7 +1,8 @@ +use defaults; +use icalwrap::IcalVCalendar; +use khline::KhLine; use utils::fileutil; use utils::misc; -use icalwrap::IcalVCalendar; -use defaults; pub fn do_new(_lines: &mut Iterator<Item = String>, _args: &[String]) { @@ -24,7 +25,8 @@ pub fn do_new(_lines: &mut Iterator<Item = String>, _args: &[String]) { }, } - println!("{}", new_cal.get_principal_event().get_khaleesi_line().unwrap()); + let khline = KhLine::from(&new_cal.get_principal_event()); + println!("{}", khline.unwrap()); } static TEMPLATE_EVENT: &str = indoc!(" diff --git a/src/actions/select.rs b/src/actions/select.rs index 87bca2b..4c37573 100644 --- a/src/actions/select.rs +++ b/src/actions/select.rs @@ -3,6 +3,7 @@ use std::path::PathBuf; use defaults; use selectors::{SelectFilters,daterange::SelectFilterFrom,daterange::SelectFilterTo}; use utils::fileutil as utils; +use khline::KhLine; impl SelectFilters { fn predicate_path_skip_while(&self) -> impl Fn(&PathBuf) -> bool + '_ { @@ -63,8 +64,9 @@ pub fn select_by_args(args: &[String]) { let mut lines: Vec<String> = cals .filter(|event| filters.is_selected(event)) - .map(|event| event.get_khaleesi_line()) + .map(|event| KhLine::from(&event)) .flatten() + .map(|khline| khline.to_string()) .collect(); lines.sort_unstable(); diff --git a/src/icalwrap/icalvcalendar.rs b/src/icalwrap/icalvcalendar.rs index 04c600d..19c182c 100644 --- a/src/icalwrap/icalvcalendar.rs +++ b/src/icalwrap/icalvcalendar.rs @@ -370,14 +370,6 @@ mod tests { } #[test] - fn get_khaleesi_line_test() { - let path = PathBuf::from("test/path"); - let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_MULTIDAY_ALLDAY, Some(&path)).unwrap(); - let event = cal.get_principal_event(); - assert_eq!(String::from("1182988800 test/path"), event.get_khaleesi_line().unwrap()) - } - - #[test] fn get_calendar_name_test() { let path = PathBuf::from("calname/event"); let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_MULTIDAY_ALLDAY, Some(&path)).unwrap(); diff --git a/src/icalwrap/icalvevent.rs b/src/icalwrap/icalvevent.rs index 658a757..d51003f 100644 --- a/src/icalwrap/icalvevent.rs +++ b/src/icalwrap/icalvevent.rs @@ -139,13 +139,6 @@ impl IcalVEvent { self.parent.as_ref() } - pub fn get_khaleesi_line(&self) -> Option<String> { - let dtstart = self.get_dtstart()?.timestamp(); - let dtstart_string = format!("{:010}", dtstart); - let path_string = self.parent.as_ref()?.get_path_as_string()?; - Some([dtstart_string, path_string].join(" ")) - } - pub fn get_summary(&self) -> Option<String> { unsafe { let ptr = ical::icalcomponent_get_summary(self.ptr); diff --git a/src/khline.rs b/src/khline.rs new file mode 100644 index 0000000..82fcf2d --- /dev/null +++ b/src/khline.rs @@ -0,0 +1,47 @@ +use std::fmt; +use std::path::PathBuf; +use chrono::prelude::*; + +use icalwrap::IcalVEvent; + +pub struct KhLine { + path: PathBuf, + time: DateTime<Local>, +} + +impl KhLine { + pub fn from(event: &IcalVEvent) -> Option<KhLine> { + let time = event.get_dtstart()?; + let path = event.get_parent()?.get_path()?.to_path_buf(); + + Some(KhLine{ path, time }) + } + + pub fn to_string(&self) -> String { + format!("{}", self) + } +} + +impl fmt::Display for KhLine { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let time_string = format!("{:010}", self.time.timestamp()); + let path_string = self.path.to_string_lossy(); + write!(f, "{} {}", time_string, path_string) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + use testdata; + use icalwrap::IcalVCalendar; + + #[test] + fn get_khaleesi_line_test() { + let path = PathBuf::from("test/path"); + let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_MULTIDAY_ALLDAY, Some(&path)).unwrap(); + let khline = KhLine::from(&cal.get_principal_event()); + assert_eq!(String::from("1182988800 test/path"), khline.unwrap().to_string()); + } +} @@ -1,4 +1,5 @@ pub mod actions; +pub mod khline; pub mod config; pub mod defaults; pub mod icalwrap; |