From: Andrea Zagli Date: Mon, 1 Dec 2025 09:35:43 +0000 (+0100) Subject: Added validator for Time. X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=710441d513652372b7a64818da037db427140e35;p=rust%2Fzakform Added validator for Time. --- diff --git a/Cargo.toml b/Cargo.toml index 1b16c91..54265c9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,8 +11,8 @@ serde_derive = "1" serde_json = "1" tera = "1.8.0" chrono = { version = "0.4" } -lazy_static = "1.4.0" -gettext = "0.4.0" +lazy_static = "1.4" +gettext = "0.4" regex = "1.5" heck = "0.5" diff --git a/po/it.po b/po/it.po index 12d97ac..981a351 100644 --- a/po/it.po +++ b/po/it.po @@ -27,5 +27,8 @@ msgstr "Valore non valido" msgid "Invalid date" msgstr "Data non valida" +msgid "Invalid time" +msgstr "Ora non valida" + msgid "Invalid number" msgstr "Numero non valido" diff --git a/src/validators.rs b/src/validators.rs index c7136df..0a8d2d6 100644 --- a/src/validators.rs +++ b/src/validators.rs @@ -244,6 +244,160 @@ impl ValidatorDate { } } +pub enum ValidatorTimeCompareType { + Less, + LessEq, + Eq, + Great, + GreatEq, +} + +#[derive(Default)] +pub struct ValidatorTime { + validator: Validator, + compare_type: Option, + compare_value: Option, + compare_value_time: Option, +} + +impl TValidator for ValidatorTime { + fn get_name(&self) -> String { + String::from(&self.validator.name) + } + + fn get_message(&self) -> String { + String::from(&self.validator.message) + } + + fn set_message(&mut self, message: &str) { + self.validator.message = String::from(message); + } + + fn validate(&self, value: &String) -> String { + let value_ = value.trim(); + if value_ == "" { + return String::from(""); + } + + let d = match chrono::NaiveTime::parse_from_str(value_, "%H:%M:%S" ) { + Ok(r) => { r }, + Err(_) => { + /* try without seconds */ + match chrono::NaiveTime::parse_from_str(value_, "%H:%M" ) { + Ok(r) => { r }, + Err(_) => { + return self.get_message(); + } + } + } + }; + + let c: chrono::NaiveTime; + match &self.compare_value { + Some(s) => { + if s == "@now" { + let now = chrono::Local::now().naive_local(); + c = now.time(); + } else { + match self.compare_value_time { + Some(x) => { c = x; }, + None => { return String::from(""); }, + }; + } + }, + None => { return String::from(""); }, + }; + + match &self.compare_type { + Some(t) => { + match t { + ValidatorTimeCompareType::Less => { + if d >= c { + return self.get_message(); + } + }, + ValidatorTimeCompareType::LessEq => { + if d > c { + return self.get_message(); + } + }, + ValidatorTimeCompareType::Eq => { + if d != c { + return self.get_message(); + } + }, + ValidatorTimeCompareType::Great => { + if d <= c { + return self.get_message(); + } + }, + ValidatorTimeCompareType::GreatEq => { + if d < c { + return self.get_message(); + } + }, + }; + }, + None => { }, + }; + + return String::from(""); + } +} + +impl ValidatorTime { + pub fn new(name: &str) -> Self { + let mut v: ValidatorTime = Default::default(); + v.validator.name = String::from(name); + v.validator.message = String::from(super::TR.gettext("Invalid time")); + + v + } + + pub fn get_compare_type(&self) -> &Option { + &self.compare_type + } + + pub fn set_compare_type(&mut self, compare_type: Option) { + self.compare_type = compare_type; + } + + pub fn get_compare_value(&self) -> &Option { + &self.compare_value + } + + pub fn set_compare_value(&mut self, compare_value: Option) { + match compare_value { + Some(c) => { + if c == "@now" { + self.compare_value = Some(c); + self.compare_value_time = None; // convert on validate + } else { + match chrono::NaiveTime::parse_from_str(&c, "%H:%M:%S") { + Ok(d) => { + self.compare_value = Some(c); + self.compare_value_time = Some(d); + }, + Err(_) => { + /* try without seconds */ + match chrono::NaiveTime::parse_from_str(&c, "%H:%M") { + Ok(d) => { + self.compare_value = Some(c); + self.compare_value_time = Some(d); + }, + Err(_) => { + self.compare_value = None; + } + } + } + }; + } + }, + None => { self.compare_value = None; }, + } + } +} + pub enum ValidatorNumberCompareType { Less, LessEq,