1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use crate::InputState;

#[derive(Clone)]
pub enum Validator {
    None,
    Custom(std::rc::Rc<dyn Fn(&str) -> InputState>),
}

impl Validator {
    pub fn is_custom(&self) -> bool {
        match self {
            Validator::Custom(_) => true,
            _ => false,
        }
    }
}

impl Default for Validator {
    fn default() -> Self {
        Self::None
    }
}

/// Validators are equal if they are still None. Everything else is a change.
impl PartialEq for Validator {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Validator::None, Validator::None) => true,
            _ => false,
        }
    }
}

impl<F> From<F> for Validator
where
    F: Fn(&str) -> InputState + 'static,
{
    fn from(v: F) -> Self {
        Self::Custom(std::rc::Rc::new(v))
    }
}