use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CodegenError {
Parse {
line: usize,
col: usize,
message: String,
},
Validation(String),
Io { path: String, message: String },
StaleTemplates(crate::TemplateDrift),
}
impl CodegenError {
pub fn parse(line: usize, col: usize, message: impl Into<String>) -> Self {
Self::Parse {
line,
col,
message: message.into(),
}
}
pub fn validation(message: impl Into<String>) -> Self {
Self::Validation(message.into())
}
pub fn io(path: impl Into<String>, err: impl fmt::Display) -> Self {
Self::Io {
path: path.into(),
message: err.to_string(),
}
}
pub fn io_path(path: &std::path::Path, err: impl fmt::Display) -> Self {
Self::io(path.display().to_string(), err)
}
}
impl fmt::Display for CodegenError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Parse { line, col, message } => {
write!(f, "spec parse error at {line}:{col}: {message}")
}
Self::Validation(msg) => write!(f, "spec validation error: {msg}"),
Self::Io { path, message } => write!(f, "I/O error for '{path}': {message}"),
Self::StaleTemplates(drift) => drift.fmt(f),
}
}
}
impl From<nativeview_hybrid_spec::SpecError> for CodegenError {
fn from(err: nativeview_hybrid_spec::SpecError) -> Self {
match err {
nativeview_hybrid_spec::SpecError::Parse {
line, col, message, ..
} => Self::Parse { line, col, message },
nativeview_hybrid_spec::SpecError::Validation { message, .. } => Self::Validation(message),
nativeview_hybrid_spec::SpecError::Io { path, message } => Self::Io { path, message },
}
}
}
impl std::error::Error for CodegenError {}