pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("syntax error at line {line}, column {column}: {message}")]
Syntax {
line: usize,
column: usize,
offset: usize,
message: String,
},
#[error("entity #{from} references undefined instance #{to}")]
UnresolvedReference {
from: u64,
to: u64,
},
#[error("entity #{0} is defined more than once")]
DuplicateId(u64),
#[error(transparent)]
Io(#[from] std::io::Error),
}
impl Error {
pub(crate) fn syntax(src: &[u8], offset: usize, message: impl Into<String>) -> Self {
let offset = offset.min(src.len());
let before = &src[..offset];
let line_start = before
.iter()
.rposition(|&b| b == b'\n')
.map_or(0, |i| i + 1);
Self::Syntax {
line: before.split(|&b| b == b'\n').count(),
column: offset - line_start + 1,
offset,
message: message.into(),
}
}
}