aboutsummaryrefslogtreecommitdiff
path: root/src/model
diff options
context:
space:
mode:
authorJoris2022-02-27 09:36:14 +0100
committerJoris2022-02-27 09:36:14 +0100
commitf58c120bcc0a84156e5e253be57c1f958ea8576e (patch)
tree664951a5b791469fea036a0552946f1bfc995116 /src/model
parent5f1f9065476a46ccced3f1b0e31a90d3a00eccef (diff)
Don’t create an event if repetition is not valid
Diffstat (limited to 'src/model')
-rw-r--r--src/model/repetition.rs20
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())
}
}