Skip to main content

openapi_nexus_config/
errors.rs

1//! Unified error types for OpenAPI Nexus configuration
2
3use std::error::Error;
4use std::fmt;
5use std::io;
6use std::path::PathBuf;
7
8/// Unified error type for all configuration-related errors
9#[derive(Debug)]
10pub enum ConfigError {
11    /// Error reading config file from filesystem
12    FileRead { path: PathBuf, source: io::Error },
13    /// Error parsing config file (TOML syntax error)
14    FileParse {
15        path: PathBuf,
16        source: toml::de::Error,
17    },
18    /// Error parsing generator config overrides from CLI
19    ParseOverrides(String),
20    /// Configuration validation error
21    Validation(String),
22}
23
24impl fmt::Display for ConfigError {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        match self {
27            ConfigError::FileRead { path, source } => {
28                write!(f, "Failed to read config file at {:?}: {}", path, source)
29            }
30            ConfigError::FileParse { path, source } => {
31                write!(f, "Failed to parse config file at {:?}: {}", path, source)
32            }
33            ConfigError::ParseOverrides(msg) => {
34                write!(f, "Failed to parse generator config overrides: {}", msg)
35            }
36            ConfigError::Validation(msg) => {
37                write!(f, "Configuration validation error: {}", msg)
38            }
39        }
40    }
41}
42
43impl Error for ConfigError {
44    fn source(&self) -> Option<&(dyn Error + 'static)> {
45        match self {
46            ConfigError::FileRead { source, .. } => Some(source),
47            ConfigError::FileParse { source, .. } => Some(source),
48            ConfigError::ParseOverrides(_) => None,
49            ConfigError::Validation(_) => None,
50        }
51    }
52}