From bd641d67c9d752f0147afacb4f5938cce9d63ce5 Mon Sep 17 00:00:00 2001 From: Boris Zhguchev Date: Wed, 22 Mar 2023 22:59:59 +0100 Subject: [PATCH] make parser public (#39) * make parser public * add v * change methods to tryFrom * correct formatting * add next formatting --- CHANGELOG.md | 2 ++ Cargo.toml | 2 +- src/lib.rs | 9 ++++++--- src/parser/mod.rs | 4 ++-- src/parser/model.rs | 11 +++++++---- 5 files changed, 18 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f82155..d957b95 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,3 +25,5 @@ * add ..* * **`0.2.5`** * build for tags +* **`0.2.6`** + * make parser mod public diff --git a/Cargo.toml b/Cargo.toml index 81bfb20..a78f155 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "jsonpath-rust" description = "The library provides the basic functionality to find the set of the data according to the filtering query." -version = "0.2.5" +version = "0.2.6" authors = ["BorisZhguchev "] edition = "2018" license-file = "LICENSE" diff --git a/src/lib.rs b/src/lib.rs index 0480bba..c6f7220 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -116,11 +116,12 @@ use crate::parser::model::JsonPath; use crate::parser::parser::parse_json_path; use crate::path::{json_path_instance, PathInstance}; use serde_json::Value; +use std::convert::TryInto; use std::fmt::Debug; use std::str::FromStr; -mod parser; -mod path; +pub mod parser; +pub mod path; #[macro_use] extern crate pest_derive; @@ -169,7 +170,9 @@ impl FromStr for JsonPathInst { type Err = String; fn from_str(s: &str) -> Result { - JsonPath::from_str(s).map(|inner| JsonPathInst { inner }) + Ok(JsonPathInst { + inner: s.try_into()?, + }) } } diff --git a/src/parser/mod.rs b/src/parser/mod.rs index b810d30..9077ad6 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -2,6 +2,6 @@ //! The module grammar denotes the structure of the parsing grammar mod macros; -pub(crate) mod model; +pub mod model; #[allow(clippy::module_inception)] -pub(crate) mod parser; +pub mod parser; diff --git a/src/parser/model.rs b/src/parser/model.rs index 2a50c3d..94814d2 100644 --- a/src/parser/model.rs +++ b/src/parser/model.rs @@ -1,5 +1,6 @@ use crate::parse_json_path; use serde_json::Value; +use std::convert::TryFrom; /// The basic structures for parsing json paths. /// The common logic of the structures pursues to correspond the internal parsing structure. @@ -27,12 +28,14 @@ pub enum JsonPath { Fn(Function), } -impl JsonPath { - /// allows to create an JsonPath from string - pub fn from_str(v: &str) -> Result { - parse_json_path(v).map_err(|e| e.to_string()) +impl TryFrom<&str> for JsonPath { + type Error = String; + + fn try_from(value: &str) -> Result { + parse_json_path(value).map_err(|e| e.to_string()) } } + #[derive(Debug, PartialEq, Clone)] pub enum Function { /// length()