use crate::validation::ValidationErrors;
use std::path::PathBuf;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("config file not found: {0}")]
FileNotFound(PathBuf),
#[error("I/O error reading {path}: {source}")]
Io {
path: PathBuf,
#[source]
source: std::io::Error,
},
#[error("TOML parse error in {path}: {source}")]
Parse {
path: String,
#[source]
source: toml::de::Error,
},
#[error("TOML serialize error: {0}")]
Serialize(#[from] toml::ser::Error),
#[error("validation failed for {context}: {reason}")]
Validation {
context: String,
reason: String,
},
#[error("{0}")]
Invalid(#[from] ValidationErrors),
}
impl Error {
pub fn validation(context: impl Into<String>, reason: impl Into<String>) -> Self {
Self::Validation {
context: context.into(),
reason: reason.into(),
}
}
pub fn io(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
Self::Io {
path: path.into(),
source,
}
}
pub fn parse(path: impl Into<String>, source: toml::de::Error) -> Self {
Self::Parse {
path: path.into(),
source,
}
}
}