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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//! Validation

#[derive(Clone, Debug)]
pub struct ValidationContext<T> {
    pub value: T,
    pub initial: bool,
}

impl<T> From<T> for ValidationContext<T> {
    fn from(value: T) -> Self {
        ValidationContext {
            value,
            initial: false,
        }
    }
}

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

impl<T, S> Validator<T, S> {
    pub fn is_custom(&self) -> bool {
        matches!(self, Self::Custom(_))
    }

    /// Convert into the context and run
    pub fn run<C>(&self, ctx: C) -> Option<S>
    where
        C: Into<ValidationContext<T>>,
    {
        self.run_if(|| ctx.into())
    }

    /// Only convert when necessary, and run.
    pub fn run_if<F>(&self, f: F) -> Option<S>
    where
        F: FnOnce() -> ValidationContext<T>,
    {
        match self {
            Self::Custom(validator) => Some(validator(f())),
            _ => None,
        }
    }

    /// Run with the provided context.
    pub fn run_ctx(&self, ctx: ValidationContext<T>) -> Option<S> {
        match self {
            Self::Custom(validator) => Some(validator(ctx)),
            _ => None,
        }
    }
}

impl<T, S> Default for Validator<T, S> {
    fn default() -> Self {
        Self::None
    }
}

/// Validators are equal if they are still None. Everything else is a change.
impl<T, S> PartialEq for Validator<T, S> {
    fn eq(&self, other: &Self) -> bool {
        matches!((self, other), (Validator::None, Validator::None))
    }
}

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

pub trait IntoValidator<T, S> {
    fn into_validator(self) -> Validator<T, S>;
}

impl<F, T, S> IntoValidator<T, S> for F
where
    F: Fn(ValidationContext<T>) -> S + 'static,
{
    fn into_validator(self) -> Validator<T, S> {
        Validator::Custom(std::rc::Rc::new(self))
    }
}