Skip to main content

lmn_core/config/
error.rs

1#[derive(Debug)]
2pub enum ConfigError {
3    Fs(String),
4    GeneralError,
5    InvalidFormat(String),
6    TemplateAlreadyExists(String),
7    TemplateNotFound(String),
8    /// YAML parsing failed on a `LumenConfig` file.
9    YamlParseError(serde_norway::Error),
10    /// The config was parseable but contained invalid values.
11    ValidationError(String),
12}
13
14impl std::fmt::Display for ConfigError {
15    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
16        match self {
17            Self::Fs(e) => write!(f, "FS Error: {}", e),
18            Self::GeneralError => write!(f, "General FS Error"),
19            Self::InvalidFormat(format) => write!(f, "Invalid format: {}", format),
20            Self::TemplateAlreadyExists(name) => {
21                write!(f, "Template \"{}\" already exists", name)
22            }
23            Self::TemplateNotFound(name) => write!(f, "Template \"{}\" not found", name),
24            Self::YamlParseError(e) => write!(f, "YAML parse error: {}", e),
25            Self::ValidationError(msg) => write!(f, "Config validation error: {}", msg),
26        }
27    }
28}
29
30impl std::error::Error for ConfigError {}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35
36    #[test]
37    fn display_fs() {
38        assert_eq!(
39            ConfigError::Fs("file.json".into()).to_string(),
40            "FS Error: file.json"
41        );
42    }
43
44    #[test]
45    fn display_general_error() {
46        assert_eq!(ConfigError::GeneralError.to_string(), "General FS Error");
47    }
48
49    #[test]
50    fn display_invalid_format() {
51        assert_eq!(
52            ConfigError::InvalidFormat("bad".into()).to_string(),
53            "Invalid format: bad"
54        );
55    }
56
57    #[test]
58    fn display_template_already_exists() {
59        assert_eq!(
60            ConfigError::TemplateAlreadyExists("foo".into()).to_string(),
61            "Template \"foo\" already exists"
62        );
63    }
64
65    #[test]
66    fn display_template_not_found() {
67        assert_eq!(
68            ConfigError::TemplateNotFound("bar".into()).to_string(),
69            "Template \"bar\" not found"
70        );
71    }
72}