Skip to main content

trail_config/
error.rs

1use std::{error::Error, fmt, io};
2
3#[derive(Debug)]
4pub enum ConfigError {
5    IoError(io::Error),
6    YamlError(String),
7    JsonError(String),
8    TomlError(String),
9    PathNotFound(String),
10    FormatError(String),
11}
12impl fmt::Display for ConfigError {
13    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14        match self {
15            ConfigError::IoError(e) => write!(f, "IO error: {}", e),
16            ConfigError::YamlError(msg) => write!(f, "YAML parse error: {}", msg),
17            ConfigError::JsonError(msg) => write!(f, "JSON parse error: {}", msg),
18            ConfigError::TomlError(msg) => write!(f, "TOML parse error: {}", msg),
19            ConfigError::PathNotFound(path) => write!(f, "Path not found in config: {}", path),
20            ConfigError::FormatError(msg) => write!(f, "Format error: {}", msg),
21        }
22    }
23}
24
25impl Error for ConfigError {}
26
27impl From<io::Error> for ConfigError {
28    fn from(err: io::Error) -> Self {
29        ConfigError::IoError(err)
30    }
31}
32
33impl From<yaml_serde::Error> for ConfigError {
34    fn from(err: yaml_serde::Error) -> Self {
35        ConfigError::YamlError(err.to_string())
36    }
37}