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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#![allow(renamed_and_removed_lints)]
#![allow(clippy::unknown_clippy_lints)]
use thiserror::Error as ThisError;
use std::fmt;
use crate::text::parse::PestError;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(ThisError, Clone, Debug, PartialEq)]
pub enum Error {
#[error("Failed parsing input Error: {0}")]
ParseError(#[from] PestError),
#[error("Invalid token stream Context: {0}")]
InvalidTokenStream(TokenContext),
}
impl From<TokenContext> for Error {
fn from(context: TokenContext) -> Self {
Self::InvalidTokenStream(context)
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum TokenContext {
EofWhileParsingKey,
EofWhileParsingVal,
EofWhileParsingSeq,
EofWhileParsingObj,
ExpectedSomeVal,
ExpectedNonSeqVal,
TrailingTokens,
}
impl fmt::Display for TokenContext {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let message = match self {
Self::EofWhileParsingKey => "Token stream ended when needed key",
Self::EofWhileParsingVal => "Token stream ended when needed value",
Self::EofWhileParsingSeq => "Token stream ended when parsing sequence",
Self::EofWhileParsingObj => "Token stream ended when parsing object",
Self::ExpectedSomeVal => "Found invalid token when expecting value",
Self::ExpectedNonSeqVal => "Found invalid token when expecing non sequence value",
Self::TrailingTokens => "Trailing tokens after finishing conversion",
};
f.write_str(message)
}
}