Skip to main content

app_json_settings/core/
error.rs

1use std::fmt;
2use std::io;
3
4#[derive(Debug)]
5pub enum ConfigError {
6    Io(io::Error),
7    Serialize(serde_json::Error),
8    Deserialize(serde_json::Error),
9    Platform(String),
10}
11
12impl From<io::Error> for ConfigError {
13    fn from(e: io::Error) -> Self {
14        ConfigError::Io(e)
15    }
16}
17
18impl From<serde_json::Error> for ConfigError {
19    fn from(e: serde_json::Error) -> Self {
20        if e.is_data() || e.is_syntax() {
21            ConfigError::Deserialize(e)
22        } else {
23            ConfigError::Serialize(e)
24        }
25    }
26}
27
28impl fmt::Display for ConfigError {
29    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30        match self {
31            ConfigError::Io(e) => write!(f, "I/O error: {e}"),
32            ConfigError::Serialize(e) => write!(f, "JSON serialization error: {e}"),
33            ConfigError::Deserialize(e) => write!(f, "JSON deserialization error: {e}"),
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::Platform(_) => None,
45        }
46    }
47}
48
49pub type Result<T> = std::result::Result<T, ConfigError>;