use crate::yaml::YamlError;
use std::fmt;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum DocumentError {
UnterminatedFrontmatter,
FrontmatterNotMapping,
InvalidYaml(YamlError),
MissingKeys(Vec<String>),
}
impl fmt::Display for DocumentError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
DocumentError::UnterminatedFrontmatter => {
write!(f, "Unterminated YAML frontmatter block")
}
DocumentError::FrontmatterNotMapping => {
write!(f, "Frontmatter must be a YAML mapping")
}
DocumentError::InvalidYaml(e) => write!(f, "Invalid YAML in frontmatter: {e}"),
DocumentError::MissingKeys(keys) => {
write!(f, "Missing required frontmatter keys: {}", keys.join(", "))
}
}
}
}
impl std::error::Error for DocumentError {}
impl From<YamlError> for DocumentError {
fn from(e: YamlError) -> Self {
DocumentError::InvalidYaml(e)
}
}
#[derive(Debug)]
pub enum BundleError {
Io(std::io::Error),
NotADirectory(std::path::PathBuf),
Document {
path: std::path::PathBuf,
error: DocumentError,
},
InvalidConceptId {
path: std::path::PathBuf,
reason: String,
},
}
impl fmt::Display for BundleError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
BundleError::Io(e) => write!(f, "I/O error: {e}"),
BundleError::NotADirectory(p) => {
write!(f, "bundle root is not a directory: {}", p.display())
}
BundleError::Document { path, error } => {
write!(f, "{}: {error}", path.display())
}
BundleError::InvalidConceptId { path, reason } => {
write!(f, "{}: invalid concept id ({reason})", path.display())
}
}
}
}
impl std::error::Error for BundleError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
BundleError::Io(e) => Some(e),
BundleError::Document { error, .. } => Some(error),
_ => None,
}
}
}
impl From<std::io::Error> for BundleError {
fn from(e: std::io::Error) -> Self {
BundleError::Io(e)
}
}