From f58c120bcc0a84156e5e253be57c1f958ea8576e Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 27 Feb 2022 09:36:14 +0100 Subject: Don’t create an event if repetition is not valid --- src/model/repetition.rs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'src/model') 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 { - let n = str.parse::().ok()?; +pub fn validate_period(str: &str) -> Result { + let n = str.parse::().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 { + let n = str.parse::().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()) } } -- cgit v1.2.3