use thiserror::Error;
pub type Result<T> = std::result::Result<T, ConfigError>;
#[derive(Debug, Error)]
pub enum ConfigError {
#[error("Configuration file error: {0}")]
FileError(#[from] std::io::Error),
#[error("Serialization error: {0}")]
SerializationError(#[from] serde_json::Error),
#[error("YAML parsing error: {0}")]
YamlError(#[from] serde_yaml::Error),
#[error("TOML parsing error: {0}")]
TomlError(#[from] toml::de::Error),
#[error("Configuration key not found: {key}")]
KeyNotFound { key: String },
#[error("Type conversion error: cannot convert {from} to {to}")]
TypeConversion { from: String, to: String },
#[error("Schema validation error: {0}")]
ValidationError(String),
#[error("Network error: {0}")]
NetworkError(#[from] reqwest::Error),
#[error("Authentication error: {0}")]
AuthenticationError(String),
#[error("Source initialization error: {0}")]
SourceInitializationError(String),
#[error("Watcher error: {0}")]
WatcherError(String),
#[error("File system error: {0}")]
FileSystemError(#[from] notify::Error),
#[cfg(feature = "redis-backend")]
#[error("Redis error: {0}")]
RedisError(#[from] redis::RedisError),
#[cfg(feature = "etcd-backend")]
#[error("Etcd error: {0}")]
EtcdError(#[from] etcd_client::Error),
#[error("Lock timeout: failed to acquire lock within {timeout_ms}ms")]
LockTimeout { timeout_ms: u64 },
#[error("Configuration conflict: {0}")]
ConflictError(String),
#[error("Configuration error: {0}")]
Other(String),
}
impl ConfigError {
pub fn validation_error<S: Into<String>>(msg: S) -> Self {
ConfigError::ValidationError(msg.into())
}
pub fn auth_error<S: Into<String>>(msg: S) -> Self {
ConfigError::AuthenticationError(msg.into())
}
pub fn source_init_error<S: Into<String>>(msg: S) -> Self {
ConfigError::SourceInitializationError(msg.into())
}
pub fn watcher_error<S: Into<String>>(msg: S) -> Self {
ConfigError::WatcherError(msg.into())
}
pub fn conflict_error<S: Into<String>>(msg: S) -> Self {
ConfigError::ConflictError(msg.into())
}
pub fn other<S: Into<String>>(msg: S) -> Self {
ConfigError::Other(msg.into())
}
}
impl From<anyhow::Error> for ConfigError {
fn from(err: anyhow::Error) -> Self {
ConfigError::Other(err.to_string())
}
}
impl From<jsonschema::ValidationError<'_>> for ConfigError {
fn from(err: jsonschema::ValidationError<'_>) -> Self {
ConfigError::ValidationError(err.to_string())
}
}