| 1 | use crate::parser::Rule;
|
| 2 | use crate::query::queryable::Queryable;
|
| 3 | use pest::iterators::Pair;
|
| 4 | use std::num::{ParseFloatError, ParseIntError};
|
| 5 | use std::str::ParseBoolError;
|
| 6 | use thiserror::Error;
|
| 7 |
|
| 8 | /// This error type is used to represent errors that can occur during the parsing of JSONPath expressions.
|
| 9 | #[derive (Error, Debug, PartialEq)]
|
| 10 | pub enum JsonPathError {
|
| 11 | #[error("Failed to parse rule: {0}" )]
|
| 12 | PestError(#[from] Box<pest::error::Error<Rule>>),
|
| 13 | #[error("Unexpected rule `{0:?}` when trying to parse `{1}`" )]
|
| 14 | UnexpectedRuleLogicError(Rule, String),
|
| 15 | #[error("Unexpected `none` when trying to parse logic atom: {0} within {1}" )]
|
| 16 | UnexpectedNoneLogicError(String, String),
|
| 17 | #[error("Pest returned successful parsing but did not produce any output, that should be unreachable due to .pest definition file: SOI ~ chain ~ EOI" )]
|
| 18 | UnexpectedPestOutput,
|
| 19 | #[error("expected a `Rule::path` but found nothing" )]
|
| 20 | NoRulePath,
|
| 21 | #[error("expected a `JsonPath::Descent` but found nothing" )]
|
| 22 | NoJsonPathDescent,
|
| 23 | #[error("expected a `JsonPath::Field` but found nothing" )]
|
| 24 | NoJsonPathField,
|
| 25 | #[error("expected a `f64` or `i64`, but got {0}" )]
|
| 26 | InvalidNumber(String),
|
| 27 | #[error("Invalid toplevel rule for JsonPath: {0:?}" )]
|
| 28 | InvalidTopLevelRule(Rule),
|
| 29 | #[error("Failed to get inner pairs for {0}" )]
|
| 30 | EmptyInner(String),
|
| 31 | #[error("Invalid json path: {0}" )]
|
| 32 | InvalidJsonPath(String),
|
| 33 | }
|
| 34 |
|
| 35 | impl JsonPathError {
|
| 36 | pub fn empty(v: &str) -> Self {
|
| 37 | JsonPathError::EmptyInner(v.to_string())
|
| 38 | }
|
| 39 | }
|
| 40 |
|
| 41 | impl<T: Queryable> From<T> for JsonPathError {
|
| 42 | fn from(val: T) -> Self {
|
| 43 | JsonPathError::InvalidJsonPath(format!("Result ' {:?}' is not a reference" , val))
|
| 44 | }
|
| 45 | }
|
| 46 |
|
| 47 | impl From<&str> for JsonPathError {
|
| 48 | fn from(val: &str) -> Self {
|
| 49 | JsonPathError::EmptyInner(val.to_string())
|
| 50 | }
|
| 51 | }
|
| 52 |
|
| 53 | impl From<(ParseIntError, &str)> for JsonPathError {
|
| 54 | fn from((err: ParseIntError, val: &str): (ParseIntError, &str)) -> Self {
|
| 55 | JsonPathError::InvalidNumber(format!(" {:?} for ` {}`" , err, val))
|
| 56 | }
|
| 57 | }
|
| 58 |
|
| 59 | impl From<(JsonPathError, &str)> for JsonPathError {
|
| 60 | fn from((err: JsonPathError, val: &str): (JsonPathError, &str)) -> Self {
|
| 61 | JsonPathError::InvalidJsonPath(format!(" {:?} for ` {}`" , err, val))
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | impl From<(ParseFloatError, &str)> for JsonPathError {
|
| 66 | fn from((err: ParseFloatError, val: &str): (ParseFloatError, &str)) -> Self {
|
| 67 | JsonPathError::InvalidNumber(format!(" {:?} for ` {}`" , err, val))
|
| 68 | }
|
| 69 | }
|
| 70 | impl From<ParseBoolError> for JsonPathError {
|
| 71 | fn from(err: ParseBoolError) -> Self {
|
| 72 | JsonPathError::InvalidJsonPath(format!(" {:?} " , err))
|
| 73 | }
|
| 74 | }
|
| 75 | impl From<Pair<'_, Rule>> for JsonPathError {
|
| 76 | fn from(rule: Pair<Rule>) -> Self {
|
| 77 | JsonPathError::UnexpectedRuleLogicError(rule.as_rule(), rule.as_str().to_string())
|
| 78 | }
|
| 79 | }
|
| 80 | |