1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use crate::*;

#[allow(type_alias_bounds)] // not yet
pub(crate) type Res<T: Scanner> = Result<T::Output, T::Input>;

/// Expected are invariants on the matching state of the combinator
///
/// [Any](crate::Expected::Any) is when *any* state was to be produced
///
/// [End](crate::Expected::End) is when the *end* was expected
///
/// [Token(E)](crate::Expected::Token) is when the Token, `e` was expected
///
/// [Unknown](crate::Expected::Unknown) is when an unknown state was entered (a catch-all)
#[derive(Debug, PartialEq, Clone)]
pub enum Expected<E> {
    Any,
    End,
    Token(E),
    Unknown,
}

/// Positional error produced from the [Expected](crate::Expected) states
#[derive(Debug, PartialEq, Clone)]
pub struct Error<E> {
    pub(crate) pos: usize,
    pub(crate) unexpected: Option<E>,
    pub(crate) expected: Expected<E>,
}

impl<E> Error<E> {
    pub(crate) fn new(pos: usize, unexpected: Option<E>, expected: Expected<E>) -> Self {
        Error {
            pos,
            unexpected,
            expected,
        }
    }
}

impl<E: std::fmt::Debug> std::fmt::Display for Error<E> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "unexpected {:?}. expected {:?} at {}",
            self.unexpected, self.expected, self.pos
        )
    }
}
impl<E: std::fmt::Debug> std::error::Error for Error<E> {}