diff options
Diffstat (limited to 'src/model')
-rw-r--r-- | src/model/event.rs | 42 |
1 files changed, 40 insertions, 2 deletions
diff --git a/src/model/event.rs b/src/model/event.rs index 2650c47..a0bdc85 100644 --- a/src/model/event.rs +++ b/src/model/event.rs @@ -1,7 +1,8 @@ use chrono::Timelike; use chrono::{NaiveDate, NaiveTime}; -// #[derive(Debug, Clone, sqlx::FromRow)] +pub static DATE_FORMAT: &str = "%d/%m/%Y"; + #[derive(Debug, Clone)] pub struct Event { pub date: NaiveDate, @@ -34,7 +35,7 @@ fn pprint_time(t: NaiveTime) -> String { } } -pub fn parse_time(t: &str) -> Option<NaiveTime> { +fn parse_time(t: &str) -> Option<NaiveTime> { match t.split('h').collect::<Vec<&str>>()[..] { [hours, minutes] => { if minutes.trim().is_empty() { @@ -46,3 +47,40 @@ pub fn parse_time(t: &str) -> Option<NaiveTime> { _ => None, } } + +// Validation + +pub fn validate(date: String, name: String, start: String, end: String) -> Option<Event> { + let start = validate_time(start)?; + let end = validate_time(end)?; + + match (start, end) { + (Some(s), Some(e)) if s > e => None?, + _ => (), + } + + Some(Event { + date: NaiveDate::parse_from_str(&date, DATE_FORMAT).ok()?, + name: validate_name(name)?, + start, + end, + }) +} + +fn validate_time(time: String) -> Option<Option<NaiveTime>> { + let time = time.trim(); + if time.is_empty() { + Some(None) + } else { + parse_time(time).map(|t| Some(t)) + } +} + +fn validate_name(name: String) -> Option<String> { + let name = name.trim(); + if name.is_empty() { + None + } else { + Some(name.to_string()) + } +} |