Skip to main content

ostrya_core/config/
error.rs

1//! The error of the typed config API.
2
3use std::fmt;
4
5/// An error of the typed config API.
6///
7/// The typed reads and edits of this crate give this error. These are the
8/// sources:
9///
10/// - A [`ValueError`](super::ValueError), a value that a key cannot read,
11///   gives a variant with the group and the key of the value.
12/// - A required key with no value and an unknown key of a closed group give
13///   [`InvalidFormat`](ConfigError::InvalidFormat).
14/// - A section name that a key file cannot hold gives
15///   [`InvalidInput`](ConfigError::InvalidInput).
16/// - A value text that a key file cannot hold gives
17///   [`Core`](ConfigError::Core).
18///
19/// Each variant prints the same text as the variant of `ostrya::Error` with
20/// the same name, and the `ostrya` crate converts each variant to that
21/// variant.
22///
23/// The enum is `#[non_exhaustive]`, so a release can add a variant without a
24/// breaking change.
25#[derive(Debug, Clone, PartialEq, Eq)]
26#[non_exhaustive]
27pub enum ConfigError {
28    /// An error of the key file.
29    ///
30    /// A value that is not a boolean or an integer, and a malformed escape
31    /// sequence, give [`Error::KeyFile`](crate::Error::KeyFile). The text is
32    /// the text of the error, with no prefix.
33    Core(crate::Error),
34    /// A value that does not match its expected format.
35    InvalidFormat(String),
36    /// An argument outside the values that the operation accepts.
37    InvalidInput(String),
38}
39
40impl fmt::Display for ConfigError {
41    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42        match self {
43            ConfigError::Core(e) => write!(f, "{e}"),
44            ConfigError::InvalidFormat(reason) => write!(f, "invalid format: {reason}"),
45            ConfigError::InvalidInput(reason) => write!(f, "invalid input: {reason}"),
46        }
47    }
48}
49
50impl std::error::Error for ConfigError {
51    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
52        match self {
53            ConfigError::Core(e) => std::error::Error::source(e),
54            _ => None,
55        }
56    }
57}
58
59impl From<crate::Error> for ConfigError {
60    fn from(e: crate::Error) -> Self {
61        ConfigError::Core(e)
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn each_variant_prints_the_text_of_its_namesake() {
71        let core = crate::Error::KeyFile("value 'x' for core.fsync is not a boolean".into());
72        assert_eq!(
73            ConfigError::from(core.clone()),
74            ConfigError::Core(core.clone())
75        );
76        assert_eq!(
77            ConfigError::Core(core).to_string(),
78            "keyfile: value 'x' for core.fsync is not a boolean"
79        );
80        assert_eq!(
81            ConfigError::InvalidFormat("[core] fsync is empty".into()).to_string(),
82            "invalid format: [core] fsync is empty"
83        );
84        assert_eq!(
85            ConfigError::InvalidInput("no section".into()).to_string(),
86            "invalid input: no section"
87        );
88    }
89
90    #[test]
91    fn the_source_of_core_is_the_source_of_the_inner_error() {
92        use std::error::Error as _;
93
94        let inner = crate::Error::Gvariant(ostrya_gvariant::Error::InvalidValue("truncated"));
95        let err = ConfigError::Core(inner.clone());
96        assert_eq!(
97            err.source().map(ToString::to_string),
98            inner.source().map(ToString::to_string)
99        );
100        assert!(err.source().is_some());
101        assert!(ConfigError::InvalidFormat("x".into()).source().is_none());
102    }
103}