Skip to content

Commit

Permalink
Add custom error with variants to parser module (#50)
Browse files Browse the repository at this point in the history
* Add custom error with variants to `parser` module

* Replace `unwrap_or` with proper Result handling

* Fix clippy warning

Closes #38
  • Loading branch information
CalemRoelofs committed Nov 10, 2023
1 parent 413b811 commit 2cfb303
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 97 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ serde_json = "1.0"
regex = "1"
pest = "2.0"
pest_derive = "2.0"
thiserror = "1.0.50"

[dev-dependencies]
lazy_static = "1.0"
18 changes: 18 additions & 0 deletions src/parser/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use pest::iterators::Pairs;
use thiserror::Error;

use super::parser::Rule;

#[derive(Error, Debug)]
pub enum JsonPathParserError<'a> {
#[error("Failed to parse rule: {0}")]
PestError(#[from] pest::error::Error<Rule>),
#[error("Failed to parse JSON: {0}")]
JsonParsingError(#[from] serde_json::Error),
#[error("{0}")]
ParserError(String),
#[error("Unexpected rule {0:?} when trying to parse logic atom: {1:?}")]
UnexpectedRuleLogicError(Rule, Pairs<'a, Rule>),
#[error("Unexpected `none` when trying to parse logic atom: {0:?}")]
UnexpectedNoneLogicError(Pairs<'a, Rule>),
}
2 changes: 2 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! The parser for the jsonpath.
//! The module grammar denotes the structure of the parsing grammar

pub mod errors;
mod macros;
pub mod model;
#[allow(clippy::module_inception)]
#[allow(clippy::result_large_err)]
pub mod parser;

0 comments on commit 2cfb303

Please sign in to comment.