ostrya_core/config/
error.rs1use std::fmt;
4
5#[derive(Debug, Clone, PartialEq, Eq)]
26#[non_exhaustive]
27pub enum ConfigError {
28 Core(crate::Error),
34 InvalidFormat(String),
36 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}