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

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

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

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

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

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

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

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

            
51
    #[must_use]
52
    pub const fn is_notice(&self) -> bool {
53
        matches!(self.status, ValidationStatus::Notice(_))
54
    }
55
}