diff options
author | Joris | 2022-01-09 13:12:45 +0100 |
---|---|---|
committer | Joris | 2022-01-09 13:12:45 +0100 |
commit | 99af88a840bef534540a4b273d24a8a17e7fc9b9 (patch) | |
tree | 20bda6c9eccd66d0c165e1f5d42fe76f80be58d2 /src/model | |
parent | bd59a5128c05dcd550e91bbdd0cd9d5996a65586 (diff) |
Split app into modules
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()) + } +} |