diff options
Diffstat (limited to 'src/model')
-rw-r--r-- | src/model/repetition.rs | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/src/model/repetition.rs b/src/model/repetition.rs index d21dd70..5288358 100644 --- a/src/model/repetition.rs +++ b/src/model/repetition.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] pub enum Repetition { - Daily { period: u8 }, + Daily { period: u32 }, Monthly { day: DayOfMonth }, Yearly, } @@ -14,12 +14,22 @@ pub enum DayOfMonth { Weekday { weekday: Weekday }, } -pub fn validate_day(str: &str) -> Option<u8> { - let n = str.parse::<u8>().ok()?; +pub fn validate_period(str: &str) -> Result<u32, String> { + let n = str.parse::<u32>().map_err(|_| format!("{} n’est pas une période valide.", str))?; + if n == 0 { + Err("La periode doit être positive.".to_string()) + } else { + Ok(n) + } +} + + +pub fn validate_day(str: &str) -> Result<u8, String> { + let n = str.parse::<u8>().map_err(|_| format!("« {} » n’est pas un jour valide.", str))?; if (1..=31).contains(&n) { - Some(n) + Ok(n) } else { - None + Err("Le jour devrait se situer entre le 1er et le 31 du mois.".to_string()) } } |