1use crate::parser::model::{Comparable, Literal, SingularQuery, SingularQuerySegment};
2use crate::query::context::JsonPathEvalContext;
3use crate::query::queryable::Queryable;
4use crate::query::selector::{process_index, process_key};
5use crate::query::state::{Data, State};
6use crate::query::Query;
7
8impl Query for Comparable {
9 fn process_with_context<'a, T: Queryable>(
10 &self,
11 step: State<'a, T>,
12 ctx: &mut JsonPathEvalContext,
13 ) -> State<'a, T> {
14 match self {
15 Comparable::Literal(lit) => lit.process_with_context(step, ctx),
16 Comparable::Function(tf) => tf.process_with_context(step, ctx),
17 Comparable::SingularQuery(query) => query.process_with_context(step, ctx),
18 }
19 }
20}
21
22impl Query for Literal {
23 fn process_with_context<'a, T: Queryable>(
24 &self,
25 state: State<'a, T>,
26 ctx: &mut JsonPathEvalContext,
27 ) -> State<'a, T> {
28 let val = match self {
29 Literal::Int(v) => (*v).into(),
30 Literal::Float(v) => (*v).into(),
31 Literal::String(v) => v.as_str().into(),
32 Literal::Bool(v) => (*v).into(),
33 Literal::Null => T::null(),
34 };
35
36 State::data(state.root, Data::Value(val))
37 }
38}
39
40impl Query for SingularQuery {
41 fn process_with_context<'a, T: Queryable>(
42 &self,
43 step: State<'a, T>,
44 ctx: &mut JsonPathEvalContext,
45 ) -> State<'a, T> {
46 match self {
47 SingularQuery::Current(segments) => segments.process_with_context(step, ctx),
48 SingularQuery::Root(segments) => {
49 segments.process_with_context(step.shift_to_root(), ctx)
50 }
51 }
52 }
53}
54
55impl Query for SingularQuerySegment {
56 fn process_with_context<'a, T: Queryable>(
57 &self,
58 step: State<'a, T>,
59 ctx: &mut JsonPathEvalContext,
60 ) -> State<'a, T> {
61 match self {
62 SingularQuerySegment::Index(idx) => step.flat_map(|d| process_index(d, idx)),
63 SingularQuerySegment::Name(key) => step.flat_map(|d| process_key(d, key)),
64 }
65 }
66}
67
68impl Query for Vec<SingularQuerySegment> {
69 fn process_with_context<'a, T: Queryable>(
70 &self,
71 state: State<'a, T>,
72 ctx: &mut JsonPathEvalContext,
73 ) -> State<'a, T> {
74 self.iter().fold(state, |next, segment| {
75 segment.process_with_context(next, ctx)
76 })
77 }
78}
79
80#[cfg(test)]
81mod tests {
82 use crate::parser::model::{Comparable, Literal, SingularQuery, SingularQuerySegment};
83 use crate::query::state::{Data, Pointer, State};
84 use crate::query::Query;
85 use serde_json::json;
86
87 #[test]
88 fn singular_query() {
89 let value = json!({
90 "result": [
91 {
92 "message": "Hello, Emmy! Your order number is: #100",
93 "phoneNumber": "255-301-9429",
94 "phoneVariation": "+90 398 588 10 73",
95 "status": "active",
96 "name": {
97 "first": "Blaise",
98 "middle": "Kyle",
99 "last": "Fadel"
100 }
101 }
102 ]
103 });
104
105 let query = SingularQuery::Current(vec![
106 SingularQuerySegment::Name("result".to_string()),
107 SingularQuerySegment::Index(0),
108 SingularQuerySegment::Name("name".to_string()),
109 SingularQuerySegment::Name("first".to_string()),
110 ]);
111
112 let state = State::root(&value);
113
114 let result = query.process(state);
115 assert_eq!(
116 result.ok_ref(),
117 Some(vec![Pointer::new(
118 &json!("Blaise"),
119 "$['result'][0]['name']['first']".to_string()
120 )])
121 );
122 }
123
124 #[test]
125 fn singular_query_root() {
126 let value = json!({
127 "result": [
128 {
129 "message": "Hello, Emmy! Your order number is: #100",
130 "phoneNumber": "255-301-9429",
131 "phoneVariation": "+90 398 588 10 73",
132 "status": "active",
133 "name": {
134 "first": "Blaise",
135 "middle": "Kyle",
136 "last": "Fadel"
137 }
138 }
139 ]
140 });
141
142 let query = SingularQuery::Root(vec![
143 SingularQuerySegment::Name("result".to_string()),
144 SingularQuerySegment::Index(0),
145 SingularQuerySegment::Name("name".to_string()),
146 SingularQuerySegment::Name("first".to_string()),
147 ]);
148
149 let state = State::data(
150 &value,
151 Data::new_ref(Pointer::new(&value, "$.name".to_string())),
152 );
153
154 let result = query.process(state);
155 assert_eq!(
156 result.ok_ref(),
157 Some(vec![Pointer::new(
158 &json!("Blaise"),
159 "$['result'][0]['name']['first']".to_string()
160 )])
161 );
162 }
163
164 #[test]
165 fn literal() {
166 let value = json!({
167 "result": [
168 {
169 "message": "Hello, Emmy! Your order number is: #100",
170 "phoneNumber": "255-301-9429",
171 "phoneVariation": "+90 398 588 10 73",
172 "status": "active",
173 "name": {
174 "first": "Blaise",
175 "middle": "Kyle",
176 "last": "Fadel"
177 }
178 }
179 ]
180 });
181
182 let query = Comparable::Literal(Literal::String("Hello".to_string()));
183
184 let state = State::root(&value);
185
186 let result = query.process(state);
187 assert_eq!(result.ok_val(), Some(json!("Hello")));
188 }
189}