@@ -2,6 +2,7 @@ use jsonpath_lib::select;
2
2
use lazy_static:: lazy_static;
3
3
use regex:: { Regex , RegexBuilder } ;
4
4
use serde_json:: Value ;
5
+ use std:: borrow:: Cow ;
5
6
use std:: { env, fmt, fs} ;
6
7
7
8
mod cache;
@@ -207,15 +208,8 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
207
208
let val = cache. get_value ( & command. args [ 0 ] ) ?;
208
209
match select ( & val, & command. args [ 1 ] ) {
209
210
Ok ( results) => {
210
- // FIXME: Share the pat getting code with the `Is` branch.
211
- let v_holder;
212
- let pat: & Value = if command. args [ 2 ] . starts_with ( "$" ) {
213
- & cache. variables [ & command. args [ 2 ] [ 1 ..] ]
214
- } else {
215
- v_holder = serde_json:: from_str ( & command. args [ 2 ] ) . unwrap ( ) ;
216
- & v_holder
217
- } ;
218
- results. contains ( pat)
211
+ let pat = string_to_value ( & command. args [ 2 ] , cache) ;
212
+ results. contains ( & pat. as_ref ( ) )
219
213
}
220
214
Err ( _) => false ,
221
215
}
@@ -240,14 +234,8 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
240
234
let val = cache. get_value ( & command. args [ 0 ] ) ?;
241
235
match select ( & val, & command. args [ 1 ] ) {
242
236
Ok ( results) => {
243
- let v_holder;
244
- let pat: & Value = if command. args [ 2 ] . starts_with ( "$" ) {
245
- & cache. variables [ & command. args [ 2 ] [ 1 ..] ]
246
- } else {
247
- v_holder = serde_json:: from_str ( & command. args [ 2 ] ) . unwrap ( ) ;
248
- & v_holder
249
- } ;
250
- results. len ( ) == 1 && results[ 0 ] == pat
237
+ let pat = string_to_value ( & command. args [ 2 ] , cache) ;
238
+ results. len ( ) == 1 && results[ 0 ] == pat. as_ref ( )
251
239
}
252
240
Err ( _) => false ,
253
241
}
@@ -296,3 +284,11 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
296
284
Ok ( ( ) )
297
285
}
298
286
}
287
+
288
+ fn string_to_value < ' a > ( s : & str , cache : & ' a Cache ) -> Cow < ' a , Value > {
289
+ if s. starts_with ( "$" ) {
290
+ Cow :: Borrowed ( & cache. variables [ & s[ 1 ..] ] )
291
+ } else {
292
+ Cow :: Owned ( serde_json:: from_str ( s) . unwrap ( ) )
293
+ }
294
+ }
0 commit comments