use std::fmt;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum SettingsErrorKind {
InvalidKey,
DuplicateKey,
UnknownKey,
Missing,
Encoding,
Limit,
DuplicateEnvironment,
Parse,
Validation,
SecretAccess,
SecretFile,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SettingsError {
key: Option<String>,
kind: SettingsErrorKind,
}
impl SettingsError {
pub(super) fn new(key: &str, kind: SettingsErrorKind) -> Self {
Self {
key: super::valid_key(key).then(|| key.to_owned()),
kind,
}
}
pub fn key(&self) -> Option<&str> {
self.key.as_deref()
}
pub fn kind(&self) -> SettingsErrorKind {
self.kind
}
}
impl fmt::Display for SettingsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "application settings {:?}", self.kind)?;
if let Some(key) = &self.key {
write!(f, " for {key}")?;
}
Ok(())
}
}
impl std::error::Error for SettingsError {}