Skip to content
Clone
dd4650a
55 lines 1.7 KB Rust
use std::fmt;
use std::path::PathBuf;
#[derive(Debug)]
pub enum ConfigError {
MissingConfig { candidates: Vec<PathBuf> },
Missing { path: PathBuf },
Ambiguous { candidates: Vec<PathBuf> },
Section { key: String, path: PathBuf },
Load { path: PathBuf, message: String },
Validation(String),
}
impl ConfigError {
pub fn validation(message: impl Into<String>) -> Self {
Self::Validation(message.into())
}
}
impl fmt::Display for ConfigError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::MissingConfig { candidates } => write!(
formatter,
"no config file found, expected one of {} (or set KENOAUTH_CONFIG)",
list(candidates)
),
Self::Missing { path } => {
write!(formatter, "config file {} does not exist", path.display())
}
Self::Ambiguous { candidates } => write!(
formatter,
"several config files found, keep exactly one of {}",
list(candidates)
),
Self::Section { key, path } => {
write!(formatter, "{} has no `{key}` section", path.display())
}
Self::Load { path, message } => {
write!(formatter, "failed to load {}: {message}", path.display())
}
Self::Validation(message) => write!(formatter, "invalid config: {message}"),
}
}
}
impl std::error::Error for ConfigError {}
fn list(paths: &[PathBuf]) -> String {
paths
.iter()
.map(|path| path.display().to_string())
.collect::<Vec<_>>()
.join(", ")
}