1use std::fmt;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7pub enum ErrorKind {
8 Syntax,
11 Unsupported,
13 UndeclaredPrefix,
15 UnknownType,
17}
18
19#[derive(Debug, Clone, PartialEq, Eq)]
21pub struct Error {
22 pub kind: ErrorKind,
24 pub offset: usize,
26 pub column: usize,
28 pub message: String,
30}
31
32impl Error {
33 pub(crate) fn new(kind: ErrorKind, src: &str, offset: usize, message: String) -> Self {
34 let column = src.get(..offset).map_or(offset, |s| s.chars().count()) + 1;
35 Self {
36 kind,
37 offset,
38 column,
39 message,
40 }
41 }
42}
43
44impl fmt::Display for Error {
45 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46 let kind = match self.kind {
47 ErrorKind::Syntax => "syntax error",
48 ErrorKind::Unsupported => "unsupported",
49 ErrorKind::UndeclaredPrefix => "undeclared namespace prefix",
50 ErrorKind::UnknownType => "unknown type",
51 };
52 write!(f, "{kind} at column {}: {}", self.column, self.message)
53 }
54}
55
56impl std::error::Error for Error {}
57
58pub type Result<T> = std::result::Result<T, Error>;