1use std::fmt;
4
5pub type Result<T> = std::result::Result<T, ConfigError>;
7
8#[derive(Debug)]
10pub enum ConfigError {
11 Io {
13 message: String,
14 source: std::io::Error,
15 },
16 Parse { message: String, file_type: String },
18 Validation { message: String },
20 Detection { message: String },
22 Other { message: String },
24}
25
26impl fmt::Display for ConfigError {
27 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28 match self {
29 ConfigError::Io { message, .. } => write!(f, "IO error: {}", message),
30 ConfigError::Parse { message, file_type } => {
31 write!(f, "Parse error in {}: {}", file_type, message)
32 }
33 ConfigError::Validation { message } => write!(f, "Validation error: {}", message),
34 ConfigError::Detection { message } => write!(f, "Detection error: {}", message),
35 ConfigError::Other { message } => write!(f, "{}", message),
36 }
37 }
38}
39
40impl std::error::Error for ConfigError {
41 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
42 match self {
43 ConfigError::Io { source, .. } => Some(source),
44 _ => None,
45 }
46 }
47}
48
49impl From<std::io::Error> for ConfigError {
50 fn from(err: std::io::Error) -> Self {
51 ConfigError::Io {
52 message: err.to_string(),
53 source: err,
54 }
55 }
56}
57
58impl From<toml::de::Error> for ConfigError {
59 fn from(err: toml::de::Error) -> Self {
60 ConfigError::Parse {
61 message: err.to_string(),
62 file_type: "TOML".to_string(),
63 }
64 }
65}
66
67impl From<toml::ser::Error> for ConfigError {
68 fn from(err: toml::ser::Error) -> Self {
69 ConfigError::Parse {
70 message: err.to_string(),
71 file_type: "TOML".to_string(),
72 }
73 }
74}
75
76impl From<serde_json::Error> for ConfigError {
77 fn from(err: serde_json::Error) -> Self {
78 ConfigError::Parse {
79 message: err.to_string(),
80 file_type: "JSON".to_string(),
81 }
82 }
83}
84
85impl From<figment::Error> for ConfigError {
86 fn from(err: figment::Error) -> Self {
87 ConfigError::Other {
88 message: format!("Figment error: {}", err),
89 }
90 }
91}