use std::fmt;
use std::path::PathBuf;
use crate::error::PackError;
#[derive(Debug)]
pub enum ScriptError {
Parse { line: usize, message: String },
UnknownKeyword {
line: usize,
keyword: String,
context: &'static str,
},
MissingOutput,
NoStructures,
UnclosedBlock(&'static str),
Io { path: PathBuf, message: String },
Pack(PackError),
}
impl fmt::Display for ScriptError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Parse { line, message } => write!(f, "line {line}: {message}"),
Self::UnknownKeyword {
line,
keyword,
context,
} => write!(
f,
"line {line}: unknown keyword `{keyword}` in {context}. \
Silently dropping it risks wrong semantics (e.g. \
`pbc` that was ignored blew the cell grid); if this \
keyword should be accepted, add it to the parser",
),
Self::MissingOutput => write!(f, "missing required `output` keyword"),
Self::NoStructures => write!(f, "no `structure` blocks found in script"),
Self::UnclosedBlock(kind) => write!(f, "unexpected EOF: unclosed `{kind}` block"),
Self::Io { path, message } => write!(f, "{}: {message}", path.display()),
Self::Pack(err) => write!(f, "packing failed: {err}"),
}
}
}
impl std::error::Error for ScriptError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Pack(err) => Some(err),
_ => None,
}
}
}
impl From<PackError> for ScriptError {
fn from(err: PackError) -> Self {
Self::Pack(err)
}
}