From 7e7dfc3d76b810f22f68f4e5334c9d89dd7d85cc Mon Sep 17 00:00:00 2001 From: Ruben Pollan Date: Tue, 28 Aug 2018 20:33:43 +0200 Subject: Let's use Option the rustic way --- src/date.rs | 6 +----- src/event.rs | 4 ++-- src/periodic.rs | 16 ++++++++-------- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/date.rs b/src/date.rs index f77b5d5..c329ac3 100644 --- a/src/date.rs +++ b/src/date.rs @@ -17,7 +17,7 @@ pub enum Date { impl Date { - pub fn empty() -> Date { + pub fn new() -> Date { Date::Time(UTC.timestamp(0, 0)) } @@ -26,10 +26,6 @@ impl Date { Date::Time(UTC.from_utc_datetime(&Utc::now().naive_utc())) } - pub fn is_empty(&self) -> bool { - *self == Date::empty() - } - pub fn parse(date_str: &str, time_zone: &str) -> Result { let absolute_time = date_str.chars().rev().next().unwrap() == 'Z'; let tz: Tz = if absolute_time { diff --git a/src/event.rs b/src/event.rs index d086295..db32b22 100644 --- a/src/event.rs +++ b/src/event.rs @@ -38,8 +38,8 @@ impl Event { location: "".to_string(), description: "".to_string(), status: Status::Confirmed, - start: Date::empty(), - end: End::Date(Date::empty()), + start: Date::new(), + end: End::Date(Date::new()), }; } diff --git a/src/periodic.rs b/src/periodic.rs index b4bf7bf..0c52a9b 100644 --- a/src/periodic.rs +++ b/src/periodic.rs @@ -12,8 +12,8 @@ pub struct Periodic { pub event: Event, pub freq: Freq, pub interval: i64, - pub count: i64, - pub until: Date, + pub count: Option, + pub until: Option, } #[derive(Debug)] @@ -33,8 +33,8 @@ impl Periodic { event: Event::new(), freq: Freq::Secondly, interval: 1, - until: Date::empty(), - count: 0, + count: None, + until: None, } } @@ -42,8 +42,8 @@ impl Periodic { match param { "FREQ" => self.freq = value.parse()?, "INTERVAL" => self.interval = value.parse()?, - "COUNT" => self.count = value.parse()?, - "UNTIL" => self.until = Date::parse(&value, "")?, + "COUNT" => self.count = Some(value.parse()?), + "UNTIL" => self.until = Some(Date::parse(&value, "")?), _ => (), } Ok(()) @@ -55,8 +55,8 @@ impl Periodic { let mut events = Vec::new(); let mut count = 0; while start <= *last { - if (!self.until.is_empty() && start <= self.until) || - (count != 0 && count >= self.count) + if (self.until.is_some() && start <= self.until.unwrap()) || + (self.count.is_some() && count >= self.count.unwrap()) { break; } -- cgit v1.2.3