]> saetta.ns0.it Git - rust/zakform/commitdiff
Added validator for Time.
authorAndrea Zagli <azagli@libero.it>
Mon, 1 Dec 2025 09:35:43 +0000 (10:35 +0100)
committerAndrea Zagli <azagli@libero.it>
Mon, 1 Dec 2025 09:35:43 +0000 (10:35 +0100)
Cargo.toml
po/it.po
src/validators.rs

index 1b16c912ec6652d82e1d8f80424dbfc5c4b0e0bc..54265c9d22283f9e47eee86530b44836961e26c6 100644 (file)
@@ -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"
 
index 12d97acf5e33ce584519825e0dd7eae0927d1e7f..981a351df664724e3be914f2c4560670e0dd94ce 100644 (file)
--- 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"
index c7136dff8c39d202b66913cda78a6cbc0b146751..0a8d2d699cdab3c736d26f6b63b08bb9dcf8e99d 100644 (file)
@@ -244,6 +244,160 @@ impl ValidatorDate {
        }
 }
 
+pub enum ValidatorTimeCompareType {
+       Less,
+       LessEq,
+       Eq,
+       Great,
+       GreatEq,
+}
+
+#[derive(Default)]
+pub struct ValidatorTime {
+       validator: Validator,
+       compare_type: Option<ValidatorTimeCompareType>,
+       compare_value: Option<String>,
+       compare_value_time: Option<chrono::NaiveTime>,
+}
+
+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<ValidatorTimeCompareType> {
+               &self.compare_type
+       }
+
+       pub fn set_compare_type(&mut self, compare_type: Option<ValidatorTimeCompareType>) {
+               self.compare_type = compare_type;
+       }
+
+       pub fn get_compare_value(&self) -> &Option<String> {
+               &self.compare_value
+       }
+
+       pub fn set_compare_value(&mut self, compare_value: Option<String>) {
+               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,