liberty_parse/
error.rs

1use nom::{
2    error::{convert_error, VerboseError},
3    Err,
4};
5use std::{error, fmt};
6
7#[derive(Debug)]
8pub struct Error<'a>(pub &'a str, pub Err<VerboseError<&'a str>>);
9
10impl<'a> Error<'a> {
11    pub fn new(input: &'a str, err: Err<VerboseError<&'a str>>) -> Self {
12        Error(input, err)
13    }
14}
15
16impl<'a> fmt::Display for Error<'a> {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        match self {
19            Error(input, Err::Error(err)) => write!(f, "{}", convert_error(input, err.clone())),
20            Error(input, Err::Failure(err)) => write!(f, "{}", convert_error(input, err.clone())),
21            Error(_, Err::Incomplete(_)) => write!(f, "Input data is incomplete"),
22        }
23    }
24}
25
26impl<'a> error::Error for Error<'a> {
27    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
28        None
29    }
30}