diff options
author | Joris | 2022-03-19 22:03:58 +0100 |
---|---|---|
committer | Joris | 2022-03-19 22:03:58 +0100 |
commit | 35cc74578e969bae4812afd2ff041eba3746142d (patch) | |
tree | c0ef18d3c81554f0e70c91fcb1006d587470365c /src/model/event.rs | |
parent | 199624dc03ead28ddc7454147457512d9568c593 (diff) |
Allow to repeat an event until a specific date
Also provide a shortcut to modify a repetead event from a specific
occurence. This set the end repetition date under the hood and create a
new repeated event.
Diffstat (limited to 'src/model/event.rs')
-rw-r--r-- | src/model/event.rs | 52 |
1 files changed, 7 insertions, 45 deletions
diff --git a/src/model/event.rs b/src/model/event.rs index 5e92692..e556f6e 100644 --- a/src/model/event.rs +++ b/src/model/event.rs @@ -1,9 +1,10 @@ -use chrono::Timelike; use chrono::{NaiveDate, NaiveTime}; use std::collections::HashMap; use uuid::Uuid; use crate::model::repetition::Repetition; +use crate::model::time; +use crate::validation; pub static DATE_FORMAT: &str = "%d/%m/%Y"; @@ -19,10 +20,10 @@ pub struct Event { impl Event { pub fn pprint(&self) -> String { - let start = self.start.map(pprint_time).unwrap_or_default(); + let start = self.start.map(time::pprint).unwrap_or_default(); let end = self .end - .map(|t| format!("-{}", pprint_time(t))) + .map(|t| format!("-{}", time::pprint(t))) .unwrap_or_default(); let space = if self.start.is_some() || self.end.is_some() { " " @@ -52,27 +53,6 @@ pub fn repetitions_between( res } -pub fn pprint_time(t: NaiveTime) -> String { - if t.minute() == 0 { - format!("{}h", t.hour()) - } else { - format!("{}h{}", t.hour(), t.minute()) - } -} - -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, - } -} - // Validation pub fn validate( @@ -83,8 +63,8 @@ pub fn validate( end: String, repetition: Option<Repetition>, ) -> Option<Event> { - let start = validate_time(start)?; - let end = validate_time(end)?; + let start = validation::time(start)?; + let end = validation::time(end)?; match (start, end) { (Some(s), Some(e)) if s > e => None?, @@ -94,27 +74,9 @@ pub fn validate( Some(Event { id, date: NaiveDate::parse_from_str(&date, DATE_FORMAT).ok()?, - name: validate_name(name)?, + name: validation::non_empty(name)?, start, end, repetition, }) } - -fn validate_time(time: String) -> Option<Option<NaiveTime>> { - let time = time.trim(); - if time.is_empty() { - Some(None) - } else { - parse_time(time).map(Some) - } -} - -fn validate_name(name: String) -> Option<String> { - let name = name.trim(); - if name.is_empty() { - None - } else { - Some(name.to_string()) - } -} |