diff options
author | Joris | 2022-01-09 09:43:21 +0100 |
---|---|---|
committer | Joris | 2022-01-09 10:11:29 +0100 |
commit | bd59a5128c05dcd550e91bbdd0cd9d5996a65586 (patch) | |
tree | 541f7d49253ad3e7c8dfab480f33a2b10107b0d2 /src/model | |
parent | ce978143f1360e16e85587644055a9f83d11c64c (diff) |
Persist events to sqlite db
Diffstat (limited to 'src/model')
-rw-r--r-- | src/model/event.rs | 45 |
1 files changed, 27 insertions, 18 deletions
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<NaiveTime>, + pub end: Option<NaiveTime>, 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<NaiveTime>, - }, -} - 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<NaiveTime> { + match t.split('h').collect::<Vec<&str>>()[..] { + [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, + } +} |