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
use std::error;
use std::fmt;

/// The error type for parsers and their combinators
///
/// This crate takes a straightforward approach to handling errors:
///
/// All built-in parsers and combinators return [Error::Mismatch](crate::result::Error::Mismatch)
///
/// The crate consumer may define parsers that return different errors by using the
/// [Custom](crate::result::Error::Custom) variant
#[derive(Debug)]
pub enum Error<E> {
    /// The parser failed to recognized the input
    Mismatch,
    /// The parser failed with a user provided error
    Custom(E),
}

impl<E: error::Error> error::Error for Error<E> {}

impl<E: error::Error> fmt::Display for Error<E> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::Mismatch => write!(f, "parser failed to match"),
            Error::Custom(inner) => fmt::Display::fmt(inner, f),
        }
    }
}

/// The return value of this crate's parsers
pub type Res<In, Out, E> = Result<(Out, In), Error<E>>;