use core::fmt;
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum SettingsError {
Parse {
key: String,
value_kind: &'static str,
},
OutOfRange {
key: String,
message: &'static str,
},
}
impl SettingsError {
#[must_use]
pub fn parse(key: impl Into<String>, value_kind: &'static str) -> Self {
Self::Parse {
key: key.into(),
value_kind,
}
}
#[must_use]
pub fn out_of_range(key: impl Into<String>, message: &'static str) -> Self {
Self::OutOfRange {
key: key.into(),
message,
}
}
#[must_use]
pub fn key(&self) -> &str {
match self {
Self::Parse { key, .. } | Self::OutOfRange { key, .. } => key,
}
}
}
impl fmt::Display for SettingsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Parse { key, value_kind } => {
write!(f, "{key} could not be parsed as {value_kind}")
}
Self::OutOfRange { key, message } => {
write!(f, "{key} is out of range: {message}")
}
}
}
}
impl std::error::Error for SettingsError {}