@@ -2,6 +2,7 @@ use jsonpath_lib::select;
22use lazy_static:: lazy_static;
33use regex:: { Regex , RegexBuilder } ;
44use serde_json:: Value ;
5+ use std:: borrow:: Cow ;
56use std:: { env, fmt, fs} ;
67
78mod cache;
@@ -207,15 +208,8 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
207208 let val = cache. get_value ( & command. args [ 0 ] ) ?;
208209 match select ( & val, & command. args [ 1 ] ) {
209210 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 ( ) )
219213 }
220214 Err ( _) => false ,
221215 }
@@ -240,14 +234,8 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
240234 let val = cache. get_value ( & command. args [ 0 ] ) ?;
241235 match select ( & val, & command. args [ 1 ] ) {
242236 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 ( )
251239 }
252240 Err ( _) => false ,
253241 }
@@ -296,3 +284,11 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
296284 Ok ( ( ) )
297285 }
298286}
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