From 42f42f4486eef1fdb116052d47988a2d4fcf3963 Mon Sep 17 00:00:00 2001 From: Trevor Hilton Date: Sun, 2 Apr 2023 11:01:20 -0400 Subject: [PATCH 1/3] Rename Error to ParseError The Error type was renamed to ParseError to reflect the fact that it is only used for parsing errors. --- serde_json_path/src/error.rs | 16 ++++++++-------- serde_json_path/src/ext.rs | 2 +- serde_json_path/src/lib.rs | 16 ++++++++-------- serde_json_path/src/parser/mod.rs | 18 +++++++++--------- serde_json_path/src/path.rs | 8 ++++---- serde_json_path_core/src/node.rs | 8 ++++---- 6 files changed, 34 insertions(+), 34 deletions(-) diff --git a/serde_json_path/src/error.rs b/serde_json_path/src/error.rs index 860f4ea..0fe15d3 100644 --- a/serde_json_path/src/error.rs +++ b/serde_json_path/src/error.rs @@ -1,15 +1,15 @@ use std::ops::Deref; -use crate::parser::ParserError; +use crate::parser::Error; /// Error type for the `serde_json_path` crate #[derive(Debug, thiserror::Error)] #[error("{err}")] -pub struct Error { +pub struct ParseError { err: Box, } -impl Error { +impl ParseError { /// Get the 1-indexed error position pub fn position(&self) -> usize { self.err.position @@ -28,11 +28,11 @@ struct ErrorImpl { message: Box, } -impl From<(I, ParserError)> for Error +impl From<(I, Error)> for ParseError where I: Deref + std::fmt::Debug, { - fn from((input, pe): (I, ParserError)) -> Self { + fn from((input, pe): (I, Error)) -> Self { #[cfg(feature = "trace")] tracing::trace!(input = %input.to_string(), parser_error = ?pe); let position = pe.calculate_position(input); @@ -45,19 +45,19 @@ where #[cfg(test)] mod tests { - use crate::Error; + use crate::ParseError; #[cfg(feature = "trace")] use test_log::test; #[test] fn test_send() { fn assert_send() {} - assert_send::(); + assert_send::(); } #[test] fn test_sync() { fn assert_sync() {} - assert_sync::(); + assert_sync::(); } } diff --git a/serde_json_path/src/ext.rs b/serde_json_path/src/ext.rs index 9faf06a..ceb1e1e 100644 --- a/serde_json_path/src/ext.rs +++ b/serde_json_path/src/ext.rs @@ -9,7 +9,7 @@ use crate::{JsonPath, NodeList}; /// use serde_json::json; /// use serde_json_path::{JsonPath, JsonPathExt}; /// -/// # fn main() -> Result<(), serde_json_path::Error> { +/// # fn main() -> Result<(), serde_json_path::ParseError> { /// let value = json!({"foo": ["bar", "baz"]}); /// let query = JsonPath::parse("$.foo[*]")?; /// let nodes = value.json_path(&query).all(); diff --git a/serde_json_path/src/lib.rs b/serde_json_path/src/lib.rs index 9ae92f2..9c1374e 100644 --- a/serde_json_path/src/lib.rs +++ b/serde_json_path/src/lib.rs @@ -29,7 +29,7 @@ //! ```rust //! use serde_json_path::JsonPath; //! -//! # fn main() -> Result<(), serde_json_path::Error> { +//! # fn main() -> Result<(), serde_json_path::ParseError> { //! let path = JsonPath::parse("$.foo.bar")?; //! # Ok(()) //! # } @@ -89,7 +89,7 @@ //! ```rust //! # use serde_json::json; //! # use serde_json_path::JsonPath; -//! # fn main() -> Result<(), serde_json_path::Error> { +//! # fn main() -> Result<(), serde_json_path::ParseError> { //! let value = json!({ "foo": { "bar": ["baz", "bop"] } }); //! let path = JsonPath::parse("$.foo.bar[*]")?; //! let nodes = path.query(&value).all(); @@ -107,7 +107,7 @@ //! ```rust //! # use serde_json::json; //! # use serde_json_path::JsonPath; -//! # fn main() -> Result<(), serde_json_path::Error> { +//! # fn main() -> Result<(), serde_json_path::ParseError> { //! let value = json!({ "foo": ["bar", "baz", "bop"] }); //! let path = JsonPath::parse("$['foo'][1:]")?; //! let nodes = path.query(&value).all(); @@ -126,7 +126,7 @@ //! ```rust //! # use serde_json::json; //! # use serde_json_path::JsonPath; -//! # fn main() -> Result<(), serde_json_path::Error> { +//! # fn main() -> Result<(), serde_json_path::ParseError> { //! let value = json!({ "foo": [1, 2, 3, 4, 5] }); //! let path = JsonPath::parse("$.foo[?@ > 2 && @ < 5]")?; //! let nodes = path.query(&value).all(); @@ -141,7 +141,7 @@ //! ```rust //! # use serde_json::json; //! # use serde_json_path::JsonPath; -//! # fn main() -> Result<(), serde_json_path::Error> { +//! # fn main() -> Result<(), serde_json_path::ParseError> { //! let value = json!([ //! { "title": "Great Expectations", "price": 10 }, //! { "title": "Tale of Two Cities", "price": 8 }, @@ -159,7 +159,7 @@ //! ```rust //! # use serde_json::json; //! # use serde_json_path::JsonPath; -//! # fn main() -> Result<(), serde_json_path::Error> { +//! # fn main() -> Result<(), serde_json_path::ParseError> { //! let value = json!([ //! "a short string", //! "a longer string", @@ -177,7 +177,7 @@ //! ```rust //! # use serde_json::json; //! # use serde_json_path::JsonPath; -//! # fn main() -> Result<(), serde_json_path::Error> { +//! # fn main() -> Result<(), serde_json_path::ParseError> { //! let value = json!({ //! "foo": { //! "bar": { @@ -241,7 +241,7 @@ mod parser; mod path; #[doc(inline)] -pub use error::Error; +pub use error::ParseError; #[doc(inline)] pub use ext::JsonPathExt; #[doc(inline)] diff --git a/serde_json_path/src/parser/mod.rs b/serde_json_path/src/parser/mod.rs index 4418d84..fd5502d 100644 --- a/serde_json_path/src/parser/mod.rs +++ b/serde_json_path/src/parser/mod.rs @@ -16,14 +16,14 @@ pub(crate) mod segment; pub(crate) mod selector; pub(crate) mod utils; -type PResult<'a, O> = IResult<&'a str, O, ParserError<&'a str>>; +type PResult<'a, O> = IResult<&'a str, O, Error<&'a str>>; #[derive(Debug, PartialEq)] -pub(crate) struct ParserError { +pub(crate) struct Error { pub(crate) errors: Vec>, } -impl ParserError +impl Error where I: Deref, { @@ -35,7 +35,7 @@ where } } -impl std::fmt::Display for ParserError { +impl std::fmt::Display for Error { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { if let Some(e) = self.errors.first() { if let Some(ctx) = e.context { @@ -48,7 +48,7 @@ impl std::fmt::Display for ParserError { } } -impl std::error::Error for ParserError {} +impl std::error::Error for Error {} #[derive(Debug, PartialEq)] pub(crate) struct ParserErrorInner { @@ -65,7 +65,7 @@ pub(crate) enum ParserErrorKind { Nom(ErrorKind), } -impl ParseError for ParserError { +impl ParseError for Error { fn from_error_kind(input: I, kind: ErrorKind) -> Self { Self { errors: vec![ParserErrorInner { @@ -86,7 +86,7 @@ impl ParseError for ParserError { } } -impl ContextError for ParserError { +impl ContextError for Error { fn add_context(_input: I, ctx: &'static str, mut other: Self) -> Self { if let Some(e) = other.errors.first_mut() { e.context = Some(ctx); @@ -95,7 +95,7 @@ impl ContextError for ParserError { } } -impl FromExternalError for ParserError +impl FromExternalError for Error where E: std::error::Error + Display, { @@ -114,7 +114,7 @@ pub(crate) trait FromInternalError { fn from_internal_error(input: I, e: E) -> Self; } -impl FromInternalError for ParserError +impl FromInternalError for Error where E: std::error::Error + Display, { diff --git a/serde_json_path/src/path.rs b/serde_json_path/src/path.rs index 3524d0c..c957370 100644 --- a/serde_json_path/src/path.rs +++ b/serde_json_path/src/path.rs @@ -7,7 +7,7 @@ use serde_json_path_core::{ spec::query::{Query, Queryable}, }; -use crate::{parser::parse_query_main, Error}; +use crate::{parser::parse_query_main, ParseError}; /// A parsed JSON Path query string /// @@ -50,7 +50,7 @@ impl JsonPath { /// let path = JsonPath::parse("$.foo[1:10:2].baz").expect("valid JSON Path"); /// # } /// ``` - pub fn parse(path_str: &str) -> Result { + pub fn parse(path_str: &str) -> Result { let (_, path) = parse_query_main(path_str).map_err(|err| match err { nom::Err::Error(e) | nom::Err::Failure(e) => (path_str, e), nom::Err::Incomplete(_) => unreachable!("we do not use streaming parsers"), @@ -64,7 +64,7 @@ impl JsonPath { /// ```rust /// # use serde_json::json; /// # use serde_json_path::JsonPath; - /// # fn main() -> Result<(), serde_json_path::Error> { + /// # fn main() -> Result<(), serde_json_path::ParseError> { /// let path = JsonPath::parse("$.foo[::2]")?; /// let value = json!({"foo": [1, 2, 3, 4]}); /// let nodes = path.query(&value); @@ -78,7 +78,7 @@ impl JsonPath { } impl FromStr for JsonPath { - type Err = Error; + type Err = ParseError; fn from_str(s: &str) -> Result { JsonPath::parse(s) diff --git a/serde_json_path_core/src/node.rs b/serde_json_path_core/src/node.rs index c494ae9..f17f629 100644 --- a/serde_json_path_core/src/node.rs +++ b/serde_json_path_core/src/node.rs @@ -21,7 +21,7 @@ impl<'a> NodeList<'a> { /// # use serde_json::json; /// # use serde_json_path::JsonPath; /// # use serde_json_path::AtMostOneError; - /// # fn main() -> Result<(), serde_json_path::Error> { + /// # fn main() -> Result<(), serde_json_path::ParseError> { /// let value = json!({"foo": ["bar", "baz"]}); /// # { /// let path = JsonPath::parse("$.foo[0]")?; @@ -55,7 +55,7 @@ impl<'a> NodeList<'a> { /// # use serde_json::json; /// # use serde_json_path::JsonPath; /// # use serde_json_path::ExactlyOneError; - /// # fn main() -> Result<(), serde_json_path::Error> { + /// # fn main() -> Result<(), serde_json_path::ParseError> { /// let value = json!({"foo": ["bar", "baz"]}); /// # { /// let path = JsonPath::parse("$.foo[0]")?; @@ -88,7 +88,7 @@ impl<'a> NodeList<'a> { /// ```rust /// # use serde_json::json; /// # use serde_json_path::JsonPath; - /// # fn main() -> Result<(), serde_json_path::Error> { + /// # fn main() -> Result<(), serde_json_path::ParseError> { /// let value = json!({"foo": ["bar", "baz"]}); /// let path = JsonPath::parse("$.foo.*")?; /// let nodes = path.query(&value).all(); @@ -141,7 +141,7 @@ impl<'a> NodeList<'a> { /// ```rust /// # use serde_json::json; /// # use serde_json_path::JsonPath; - /// # fn main() -> Result<(), serde_json_path::Error> { + /// # fn main() -> Result<(), serde_json_path::ParseError> { /// let value = json!({"foo": ["bar", "baz"]}); /// # { /// let path = JsonPath::parse("$.foo[0]")?; From 91e37ca2c9019a5d06aacd994efd7b51aa4446be Mon Sep 17 00:00:00 2001 From: Trevor Hilton Date: Sun, 2 Apr 2023 11:06:16 -0400 Subject: [PATCH 2/3] update changelog --- serde_json_path/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/serde_json_path/CHANGELOG.md b/serde_json_path/CHANGELOG.md index 8fe8352..baa6428 100644 --- a/serde_json_path/CHANGELOG.md +++ b/serde_json_path/CHANGELOG.md @@ -93,9 +93,11 @@ isolate cases where specific errors can be propagated up, and give better error - **added:** updated to latest version of CTS to ensure compliance [#33] - **added:** implement `Eq` for `JsonPath` [#34] +- **breaking:**: Changed the name of `Error` type to `ParseError` [#36] [#33]: https://github.com/hiltontj/serde_json_path/pull/33 [#34]: https://github.com/hiltontj/serde_json_path/pull/34 +[#36]: https://github.com/hiltontj/serde_json_path/pull/36 # 0.5.3 (14 March 2023) From 34c0a18e0189ba9f2cb74c1c75c15e03cc7a694c Mon Sep 17 00:00:00 2001 From: Trevor Hilton Date: Sun, 2 Apr 2023 11:26:04 -0400 Subject: [PATCH 3/3] better doc comment for the ParseError type --- serde_json_path/src/error.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/serde_json_path/src/error.rs b/serde_json_path/src/error.rs index 0fe15d3..2369c61 100644 --- a/serde_json_path/src/error.rs +++ b/serde_json_path/src/error.rs @@ -2,7 +2,7 @@ use std::ops::Deref; use crate::parser::Error; -/// Error type for the `serde_json_path` crate +/// Error type for JSONPath query string parsing errors #[derive(Debug, thiserror::Error)] #[error("{err}")] pub struct ParseError {