pub trait Error<'a, I: Input<'a>>: Sized + LabelError<'a, I, DefaultExpected<'a, I::Token>> {
// Provided method
fn merge(self, other: Self) -> Self { ... }
}Expand description
A trait that describes parser error types.
If you have a custom error type in your compiler, or your needs are not sufficiently met by Simple, you should
implement this trait. If your error type has ‘extra’ features that allow for more specific error messages, you can
use the Parser::map_err or Parser::try_map functions to take advantage of these inline within your parser.
§Examples
use chumsky::{prelude::*, error::{Error, LabelError}, util::MaybeRef, DefaultExpected};
type Span = SimpleSpan<usize>;
// A custom error type
#[derive(Debug, PartialEq)]
enum MyError {
ExpectedFound {
span: Span,
expected: Vec<DefaultExpected<'static, char>>,
found: Option<char>,
},
NotADigit(Span, char),
}
impl<'a> Error<'a, &'a str> for MyError {
fn merge(mut self, mut other: Self) -> Self {
if let (Self::ExpectedFound { expected, .. }, Self::ExpectedFound { expected: expected_other, .. }) = (
&mut self,
&mut other,
) {
expected.append(expected_other);
}
self
}
}
impl<'a> LabelError<'a, &'a str, DefaultExpected<'a, char>> for MyError {
fn expected_found<Iter: IntoIterator<Item = DefaultExpected<'a, char>>>(
expected: Iter,
found: Option<MaybeRef<'a, char>>,
span: Span,
) -> Self {
Self::ExpectedFound {
span,
expected: expected
.into_iter()
.map(|e| e.into_owned())
.collect(),
found: found.as_deref().copied(),
}
}
}
let numeral = any::<_, extra::Err<MyError>>().try_map(|c: char, span| match c.to_digit(10) {
Some(x) => Ok(x),
None => Err(MyError::NotADigit(span, c)),
});
assert_eq!(numeral.parse("3").into_result(), Ok(3));
assert_eq!(numeral.parse("7").into_result(), Ok(7));
assert_eq!(numeral.parse("f").into_errors(), vec![MyError::NotADigit((0..1).into(), 'f')]);Provided Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.