Skip to content

Commit 474293e

Browse files
authored
fix #92 (#93)
* fix #92 * fix linter
1 parent f97c0f3 commit 474293e

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

src/parser.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub(super) struct JSPathParser;
1919
const MAX_VAL: i64 = 9007199254740991; // Maximum safe integer value in JavaScript
2020
const MIN_VAL: i64 = -9007199254740991; // Minimum safe integer value in JavaScript
2121

22-
pub(super) type Parsed<T> = Result<T, JsonPathError>;
22+
pub type Parsed<T> = Result<T, JsonPathError>;
2323

2424
/// Parses a string into a [JsonPath].
2525
///

src/parser/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::str::ParseBoolError;
66
use thiserror::Error;
77

88
/// This error type is used to represent errors that can occur during the parsing of JSONPath expressions.
9-
#[derive(Error, Debug, PartialEq)]
9+
#[derive(Error, Debug, PartialEq, Clone)]
1010
pub enum JsonPathError {
1111
#[error("Failed to parse rule: {0}")]
1212
PestError(#[from] Box<pest::error::Error<Rule>>),

src/query.rs

+37-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ mod test;
1111
mod test_function;
1212

1313
use crate::parser::errors::JsonPathError;
14-
use crate::parser::parse_json_path;
14+
use crate::parser::model::JpQuery;
15+
use crate::parser::{parse_json_path, Parsed};
1516
use crate::query::queryable::Queryable;
1617
use crate::query::state::{Data, Pointer};
1718
use state::State;
@@ -62,7 +63,16 @@ impl<'a, T: Queryable> From<Pointer<'a, T>> for QueryRef<'a, T> {
6263
/// The main function to process a JSONPath query.
6364
/// It takes a path and a value, and returns a vector of `QueryResult` thus values + paths.
6465
pub fn js_path<'a, T: Queryable>(path: &str, value: &'a T) -> Queried<Vec<QueryRef<'a, T>>> {
65-
match parse_json_path(path)?.process(State::root(value)).data {
66+
js_path_process(&parse_json_path(path)?, value)
67+
}
68+
69+
/// A convenience function to process a JSONPath query
70+
/// and return a vector of values, omitting the path.
71+
pub fn js_path_process<'a, 'b, T: Queryable>(
72+
path: &'b JpQuery,
73+
value: &'a T,
74+
) -> Queried<Vec<QueryRef<'a, T>>> {
75+
match path.process(State::root(value)).data {
6676
Data::Ref(p) => Ok(vec![p.into()]),
6777
Data::Refs(refs) => Ok(refs.into_iter().map(Into::into).collect()),
6878
Data::Value(v) => Err(v.into()),
@@ -89,9 +99,9 @@ pub fn js_path_path<T: Queryable>(path: &str, value: &T) -> Queried<Vec<QueryPat
8999
#[cfg(test)]
90100
mod tests {
91101
use crate::parser::errors::JsonPathError;
92-
use crate::parser::Parsed;
102+
use crate::parser::{parse_json_path, Parsed};
93103
use crate::query::queryable::Queryable;
94-
use crate::query::{js_path, Queried, QueryRef};
104+
use crate::query::{js_path, js_path_process, Queried, QueryRef};
95105
use crate::JsonPath;
96106
use serde_json::{json, Value};
97107

@@ -807,4 +817,27 @@ mod tests {
807817

808818
Ok(())
809819
}
820+
821+
#[test]
822+
fn prepared_query() -> Queried<()> {
823+
let json1 = json!({
824+
"a": 1,
825+
"b": 2,
826+
"c": 3
827+
});
828+
let json2 = json!({
829+
"a": 1,
830+
"b": 2,
831+
"c": 3
832+
});
833+
834+
let jq = parse_json_path("$[?@<3]")?;
835+
836+
let v1 = js_path_process(&jq, &json1)?;
837+
let v2 = js_path_process(&jq, &json2)?;
838+
839+
assert_eq!(v1, v2);
840+
841+
Ok(())
842+
}
810843
}

0 commit comments

Comments
 (0)