| 1 | use crate::parser::model::{ | 
| 2 | Comparable, Filter, FilterAtom, FnArg, Literal, Segment, Selector, SingularQuery, Test, | 
|---|
| 3 | }; | 
|---|
| 4 |  | 
|---|
| 5 | #[ macro_export] | 
|---|
| 6 | macro_rules! lit { | 
|---|
| 7 | () => { | 
|---|
| 8 | Literal::Null | 
|---|
| 9 | }; | 
|---|
| 10 | (b$b:expr ) => { | 
|---|
| 11 | Literal::Bool($b) | 
|---|
| 12 | }; | 
|---|
| 13 | (s$s:expr) => { | 
|---|
| 14 | Literal::String($s.to_string()) | 
|---|
| 15 | }; | 
|---|
| 16 | (i$n:expr) => { | 
|---|
| 17 | Literal::Int($n) | 
|---|
| 18 | }; | 
|---|
| 19 | (f$n:expr) => { | 
|---|
| 20 | Literal::Float($n) | 
|---|
| 21 | }; | 
|---|
| 22 | } | 
|---|
| 23 |  | 
|---|
| 24 | #[ macro_export] | 
|---|
| 25 | macro_rules! q_segments { | 
|---|
| 26 | ($segment:tt) => { | 
|---|
| 27 | vec![q_segment!($segment)] | 
|---|
| 28 | }; | 
|---|
| 29 | // Recursive case: multiple segments | 
|---|
| 30 | ($segment:tt $($rest:tt)*) => {{ | 
|---|
| 31 | let mut segments = q_segments!($($rest)*); | 
|---|
| 32 | segments.insert(0, q_segment!($segment)); | 
|---|
| 33 | segments | 
|---|
| 34 | }}; | 
|---|
| 35 | } | 
|---|
| 36 |  | 
|---|
| 37 | #[ macro_export] | 
|---|
| 38 | macro_rules! q_segment { | 
|---|
| 39 | ($name:ident) => { | 
|---|
| 40 | SingularQuerySegment::Name(stringify!($name).to_string()) | 
|---|
| 41 | }; | 
|---|
| 42 | ([$name:ident]) => { | 
|---|
| 43 | SingularQuerySegment::Name(format!( "\" {}\" ", stringify!($name))) | 
|---|
| 44 | }; | 
|---|
| 45 | ([$index:expr]) => { | 
|---|
| 46 | SingularQuerySegment::Index($index) | 
|---|
| 47 | }; | 
|---|
| 48 | } | 
|---|
| 49 | #[ macro_export] | 
|---|
| 50 | macro_rules! singular_query { | 
|---|
| 51 | (@ $($segment:tt)*) => { | 
|---|
| 52 | SingularQuery::Current(q_segments!($($segment)*)) | 
|---|
| 53 | }; | 
|---|
| 54 | ($($segment:tt)*) => { | 
|---|
| 55 | SingularQuery::Root(q_segments!($($segment)*)) | 
|---|
| 56 | }; | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 | #[ macro_export] | 
|---|
| 60 | macro_rules! slice { | 
|---|
| 61 | () => { | 
|---|
| 62 | (None, None, None) | 
|---|
| 63 | }; | 
|---|
| 64 | ($start:expr) => { | 
|---|
| 65 | (Some($start), None, None) | 
|---|
| 66 | }; | 
|---|
| 67 | ($start:expr, $end:expr) => { | 
|---|
| 68 | (Some($start), Some($end), None) | 
|---|
| 69 | }; | 
|---|
| 70 | ($start:expr,, $step:expr) => { | 
|---|
| 71 | (Some($start), None, Some($step)) | 
|---|
| 72 | }; | 
|---|
| 73 | (,, $step:expr) => { | 
|---|
| 74 | (None, None, Some($step)) | 
|---|
| 75 | }; | 
|---|
| 76 | (, $end:expr) => { | 
|---|
| 77 | (None, Some($end), None) | 
|---|
| 78 | }; | 
|---|
| 79 | (, $end:expr,$step:expr ) => { | 
|---|
| 80 | (None, Some($end), Some($step)) | 
|---|
| 81 | }; | 
|---|
| 82 | ($start:expr, $end:expr, $step:expr) => { | 
|---|
| 83 | (Some($start), Some($end), Some($step)) | 
|---|
| 84 | }; | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | #[ macro_export] | 
|---|
| 88 | macro_rules! test_fn { | 
|---|
| 89 | ($name:ident $arg:expr) => { | 
|---|
| 90 | TestFunction::try_new(stringify!($name), vec![$arg]).unwrap() | 
|---|
| 91 | }; | 
|---|
| 92 |  | 
|---|
| 93 | ($name:ident $arg1:expr, $arg2:expr ) => { | 
|---|
| 94 | TestFunction::try_new(stringify!($name), vec![$arg1, $arg2]).unwrap() | 
|---|
| 95 | }; | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | #[ macro_export] | 
|---|
| 99 | macro_rules! arg { | 
|---|
| 100 | ($arg:expr) => { | 
|---|
| 101 | FnArg::Literal($arg) | 
|---|
| 102 | }; | 
|---|
| 103 | (t $arg:expr) => { | 
|---|
| 104 | FnArg::Test(Box::new($arg)) | 
|---|
| 105 | }; | 
|---|
| 106 | (f $arg:expr) => { | 
|---|
| 107 | FnArg::Filter($arg) | 
|---|
| 108 | }; | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | #[ macro_export] | 
|---|
| 112 | macro_rules! test { | 
|---|
| 113 | (@ $($segments:expr)*) => { Test::RelQuery(vec![$($segments),*]) }; | 
|---|
| 114 | (S $jq:expr) => { Test::AbsQuery($jq) }; | 
|---|
| 115 | ($tf:expr) => { Test::Function(Box::new($tf)) }; | 
|---|
| 116 |  | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | #[ macro_export] | 
|---|
| 120 | macro_rules! or { | 
|---|
| 121 | ($($items:expr),*) => { | 
|---|
| 122 | crate::parser::model::Filter::Or(vec![ $($items),* ]) | 
|---|
| 123 | }; | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 | #[ macro_export] | 
|---|
| 127 | macro_rules! and { | 
|---|
| 128 | ($($items:expr),*) => { | 
|---|
| 129 | crate::parser::model::Filter::And(vec![ $($items),* ]) | 
|---|
| 130 | }; | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 | #[ macro_export] | 
|---|
| 134 | macro_rules! filter_ { | 
|---|
| 135 | ($item:expr) => { | 
|---|
| 136 | crate::parser::model::Filter::Atom($item) | 
|---|
| 137 | }; | 
|---|
| 138 |  | 
|---|
| 139 | (or $($items:expr),*) => { | 
|---|
| 140 | crate::parser::model::Filter::Or(vec![ $($items),* ]) | 
|---|
| 141 | }; | 
|---|
| 142 |  | 
|---|
| 143 | (and $($items:expr),*) => { | 
|---|
| 144 | crate::parser::model::Filter::And(vec![ $($items),* ]) | 
|---|
| 145 | }; | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | #[ macro_export] | 
|---|
| 149 | macro_rules! atom { | 
|---|
| 150 | (! $filter:expr) => { | 
|---|
| 151 | FilterAtom::filter($filter, true) | 
|---|
| 152 | }; | 
|---|
| 153 | ($filter:expr) => { | 
|---|
| 154 | FilterAtom::filter($filter, false) | 
|---|
| 155 | }; | 
|---|
| 156 | (t! $filter:expr) => { | 
|---|
| 157 | FilterAtom::test($filter, true) | 
|---|
| 158 | }; | 
|---|
| 159 | (t $filter:expr) => { | 
|---|
| 160 | FilterAtom::filter($filter, false) | 
|---|
| 161 | }; | 
|---|
| 162 | ($lhs:expr, $s:expr, $rhs:expr) => { | 
|---|
| 163 | FilterAtom::Comparison(Box::new(cmp!($lhs, $s, $rhs))) | 
|---|
| 164 | }; | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | #[ macro_export] | 
|---|
| 168 | macro_rules! cmp { | 
|---|
| 169 | ($lhs:expr, $op:expr , $rhs:expr) => { | 
|---|
| 170 | Comparison::try_new($op, $lhs, $rhs).unwrap() | 
|---|
| 171 | }; | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 | #[ macro_export] | 
|---|
| 175 | macro_rules! comparable { | 
|---|
| 176 | ($lit:expr) => { | 
|---|
| 177 | Comparable::Literal($lit) | 
|---|
| 178 | }; | 
|---|
| 179 | (f $func:expr) => { | 
|---|
| 180 | Comparable::Function($func) | 
|---|
| 181 | }; | 
|---|
| 182 | (> $sq:expr) => { | 
|---|
| 183 | Comparable::SingularQuery($sq) | 
|---|
| 184 | }; | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | #[ macro_export] | 
|---|
| 188 | macro_rules! selector { | 
|---|
| 189 | (*) => { | 
|---|
| 190 | Selector::Wildcard | 
|---|
| 191 | }; | 
|---|
| 192 | (?$filter:expr) => { | 
|---|
| 193 | Selector::Filter($filter) | 
|---|
| 194 | }; | 
|---|
| 195 | (slice $slice:expr) => { | 
|---|
| 196 | slice_from($slice) | 
|---|
| 197 | }; | 
|---|
| 198 | ($name:ident) => { | 
|---|
| 199 | Selector::Name(stringify!($name).to_string()) | 
|---|
| 200 | }; | 
|---|
| 201 | ([$name:ident]) => { | 
|---|
| 202 | Selector::Name(format!( "\" {}\" ", stringify!($name))) | 
|---|
| 203 | }; | 
|---|
| 204 | ([$index:expr]) => { | 
|---|
| 205 | Selector::Index($index) | 
|---|
| 206 | }; | 
|---|
| 207 | } | 
|---|
| 208 |  | 
|---|
| 209 | #[ macro_export] | 
|---|
| 210 | macro_rules! segment { | 
|---|
| 211 | (..$segment:expr) => { | 
|---|
| 212 | Segment::Descendant(Box::new($segment)) | 
|---|
| 213 | }; | 
|---|
| 214 | ($selector:expr) => { | 
|---|
| 215 | Segment::Selector($selector) | 
|---|
| 216 | }; | 
|---|
| 217 | ($($selectors:expr),*) => { | 
|---|
| 218 | Segment::Selectors(vec![$($selectors),*]) | 
|---|
| 219 | }; | 
|---|
| 220 | } | 
|---|
| 221 |  | 
|---|
| 222 | #[ macro_export] | 
|---|
| 223 | macro_rules! jq { | 
|---|
| 224 | ($($segment:expr),*) => { | 
|---|
| 225 | JpQuery::new(vec![$($segment),*]) | 
|---|
| 226 | }; | 
|---|
| 227 | } | 
|---|
| 228 |  | 
|---|