| 1 | use std::fmt; |
| 2 | |
| 3 | #[derive(Debug)] |
| 4 | pub enum JSONPathErrorType { |
| 5 | LexerError, |
| 6 | SyntaxError, |
| 7 | TypeError, |
| 8 | NameError, |
| 9 | } |
| 10 | |
| 11 | #[derive(Debug)] |
| 12 | pub struct JSONPathError { |
| 13 | pub kind: JSONPathErrorType, |
| 14 | pub msg: String, |
| 15 | } |
| 16 | |
| 17 | impl JSONPathError { |
| 18 | pub fn new(error: JSONPathErrorType, msg: String) -> Self { |
| 19 | Self { kind: error, msg } |
| 20 | } |
| 21 | |
| 22 | pub fn syntax(msg: String) -> Self { |
| 23 | Self { |
| 24 | kind: JSONPathErrorType::SyntaxError, |
| 25 | msg, |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | pub fn typ(msg: String) -> Self { |
| 30 | Self { |
| 31 | kind: JSONPathErrorType::TypeError, |
| 32 | msg, |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | pub fn name(msg: String) -> Self { |
| 37 | Self { |
| 38 | kind: JSONPathErrorType::NameError, |
| 39 | msg, |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | impl fmt::Display for JSONPathErrorType { |
| 45 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 46 | match self { |
| 47 | JSONPathErrorType::LexerError => f.write_str("lexer error:"), |
| 48 | JSONPathErrorType::SyntaxError => f.write_str("syntax error:"), |
| 49 | JSONPathErrorType::TypeError => f.write_str("type error:"), |
| 50 | JSONPathErrorType::NameError => f.write_str("name error:"), |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | impl std::error::Error for JSONPathError {} |
| 56 | |
| 57 | impl fmt::Display for JSONPathError { |
| 58 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 59 | write!(f, "{}", self.msg) |
| 60 | } |
| 61 | } |