From bd59a5128c05dcd550e91bbdd0cd9d5996a65586 Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 9 Jan 2022 09:43:21 +0100 Subject: Persist events to sqlite db --- src/model/event.rs | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) (limited to 'src/model') diff --git a/src/model/event.rs b/src/model/event.rs index d1d9775..2650c47 100644 --- a/src/model/event.rs +++ b/src/model/event.rs @@ -1,35 +1,31 @@ use chrono::Timelike; use chrono::{NaiveDate, NaiveTime}; +// #[derive(Debug, Clone, sqlx::FromRow)] #[derive(Debug, Clone)] pub struct Event { pub date: NaiveDate, - pub time: Time, + pub start: Option, + pub end: Option, pub name: String, } impl Event { pub fn pprint(&self) -> String { - match self.time { - Time::AllDay => self.name.clone(), - Time::Time { start, end: None } => format!("{} {}", pprint_time(start), self.name), - Time::Time { - start, - end: Some(e), - } => format!("{}-{} {}", pprint_time(start), pprint_time(e), self.name), - } + let start = self.start.map(pprint_time).unwrap_or_default(); + let end = self + .end + .map(|t| format!("-{}", pprint_time(t))) + .unwrap_or_default(); + let space = if self.start.is_some() || self.end.is_some() { + " " + } else { + "" + }; + format!("{}{}{}{}", start, end, space, self.name) } } -#[derive(Debug, Clone, Copy, PartialOrd, PartialEq, Eq, Ord)] -pub enum Time { - AllDay, - Time { - start: NaiveTime, - end: Option, - }, -} - fn pprint_time(t: NaiveTime) -> String { if t.minute() == 0 { format!("{}h", t.hour()) @@ -37,3 +33,16 @@ fn pprint_time(t: NaiveTime) -> String { format!("{}h{}", t.hour(), t.minute()) } } + +pub fn parse_time(t: &str) -> Option { + match t.split('h').collect::>()[..] { + [hours, minutes] => { + if minutes.trim().is_empty() { + NaiveTime::from_hms_opt(hours.parse().ok()?, 0, 0) + } else { + NaiveTime::from_hms_opt(hours.parse().ok()?, minutes.parse().ok()?, 0) + } + } + _ => None, + } +} -- cgit v1.2.3