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
6
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13
6
        match self {
14
6
            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
6
    }
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
6
    pub fn error(message: impl Into<String>) -> Self {
30
6
        Self {
31
6
            status: ValidationStatus::Error(message.into()),
32
6
        }
33
6
    }
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
6
    pub const fn is_error(&self) -> bool {
49
6
        matches!(self.status, ValidationStatus::Error(_))
50
6
    }
51

            
52
    pub const fn is_success(&self) -> bool {
53
        matches!(self.status, ValidationStatus::Success(_))
54
    }
55

            
56
    pub const fn is_notice(&self) -> bool {
57
        matches!(self.status, ValidationStatus::Notice(_))
58
    }
59

            
60
6
    pub const fn has_message(&self) -> bool {
61
6
        !matches!(self.status, ValidationStatus::None)
62
6
    }
63
}