Skip to main content

web/pages/validation/
feedback.rs

1use askama::Template;
2use std::fmt;
3
4pub enum ValidationStatus {
5    Error(String),
6    Success(String),
7    Notice(String),
8    None,
9}
10
11impl fmt::Display for ValidationStatus {
12    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13        match self {
14            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    }
20}
21
22#[derive(Template)]
23#[template(path = "components/validation/feedback.html")]
24pub struct ValidationFeedback {
25    status: ValidationStatus,
26}
27
28impl ValidationFeedback {
29    pub fn error(message: impl Into<String>) -> Self {
30        Self {
31            status: ValidationStatus::Error(message.into()),
32        }
33    }
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    pub const fn is_error(&self) -> bool {
50        matches!(self.status, ValidationStatus::Error(_))
51    }
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    pub const fn has_message(&self) -> bool {
65        !matches!(self.status, ValidationStatus::None)
66    }
67}