Skip to main content

app_json_settings/core/
error.rs

1use std::fmt;
2use std::io;
3
4/// Error type returned by `app-json-settings` operations.
5#[derive(Debug)]
6pub enum ConfigError {
7    /// File-system or stream I/O failed.
8    Io(io::Error),
9    /// JSON serialization failed while saving a configuration value.
10    Serialize(serde_json::Error),
11    /// JSON deserialization failed while loading a configuration value.
12    Deserialize(serde_json::Error),
13    /// A caller-supplied file name or path component is unsafe.
14    InvalidPathComponent(String),
15    /// A platform-specific storage resolver failed.
16    Platform(String),
17}
18
19impl From<io::Error> for ConfigError {
20    fn from(e: io::Error) -> Self {
21        ConfigError::Io(e)
22    }
23}
24
25impl fmt::Display for ConfigError {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        match self {
28            ConfigError::Io(e) => write!(f, "I/O error: {e}"),
29            ConfigError::Serialize(e) => write!(f, "JSON serialization error: {e}"),
30            ConfigError::Deserialize(e) => write!(f, "JSON deserialization error: {e}"),
31            ConfigError::InvalidPathComponent(value) => {
32                write!(f, "invalid path component: {value:?}")
33            }
34            ConfigError::Platform(e) => write!(f, "platform error: {e}"),
35        }
36    }
37}
38
39impl std::error::Error for ConfigError {
40    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
41        match self {
42            ConfigError::Io(e) => Some(e),
43            ConfigError::Serialize(e) | ConfigError::Deserialize(e) => Some(e),
44            ConfigError::InvalidPathComponent(_) | ConfigError::Platform(_) => None,
45        }
46    }
47}
48
49pub type Result<T> = std::result::Result<T, ConfigError>;