use config::ConfigError;
use std::fmt::{Debug, Display, Formatter};
#[derive(Clone)]
#[non_exhaustive]
pub enum AppConfigError {
Frozen,
NotFound(String),
PathParse {
cause_message: String,
},
FileParse {
uri: Option<String>,
cause_message: String,
},
Type {
origin: Option<String>,
unexpected_content: String,
expected: &'static str,
key: Option<String>,
},
At {
error_message: String,
origin: Option<String>,
key: Option<String>,
},
Message(String),
Foreign(String),
Unsupported(String),
}
impl From<ConfigError> for AppConfigError {
fn from(value: ConfigError) -> Self {
match value {
ConfigError::Frozen => Self::Frozen,
ConfigError::NotFound(value) => Self::NotFound(value),
ConfigError::PathParse { cause } => Self::PathParse {
cause_message: cause.to_string(),
},
ConfigError::FileParse { uri, cause } => Self::FileParse {
uri,
cause_message: cause.to_string(),
},
ConfigError::Type {
origin,
unexpected,
expected,
key,
} => Self::Type {
origin,
unexpected_content: unexpected.to_string(),
expected,
key,
},
ConfigError::At { error, origin, key } => Self::At {
error_message: error.to_string(),
origin,
key,
},
ConfigError::Message(value) => Self::Message(value),
ConfigError::Foreign(value) => Self::Foreign(value.to_string()),
_ => Self::Unsupported(value.to_string()),
}
}
}
impl From<&ConfigError> for AppConfigError {
fn from(value: &ConfigError) -> Self {
match value {
ConfigError::Frozen => Self::Frozen,
ConfigError::NotFound(value) => Self::NotFound(value.clone()),
ConfigError::PathParse { cause } => Self::PathParse {
cause_message: cause.to_string(),
},
ConfigError::FileParse { uri, cause } => Self::FileParse {
uri: uri.clone(),
cause_message: cause.to_string(),
},
ConfigError::Type {
origin,
unexpected,
expected,
key,
} => Self::Type {
origin: origin.clone(),
unexpected_content: unexpected.to_string(),
expected,
key: key.clone(),
},
ConfigError::At { error, origin, key } => Self::At {
error_message: error.to_string(),
origin: origin.clone(),
key: key.clone(),
},
ConfigError::Message(value) => Self::Message(value.clone()),
ConfigError::Foreign(value) => Self::Foreign(value.to_string()),
_ => Self::Unsupported(value.to_string()),
}
}
}
impl Debug for AppConfigError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", *self)
}
}
impl Display for AppConfigError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match *self {
Self::Frozen => write!(f, "configuration is frozen"),
Self::PathParse { ref cause_message } => write!(f, "{cause_message}"),
Self::Message(ref value) => write!(f, "{value}"),
Self::Foreign(ref value) => write!(f, "{value}"),
Self::NotFound(ref value) => {
write!(f, "configuration property {value} not found")
}
Self::Type {
ref origin,
ref unexpected_content,
expected,
ref key,
} => {
write!(f, "invalid type: {unexpected_content}, expected {expected}")?;
if let Some(ref key) = *key {
write!(f, " for key `{key}`")?;
}
if let Some(ref origin) = *origin {
write!(f, " in {origin}")?;
}
Ok(())
}
Self::At {
ref error_message,
ref origin,
ref key,
} => {
write!(f, "{error_message}")?;
if let Some(ref key) = *key {
write!(f, " for key `{key}`")?;
}
if let Some(ref origin) = *origin {
write!(f, " in {origin}")?;
}
Ok(())
}
Self::FileParse {
ref uri,
ref cause_message,
} => {
write!(f, "{cause_message}")?;
if let Some(ref uri) = *uri {
write!(f, " in {uri}")?;
}
Ok(())
}
AppConfigError::Unsupported(ref value) => write!(f, "{value}"),
}
}
}