ftth_rsip/error/
tokenizer_error.rs

1use std::{error::Error as StdError, fmt};
2
3#[derive(Debug, Eq, PartialEq, Clone)]
4pub struct TokenizerError {
5    pub context: String,
6}
7
8impl<'a, S, T> From<(S, T)> for TokenizerError
9where
10    S: Into<String>,
11    T: Into<&'a bstr::BStr>,
12{
13    fn from(from: (S, T)) -> Self {
14        Self {
15            context: format!("failed to tokenize {}: {}", from.0.into(), from.1.into()),
16        }
17    }
18}
19
20impl From<&'static str> for TokenizerError {
21    fn from(from: &'static str) -> Self {
22        Self {
23            context: from.into(),
24        }
25    }
26}
27
28#[allow(clippy::from_over_into)]
29impl Into<nom::Err<Self>> for TokenizerError {
30    fn into(self) -> nom::Err<Self> {
31        nom::Err::Error(self)
32    }
33}
34
35#[allow(clippy::from_over_into)]
36impl<'a, T> Into<crate::IResult<'a, T>> for TokenizerError {
37    fn into(self) -> crate::IResult<'a, T> {
38        Err(nom::Err::Error(self))
39    }
40}
41
42impl fmt::Display for TokenizerError {
43    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44        write!(f, "Tokenizer error: {}", self.context)
45    }
46}
47
48impl StdError for TokenizerError {}
49
50impl<'a, T: Into<&'a bstr::BStr>> nom::error::ParseError<T> for TokenizerError {
51    fn from_error_kind(input: T, kind: nom::error::ErrorKind) -> Self {
52        Self {
53            context: format!("could not tokenize ({:?}): {}", kind, input.into()),
54        }
55    }
56    fn append(input: T, kind: nom::error::ErrorKind, other: Self) -> Self {
57        Self {
58            context: format!(
59                "{}. Also, could not tokenize ({:?}): {}",
60                other,
61                kind,
62                input.into(),
63            ),
64        }
65    }
66
67    fn from_char(input: T, c: char) -> Self {
68        Self {
69            context: format!("was expecting char {} in: {}", c, input.into()),
70        }
71    }
72
73    fn or(self, other: Self) -> Self {
74        Self {
75            context: format!("tokenizer error: {} or {}", self, other.context),
76        }
77    }
78}