use chrono::NaiveDate; use std::collections::HashMap; use std::str::FromStr; use crate::model::frequency::Frequency; pub fn non_empty( form: &HashMap, field: &str, ) -> Option { let s = form.get(field)?.trim(); if s.is_empty() { None } else { Some(s.to_string()) } } pub fn parse( form: &HashMap, field: &str, ) -> Option { let s = form.get(field)?; s.parse::().ok() } pub fn date(form: &HashMap, field: &str) -> Option { let s = form.get(field)?; NaiveDate::parse_from_str(s, "%Y-%m-%d").ok() } pub fn frequency( form: &HashMap, field: &str, ) -> Option { let s = form.get(field)?; Frequency::from_str(s).ok() } pub fn color(form: &HashMap, field: &str) -> Option { let s = form.get(field)?; if s.len() == 7 && &s[0..1] == "#" && s[1..] .to_string() .into_bytes() .into_iter() .all(|c| c.is_ascii_hexdigit()) { Some(s.to_string()) } else { None } }