1
use askama::Template;
2
use std::fmt;
3

            
4
pub enum ValidationStatus {
5
    Error(String),
6
    Success(String),
7
    Notice(String),
8
    None,
9
}
10

            
11
impl fmt::Display for ValidationStatus {
12
3
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13
3
        match self {
14
3
            ValidationStatus::Error(msg) => write!(f, "{msg}"),
15
            ValidationStatus::Success(msg) => write!(f, "{msg}"),
16
            ValidationStatus::Notice(msg) => write!(f, "{msg}"),
17
            ValidationStatus::None => write!(f, ""),
18
        }
19
3
    }
20
}
21

            
22
#[derive(Template)]
23
#[template(path = "components/validation/feedback.html")]
24
pub struct ValidationFeedback {
25
    status: ValidationStatus,
26
}
27

            
28
impl ValidationFeedback {
29
3
    pub fn error(message: impl Into<String>) -> Self {
30
3
        Self {
31
3
            status: ValidationStatus::Error(message.into()),
32
3
        }
33
3
    }
34

            
35
    pub fn success(message: impl Into<String>) -> Self {
36
        Self {
37
            status: ValidationStatus::Success(message.into()),
38
        }
39
    }
40

            
41
    pub fn notice(message: impl Into<String>) -> Self {
42
        Self {
43
            status: ValidationStatus::Notice(message.into()),
44
        }
45
    }
46

            
47
    // Helper methods for template
48
    #[must_use]
49
3
    pub const fn is_error(&self) -> bool {
50
3
        matches!(self.status, ValidationStatus::Error(_))
51
3
    }
52

            
53
    #[must_use]
54
    pub const fn is_success(&self) -> bool {
55
        matches!(self.status, ValidationStatus::Success(_))
56
    }
57

            
58
    #[must_use]
59
    pub const fn is_notice(&self) -> bool {
60
        matches!(self.status, ValidationStatus::Notice(_))
61
    }
62

            
63
    #[must_use]
64
3
    pub const fn has_message(&self) -> bool {
65
3
        !matches!(self.status, ValidationStatus::None)
66
3
    }
67
}