jsonpath_rust/parser/
errors.rs1use crate::parser::Rule;
2use crate::query::queryable::Queryable;
3use pest::iterators::Pair;
4use std::num::{ParseFloatError, ParseIntError};
5use std::str::ParseBoolError;
6use thiserror::Error;
7
8#[derive(Error, Debug, PartialEq, Clone)]
10pub 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 #[error("JSONPath nesting depth exceeds the maximum of {0}")]
34 MaxNestingDepthExceeded(usize),
35}
36
37impl JsonPathError {
38 pub fn empty(v: &str) -> Self {
39 JsonPathError::EmptyInner(v.to_string())
40 }
41}
42
43impl<T: Queryable> From<T> for JsonPathError {
44 fn from(val: T) -> Self {
45 JsonPathError::InvalidJsonPath(format!("Result '{:?}' is not a reference", val))
46 }
47}
48
49impl From<&str> for JsonPathError {
50 fn from(val: &str) -> Self {
51 JsonPathError::EmptyInner(val.to_string())
52 }
53}
54
55impl From<(ParseIntError, &str)> for JsonPathError {
56 fn from((err, val): (ParseIntError, &str)) -> Self {
57 JsonPathError::InvalidNumber(format!("{:?} for `{}`", err, val))
58 }
59}
60
61impl From<(JsonPathError, &str)> for JsonPathError {
62 fn from((err, val): (JsonPathError, &str)) -> Self {
63 JsonPathError::InvalidJsonPath(format!("{:?} for `{}`", err, val))
64 }
65}
66
67impl From<(ParseFloatError, &str)> for JsonPathError {
68 fn from((err, val): (ParseFloatError, &str)) -> Self {
69 JsonPathError::InvalidNumber(format!("{:?} for `{}`", err, val))
70 }
71}
72impl From<ParseBoolError> for JsonPathError {
73 fn from(err: ParseBoolError) -> Self {
74 JsonPathError::InvalidJsonPath(format!("{:?} ", err))
75 }
76}
77impl From<Pair<'_, Rule>> for JsonPathError {
78 fn from(rule: Pair<Rule>) -> Self {
79 JsonPathError::UnexpectedRuleLogicError(rule.as_rule(), rule.as_str().to_string())
80 }
81}