use std::error::Error as StdError;
use std::fmt;
use crate::span::{Location, Span};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ErrorKind {
InvalidEncoding,
Scanner,
Parser,
Alias,
DuplicateKey,
Tag,
Resolution,
Limit,
Composition,
Serde,
Emitter,
Io,
}
impl fmt::Display for ErrorKind {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
let name = match self {
Self::InvalidEncoding => "invalid encoding",
Self::Scanner => "scanner error",
Self::Parser => "parser error",
Self::Alias => "alias error",
Self::DuplicateKey => "duplicate key",
Self::Tag => "tag error",
Self::Resolution => "scalar resolution error",
Self::Limit => "resource limit",
Self::Composition => "composition error",
Self::Serde => "serde error",
Self::Emitter => "emitter error",
Self::Io => "I/O error",
};
formatter.write_str(name)
}
}
#[derive(Debug)]
pub struct Error {
kind: ErrorKind,
span: Option<Span>,
message: String,
limit: Option<Box<crate::limits::LimitExceeded>>,
source: Option<Box<dyn StdError + Send + Sync + 'static>>,
}
impl Error {
#[must_use]
pub fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
Self {
kind,
span: None,
message: message.into(),
limit: None,
source: None,
}
}
#[must_use]
pub fn with_span(mut self, span: Span) -> Self {
self.span = Some(span);
self
}
#[must_use]
pub fn with_source(mut self, source: impl StdError + Send + Sync + 'static) -> Self {
self.source = Some(Box::new(source));
self
}
#[must_use]
pub fn with_limit(mut self, limit: crate::limits::LimitExceeded) -> Self {
self.limit = Some(Box::new(limit));
self
}
#[must_use]
pub fn limit(&self) -> Option<crate::limits::LimitExceeded> {
self.limit.as_deref().copied()
}
#[must_use]
pub const fn kind(&self) -> ErrorKind {
self.kind
}
#[must_use]
pub const fn span(&self) -> Option<Span> {
self.span
}
#[must_use]
pub fn location(&self) -> Option<Location> {
self.span.map(|span| span.start().location())
}
#[must_use]
pub fn message(&self) -> &str {
&self.message
}
}
impl fmt::Display for Error {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(&self.message)?;
if let Some(span) = self.span
&& !self.message.contains("at line ")
{
let location = span.start().location();
write!(
formatter,
" at line {} column {}",
location.line(),
location.column()
)?;
}
Ok(())
}
}
impl StdError for Error {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
self.source
.as_deref()
.map(|source| source as &(dyn StdError + 'static))
}
}
impl serde::de::Error for Error {
fn custom<T: fmt::Display>(message: T) -> Self {
Self::new(ErrorKind::Serde, message.to_string())
}
}
impl serde::ser::Error for Error {
fn custom<T: fmt::Display>(message: T) -> Self {
Self::new(ErrorKind::Serde, message.to_string())
}
}
#[cfg(test)]
mod tests {
use super::{Error, ErrorKind};
use crate::span::{Marker, Span};
use std::error::Error as _;
#[test]
fn kind_and_message_are_exposed() {
let error = Error::new(ErrorKind::Parser, "unexpected token");
assert_eq!(error.kind(), ErrorKind::Parser);
assert_eq!(error.message(), "unexpected token");
assert_eq!(error.to_string(), "unexpected token");
assert!(error.span().is_none());
assert!(error.location().is_none());
}
#[test]
fn span_and_location_are_one_based() {
let span = Span::new(Marker::new(4, 2, 3), Marker::new(5, 2, 4));
let error = Error::new(ErrorKind::Scanner, "bad token").with_span(span);
assert_eq!(error.span(), Some(span));
let location = error.location().expect("location");
assert_eq!((location.line(), location.column()), (2, 3));
}
#[test]
fn source_is_chained() {
let inner = std::io::Error::other("inner");
let error = Error::new(ErrorKind::Io, "outer").with_source(inner);
assert!(error.source().is_some());
}
#[test]
fn categories_display_stably() {
assert_eq!(ErrorKind::DuplicateKey.to_string(), "duplicate key");
assert_eq!(ErrorKind::Limit.to_string(), "resource limit");
}
}