use crate::diagnostic::{ErrorKind, Location, Position};
use crate::limits::Limit;
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("unexpected end of input at {at}")]
UnexpectedEof {
at: Position,
},
#[error("syntax error at {at}: {message}")]
Syntax {
at: Position,
message: &'static str,
},
#[error("{} exceeded{}", limit.name(), at.map(|at| format!(" at {at}")).unwrap_or_default())]
LimitExceeded {
limit: Limit,
at: Option<Position>,
},
#[error("type error: expected {expected} for {context}")]
Type {
expected: &'static str,
context: &'static str,
},
#[error("unsupported filter")]
UnsupportedFilter(String),
#[error("corrupt compressed data: {0}")]
Decompress(&'static str),
#[error("invalid {kind}: {message}")]
InvalidStructure {
kind: &'static str,
message: &'static str,
},
}
impl Error {
pub const CODES: &'static [&'static str] = &[
"unexpected-eof",
"syntax",
"limit-exceeded",
"type",
"unsupported-filter",
"decompress",
"invalid-structure",
];
#[must_use]
pub const fn code(&self) -> &'static str {
match self {
Self::UnexpectedEof { .. } => "unexpected-eof",
Self::Syntax { .. } => "syntax",
Self::LimitExceeded { .. } => "limit-exceeded",
Self::Type { .. } => "type",
Self::UnsupportedFilter(_) => "unsupported-filter",
Self::Decompress(_) => "decompress",
Self::InvalidStructure { .. } => "invalid-structure",
}
}
#[must_use]
pub const fn kind(&self) -> ErrorKind {
match self {
Self::UnexpectedEof { .. }
| Self::Syntax { .. }
| Self::Type { .. }
| Self::Decompress(_)
| Self::InvalidStructure { .. } => ErrorKind::Input,
Self::LimitExceeded { .. } => ErrorKind::Limit,
Self::UnsupportedFilter(_) => ErrorKind::Unsupported,
}
}
#[must_use]
pub fn location(&self) -> Location {
Location::from(self.position())
}
#[must_use]
pub const fn position(&self) -> Option<Position> {
match *self {
Self::UnexpectedEof { at } | Self::Syntax { at, .. } => Some(at),
Self::LimitExceeded { at, .. } => at,
Self::Type { .. } | Self::UnsupportedFilter(_) | Self::Decompress(_) | Self::InvalidStructure { .. } => {
None
}
}
}
#[must_use]
pub fn map_position(self, f: impl FnOnce(Position) -> Position) -> Self {
match self {
Self::UnexpectedEof { at } => Self::UnexpectedEof { at: f(at) },
Self::Syntax { at, message } => Self::Syntax { at: f(at), message },
Self::LimitExceeded { limit, at } => Self::LimitExceeded { limit, at: at.map(f) },
other => other,
}
}
pub(crate) fn limit(limit: Limit, at: Option<usize>) -> Self {
Self::LimitExceeded {
limit,
at: at.map(Position::offset),
}
}
}
pub type Result<T> = core::result::Result<T, Error>;
#[cfg(test)]
mod tests {
use super::*;
use crate::test_codes as codes;
fn samples() -> Vec<Error> {
let at = Position::FileOffset(0);
vec![
Error::UnexpectedEof { at },
Error::Syntax { at, message: "m" },
Error::LimitExceeded {
limit: Limit::NestingDepth,
at: None,
},
Error::Type {
expected: "name",
context: "/Filter",
},
Error::UnsupportedFilter(String::new()),
Error::Decompress("m"),
Error::InvalidStructure {
kind: "k",
message: "m",
},
]
}
#[test]
fn codes_lists_the_code_of_every_variant_in_order() {
assert_eq!(codes::distinct(samples().iter().map(Error::code)), Error::CODES);
}
#[test]
fn every_variant_has_the_kind_codes_json_lists() {
codes::assert_kinds_match(samples().iter().map(|e| (e.code(), e.kind().as_str())));
}
}