stork_lib/config/
errors.rs

1use thiserror::Error;
2
3#[derive(Error, Debug)]
4pub enum ConfigReadError {
5    #[error("Recieved empty configuration string")]
6    EmptyString,
7
8    #[error("Cannot parse config as TOML. Stork recieved error: `{0}`")]
9    UnparseableTomlInput(#[from] toml::de::Error),
10
11    #[error("Cannot parse config as JSON. Stork recieved error: `{0}`")]
12    UnparseableJsonInput(#[from] serde_json::Error),
13}
14
15impl PartialEq for ConfigReadError {
16    fn eq(&self, other: &Self) -> bool {
17        match (self, other) {
18            (Self::UnparseableTomlInput(l0), Self::UnparseableTomlInput(r0)) => l0 == r0,
19
20            // default case also catches UnparseableJsonInput, which would otherwise look like
21            // the TomlInput case above, except serde_json::Error doesn't impl PartialEq.
22            _ => core::mem::discriminant(self) == core::mem::discriminant(other),
23        }
24    }
25}
26
27#[cfg(test)]
28mod tests {
29    use super::*;
30    use pretty_assertions::assert_eq;
31
32    #[test]
33    fn from_toml_error() {
34        let expected = "Cannot parse config as TOML. Stork recieved error: `expected an equals, found an identifier at line 1 column 6`";
35        let computed = toml::from_str::<()>("this is bad toml")
36            .map_err(ConfigReadError::from)
37            .unwrap_err()
38            .to_string();
39        assert_eq!(expected, computed);
40    }
41
42    #[test]
43    fn partial_eq_json() {
44        let json_error_one = serde_json::from_str::<()>("this is not json").unwrap_err();
45        let json_error_two = serde_json::from_str::<()>("{[}").unwrap_err();
46
47        let config_read_error_one = ConfigReadError::UnparseableJsonInput(json_error_one);
48        let config_read_error_two = ConfigReadError::UnparseableJsonInput(json_error_two);
49
50        assert_eq!(config_read_error_one, config_read_error_two);
51    }
52}