}
}
+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,