profig_commons/
error.rs

1use std::error::Error;
2use std::fmt;
3use std::io;
4
5#[derive(Debug)]
6pub enum ProfigError {
7    Io(io::Error),
8    Parse { format: &'static str, error: String },
9    InvalidFormat(String),
10    Validation(String),
11    Custom(String),
12}
13
14impl fmt::Display for ProfigError {
15    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16        match self {
17            ProfigError::Io(e) => write!(f, "I/O Error: {}", e),
18            ProfigError::Parse { format, error } => {
19                write!(f, "Failed to parse {}: {}", format, error)
20            }
21            ProfigError::InvalidFormat(msg) => write!(f, "Invalid Format: {}", msg),
22            ProfigError::Validation(msg) => write!(f, "Validation Error: {}", msg),
23            ProfigError::Custom(msg) => write!(f, "{}", msg),
24        }
25    }
26}
27
28impl Error for ProfigError {
29    fn source(&self) -> Option<&(dyn Error + 'static)> {
30        match self {
31            ProfigError::Io(e) => Some(e),
32            _ => None,
33        }
34    }
35}
36
37impl From<io::Error> for ProfigError {
38    fn from(err: io::Error) -> Self {
39        ProfigError::Io(err)
40    }
41}