jsonpath_rust/parser/
macros.rs

1#[macro_export]
2macro_rules! filter {
3   () => {FilterExpression::Atom(op!,FilterSign::new(""),op!())};
4   ( $left:expr, $s:literal, $right:expr) => {
5      FilterExpression::Atom($left,FilterSign::new($s),$right)
6   };
7   ( $left:expr,||, $right:expr) => {FilterExpression::Or(Box::new($left),Box::new($right)) };
8   ( $left:expr,&&, $right:expr) => {FilterExpression::And(Box::new($left),Box::new($right)) };
9}
10
11#[macro_export]
12macro_rules! op {
13    ( ) => {
14        Operand::Dynamic(Box::new(JsonPath::Empty))
15    };
16    ( $s:literal) => {
17        Operand::Static(json!($s))
18    };
19    ( s $s:expr) => {
20        Operand::Static(json!($s))
21    };
22    ( $s:expr) => {
23        Operand::Dynamic(Box::new($s))
24    };
25}
26
27#[macro_export]
28macro_rules! idx {
29   ( $s:literal) => {JsonPathIndex::Single(json!($s))};
30   ( idx $($ss:literal),+) => {{
31       let ss_vec = vec![
32           $(
33               json!($ss),
34           )+
35       ];
36       JsonPathIndex::UnionIndex(ss_vec)
37   }};
38   ( $($ss:literal),+) => {{
39       let ss_vec = vec![
40           $(
41               $ss.to_string(),
42           )+
43       ];
44       JsonPathIndex::UnionKeys(ss_vec)
45   }};
46   ( $s:literal) => {JsonPathIndex::Single(json!($s))};
47   ( ? $s:expr) => {JsonPathIndex::Filter($s)};
48   ( [$l:literal;$m:literal;$r:literal]) => {JsonPathIndex::Slice($l,$m,$r)};
49   ( [$l:literal;$m:literal;]) => {JsonPathIndex::Slice($l,$m,1)};
50   ( [$l:literal;;$m:literal]) => {JsonPathIndex::Slice($l,0,$m)};
51   ( [;$l:literal;$m:literal]) => {JsonPathIndex::Slice(0,$l,$m)};
52   ( [;;$m:literal]) => {JsonPathIndex::Slice(0,0,$m)};
53   ( [;$m:literal;]) => {JsonPathIndex::Slice(0,$m,1)};
54   ( [$m:literal;;]) => {JsonPathIndex::Slice($m,0,1)};
55   ( [;;]) => {JsonPathIndex::Slice(0,0,1)};
56}
57
58#[macro_export]
59macro_rules! chain {
60    ($($ss:expr),+) => {{
61        let ss_vec = vec![
62            $(
63                $ss,
64            )+
65        ];
66        JsonPath::Chain(ss_vec)
67   }};
68}
69
70/// Can be used to Parse a JsonPath with a more native way.
71/// e.g.
72/// ```rust
73/// use jsonpath_rust::{path, JsonPath};
74/// use std::str::FromStr;
75/// use serde_json::Value;
76///
77/// let path:JsonPath<Value> = JsonPath::from_str(".abc.*").unwrap();
78/// let path2 = JsonPath::Chain(vec![path!("abc"), path!(*)]);
79/// assert_eq!(path, path2);
80/// ```
81#[macro_export]
82macro_rules! path {
83   ( ) => {JsonPath::Empty};
84   (*) => {JsonPath::Wildcard};
85   ($) => {JsonPath::Root};
86   (@) => {JsonPath::Current(Box::new(JsonPath::Empty))};
87   (@$e:expr) => {JsonPath::Current(Box::new($e))};
88   (@,$($ss:expr),+) => {{
89       let ss_vec = vec![
90           $(
91               $ss,
92           )+
93       ];
94       let chain = JsonPath::Chain(ss_vec);
95       JsonPath::Current(Box::new(chain))
96   }};
97   (..$e:literal) => {JsonPath::Descent($e.to_string())};
98   (..*) => {JsonPath::DescentW};
99   ($e:literal) => {JsonPath::Field($e.to_string())};
100   ($e:expr) => {JsonPath::Index($e)};
101}
102
103#[cfg(test)]
104pub(crate) use chain;
105#[cfg(test)]
106pub(crate) use filter;
107#[cfg(test)]
108pub(crate) use idx;
109#[cfg(test)]
110pub(crate) use op;