Skip to content

Commit c529399

Browse files
authored
71 string (#72)
* bump v * add to string
1 parent 82b6d90 commit c529399

File tree

4 files changed

+141
-17
lines changed

4 files changed

+141
-17
lines changed

CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747
- **`0.6`**
4848
- allow to reuse regex, that improves performance without needing an internal cache
4949

50-
- **`6.1`**
50+
- **`0.6.1`**
5151
- Performance improvements
52-
- Change the contract for the struct of errors
52+
- Change the contract for the struct of errors
53+
- **`0.7.0`**
54+
- Bug fixes and api changes
55+
- **`0.7.1`**
56+
- add Display to JsonPath

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "jsonpath-rust"
33
description = "The library provides the basic functionality to find the set of the data according to the filtering query."
4-
version = "0.6.1"
4+
version = "0.7.1"
55
authors = ["BorisZhguchev <[email protected]>"]
66
edition = "2021"
77
license = "MIT"

src/lib.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -378,16 +378,21 @@ impl<'a, Data> JsonPathValue<'a, Data> {
378378

379379
#[cfg(test)]
380380
mod tests {
381-
// #[test]
382-
// fn no_value_len_field_test() {
383-
// let json: Box<Value> =
384-
// Box::new(json!([{"verb": "TEST","a":[1,2,3]},{"verb": "TEST","a":[1,2,3]},{"verb": "TEST"}, {"verb": "RUN"}]));
385-
// let path: Box<JsonPath> = Box::from(
386-
// JsonPath::from_str("$.[?(@.verb == 'TEST')].a.length()")
387-
// .expect("the path is correct"),
388-
// );
389-
//
390-
// let v = json.find_slice(&path);
391-
// assert_eq!(v, vec![NewValue(json!(3))]);
392-
// }
381+
use crate::JsonPath;
382+
use std::str::FromStr;
383+
384+
#[test]
385+
fn to_string_test() {
386+
let path: Box<JsonPath> = Box::from(
387+
JsonPath::from_str(
388+
"$.['a'].a..book[1:3][*][1]['a','b'][?(@)][?(@.verb == 'TEST')].a.length()",
389+
)
390+
.unwrap(),
391+
);
392+
393+
assert_eq!(
394+
path.to_string(),
395+
"$.'a'.'a'..book[1:3:1][*][1]['a','b'][?(@ exists )][?(@.'verb' == \"TEST\")].'a'.length()"
396+
);
397+
}
393398
}

src/parser/model.rs

Lines changed: 117 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
use super::errors::JsonPathParserError;
12
use super::parse_json_path;
23
use serde_json::Value;
4+
use std::fmt::{Display, Formatter};
35
use std::{convert::TryFrom, str::FromStr};
46

5-
use super::errors::JsonPathParserError;
6-
77
/// The basic structures for parsing json paths.
88
/// The common logic of the structures pursues to correspond the internal parsing structure.
99
///
@@ -32,6 +32,26 @@ pub enum JsonPath {
3232
Fn(Function),
3333
}
3434

35+
impl Display for JsonPath {
36+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
37+
let str = match self {
38+
JsonPath::Root => "$".to_string(),
39+
JsonPath::Field(e) => format!(".'{}'", e),
40+
JsonPath::Chain(elems) => elems.iter().map(ToString::to_string).collect::<String>(),
41+
JsonPath::Descent(e) => {
42+
format!("..{}", e)
43+
}
44+
JsonPath::DescentW => "..*".to_string(),
45+
JsonPath::Index(e) => e.to_string(),
46+
JsonPath::Current(e) => format!("@{}", e),
47+
JsonPath::Wildcard => "[*]".to_string(),
48+
JsonPath::Empty => "".to_string(),
49+
JsonPath::Fn(e) => format!(".{}", e),
50+
};
51+
write!(f, "{}", str)
52+
}
53+
}
54+
3555
impl TryFrom<&str> for JsonPath {
3656
type Error = JsonPathParserError;
3757

@@ -63,6 +83,16 @@ pub enum Function {
6383
/// length()
6484
Length,
6585
}
86+
87+
impl Display for Function {
88+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
89+
let str = match self {
90+
Function::Length => "length()".to_string(),
91+
};
92+
write!(f, "{}", str)
93+
}
94+
}
95+
6696
#[derive(Debug, Clone)]
6797
pub enum JsonPathIndex {
6898
/// A single element in array
@@ -77,6 +107,39 @@ pub enum JsonPathIndex {
77107
Filter(FilterExpression),
78108
}
79109

110+
impl Display for JsonPathIndex {
111+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
112+
let str = match self {
113+
JsonPathIndex::Single(e) => format!("[{}]", e),
114+
JsonPathIndex::UnionIndex(elems) => {
115+
format!(
116+
"[{}]",
117+
elems
118+
.iter()
119+
.map(ToString::to_string)
120+
.collect::<Vec<_>>()
121+
.join(",")
122+
)
123+
}
124+
JsonPathIndex::UnionKeys(elems) => {
125+
format!(
126+
"[{}]",
127+
elems
128+
.iter()
129+
.map(|el| format!("'{}'", el))
130+
.collect::<Vec<_>>()
131+
.join(",")
132+
)
133+
}
134+
JsonPathIndex::Slice(s, e, st) => {
135+
format!("[{}:{}:{}]", s, e, st)
136+
}
137+
JsonPathIndex::Filter(filter) => format!("[?({})]", filter),
138+
};
139+
write!(f, "{}", str)
140+
}
141+
}
142+
80143
#[derive(Debug, Clone, PartialEq)]
81144
pub enum FilterExpression {
82145
/// a single expression like a > 2
@@ -89,6 +152,26 @@ pub enum FilterExpression {
89152
Not(Box<FilterExpression>),
90153
}
91154

155+
impl Display for FilterExpression {
156+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
157+
let str = match self {
158+
FilterExpression::Atom(left, sign, right) => {
159+
format!("{} {} {}", left, sign, right)
160+
}
161+
FilterExpression::And(left, right) => {
162+
format!("{} && {}", left, right)
163+
}
164+
FilterExpression::Or(left, right) => {
165+
format!("{} || {}", left, right)
166+
}
167+
FilterExpression::Not(expr) => {
168+
format!("!{}", expr)
169+
}
170+
};
171+
write!(f, "{}", str)
172+
}
173+
}
174+
92175
impl FilterExpression {
93176
pub fn exists(op: Operand) -> Self {
94177
FilterExpression::Atom(
@@ -106,6 +189,16 @@ pub enum Operand {
106189
Dynamic(Box<JsonPath>),
107190
}
108191

192+
impl Display for Operand {
193+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
194+
let str = match self {
195+
Operand::Static(e) => e.to_string(),
196+
Operand::Dynamic(e) => e.to_string(),
197+
};
198+
write!(f, "{}", str)
199+
}
200+
}
201+
109202
#[allow(dead_code)]
110203
impl Operand {
111204
pub fn val(v: Value) -> Self {
@@ -132,6 +225,28 @@ pub enum FilterSign {
132225
Exists,
133226
}
134227

228+
impl Display for FilterSign {
229+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
230+
let str = match self {
231+
FilterSign::Equal => "==",
232+
FilterSign::Unequal => "!=",
233+
FilterSign::Less => "<",
234+
FilterSign::Greater => ">",
235+
FilterSign::LeOrEq => "<=",
236+
FilterSign::GrOrEq => ">=",
237+
FilterSign::Regex => "~=",
238+
FilterSign::In => "in",
239+
FilterSign::Nin => "nin",
240+
FilterSign::Size => "size",
241+
FilterSign::NoneOf => "noneOf",
242+
FilterSign::AnyOf => "anyOf",
243+
FilterSign::SubSetOf => "subsetOf",
244+
FilterSign::Exists => "exists",
245+
};
246+
write!(f, "{}", str)
247+
}
248+
}
249+
135250
impl FilterSign {
136251
pub fn new(key: &str) -> Self {
137252
match key {

0 commit comments

Comments
 (0)