@@ -17,7 +17,7 @@ fn main() -> Result<(), String> {
17
17
let config = parse_config ( env:: args ( ) . collect ( ) ) ;
18
18
19
19
let mut failed = Vec :: new ( ) ;
20
- let mut cache = Cache :: new ( & config. doc_dir ) ;
20
+ let mut cache = Cache :: new ( & config) ;
21
21
let commands = get_commands ( & config. template )
22
22
. map_err ( |_| format ! ( "Jsondocck failed for {}" , & config. template) ) ?;
23
23
@@ -55,28 +55,23 @@ pub enum CommandKind {
55
55
}
56
56
57
57
impl CommandKind {
58
- fn validate ( & self , args : & [ String ] , command_num : usize , lineno : usize ) -> bool {
58
+ fn validate ( & self , args : & [ String ] , lineno : usize ) -> bool {
59
59
let count = match self {
60
- CommandKind :: Has => ( 1 ..=3 ) . contains ( & args. len ( ) ) ,
61
- CommandKind :: IsMany => args. len ( ) >= 3 ,
62
- CommandKind :: Count | CommandKind :: Is => 3 == args. len ( ) ,
63
- CommandKind :: Set => 4 == args. len ( ) ,
60
+ CommandKind :: Has => ( 1 ..=2 ) . contains ( & args. len ( ) ) ,
61
+ CommandKind :: IsMany => args. len ( ) >= 2 ,
62
+ CommandKind :: Count | CommandKind :: Is => 2 == args. len ( ) ,
63
+ CommandKind :: Set => 3 == args. len ( ) ,
64
64
} ;
65
65
66
66
if !count {
67
67
print_err ( & format ! ( "Incorrect number of arguments to `@{}`" , self ) , lineno) ;
68
68
return false ;
69
69
}
70
70
71
- if args[ 0 ] == "-" && command_num == 0 {
72
- print_err ( & format ! ( "Tried to use the previous path in the first command" ) , lineno) ;
73
- return false ;
74
- }
75
-
76
71
if let CommandKind :: Count = self {
77
- if args[ 2 ] . parse :: < usize > ( ) . is_err ( ) {
72
+ if args[ 1 ] . parse :: < usize > ( ) . is_err ( ) {
78
73
print_err (
79
- & format ! ( "Third argument to @count must be a valid usize (got `{}`)" , args[ 2 ] ) ,
74
+ & format ! ( "Second argument to @count must be a valid usize (got `{}`)" , args[ 2 ] ) ,
80
75
lineno,
81
76
) ;
82
77
return false ;
@@ -181,7 +176,7 @@ fn get_commands(template: &str) -> Result<Vec<Command>, ()> {
181
176
}
182
177
} ;
183
178
184
- if !cmd. validate ( & args, commands . len ( ) , lineno) {
179
+ if !cmd. validate ( & args, lineno) {
185
180
errors = true ;
186
181
continue ;
187
182
}
@@ -199,26 +194,24 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
199
194
let result = match command. kind {
200
195
CommandKind :: Has => {
201
196
match command. args . len ( ) {
202
- // @has <path> = file existence
203
- 1 => cache. get_file ( & command. args [ 0 ] ) . is_ok ( ) ,
204
- // @has <path> <jsonpath> = check path exists
205
- 2 => {
206
- let val = cache. get_value ( & command. args [ 0 ] ) ?;
207
- let results = select ( & val, & command. args [ 1 ] ) . unwrap ( ) ;
197
+ // @has <jsonpath> = check path exists
198
+ 1 => {
199
+ let val = cache. value ( ) ;
200
+ let results = select ( val, & command. args [ 0 ] ) . unwrap ( ) ;
208
201
!results. is_empty ( )
209
202
}
210
- // @has <path> < jsonpath> <value> = check *any* item matched by path equals value
211
- 3 => {
212
- let val = cache. get_value ( & command . args [ 0 ] ) ? ;
213
- let results = select ( & val, & command. args [ 1 ] ) . unwrap ( ) ;
214
- let pat = string_to_value ( & command. args [ 2 ] , cache) ;
203
+ // @has <jsonpath> <value> = check *any* item matched by path equals value
204
+ 2 => {
205
+ let val = cache. value ( ) . clone ( ) ;
206
+ let results = select ( & val, & command. args [ 0 ] ) . unwrap ( ) ;
207
+ let pat = string_to_value ( & command. args [ 1 ] , cache) ;
215
208
let has = results. contains ( & pat. as_ref ( ) ) ;
216
209
// Give better error for when @has check fails
217
210
if !command. negated && !has {
218
211
return Err ( CkError :: FailedCheck (
219
212
format ! (
220
213
"{} matched to {:?} but didn't have {:?}" ,
221
- & command. args[ 1 ] ,
214
+ & command. args[ 0 ] ,
222
215
results,
223
216
pat. as_ref( )
224
217
) ,
@@ -233,13 +226,13 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
233
226
}
234
227
CommandKind :: IsMany => {
235
228
// @ismany <path> <jsonpath> <value>...
236
- let ( path , query, values) = if let [ path , query, values @ ..] = & command. args [ ..] {
237
- ( path , query, values)
229
+ let ( query, values) = if let [ query, values @ ..] = & command. args [ ..] {
230
+ ( query, values)
238
231
} else {
239
232
unreachable ! ( "Checked in CommandKind::validate" )
240
233
} ;
241
- let val = cache. get_value ( path ) ? ;
242
- let got_values = select ( & val, & query) . unwrap ( ) ;
234
+ let val = cache. value ( ) ;
235
+ let got_values = select ( val, & query) . unwrap ( ) ;
243
236
assert ! ( !command. negated, "`@!ismany` is not supported" ) ;
244
237
245
238
// Serde json doesn't implement Ord or Hash for Value, so we must
@@ -270,18 +263,17 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
270
263
true
271
264
}
272
265
CommandKind :: Count => {
273
- // @count <path> <jsonpath> <count> = Check that the jsonpath matches exactly [count] times
274
- assert_eq ! ( command. args. len( ) , 3 ) ;
275
- let expected: usize = command. args [ 2 ] . parse ( ) . unwrap ( ) ;
276
-
277
- let val = cache. get_value ( & command. args [ 0 ] ) ?;
278
- let results = select ( & val, & command. args [ 1 ] ) . unwrap ( ) ;
266
+ // @count <jsonpath> <count> = Check that the jsonpath matches exactly [count] times
267
+ assert_eq ! ( command. args. len( ) , 2 ) ;
268
+ let expected: usize = command. args [ 1 ] . parse ( ) . unwrap ( ) ;
269
+ let val = cache. value ( ) ;
270
+ let results = select ( val, & command. args [ 0 ] ) . unwrap ( ) ;
279
271
let eq = results. len ( ) == expected;
280
272
if !command. negated && !eq {
281
273
return Err ( CkError :: FailedCheck (
282
274
format ! (
283
275
"`{}` matched to `{:?}` with length {}, but expected length {}" ,
284
- & command. args[ 1 ] ,
276
+ & command. args[ 0 ] ,
285
277
results,
286
278
results. len( ) ,
287
279
expected
@@ -293,17 +285,17 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
293
285
}
294
286
}
295
287
CommandKind :: Is => {
296
- // @has <path> < jsonpath> <value> = check *exactly one* item matched by path, and it equals value
297
- assert_eq ! ( command. args. len( ) , 3 ) ;
298
- let val = cache. get_value ( & command . args [ 0 ] ) ? ;
299
- let results = select ( & val, & command. args [ 1 ] ) . unwrap ( ) ;
300
- let pat = string_to_value ( & command. args [ 2 ] , cache) ;
288
+ // @has <jsonpath> <value> = check *exactly one* item matched by path, and it equals value
289
+ assert_eq ! ( command. args. len( ) , 2 ) ;
290
+ let val = cache. value ( ) . clone ( ) ;
291
+ let results = select ( & val, & command. args [ 0 ] ) . unwrap ( ) ;
292
+ let pat = string_to_value ( & command. args [ 1 ] , cache) ;
301
293
let is = results. len ( ) == 1 && results[ 0 ] == pat. as_ref ( ) ;
302
294
if !command. negated && !is {
303
295
return Err ( CkError :: FailedCheck (
304
296
format ! (
305
297
"{} matched to {:?}, but expected {:?}" ,
306
- & command. args[ 1 ] ,
298
+ & command. args[ 0 ] ,
307
299
results,
308
300
pat. as_ref( )
309
301
) ,
@@ -314,16 +306,16 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
314
306
}
315
307
}
316
308
CommandKind :: Set => {
317
- // @set <name> = <path> < jsonpath>
318
- assert_eq ! ( command. args. len( ) , 4 ) ;
309
+ // @set <name> = <jsonpath>
310
+ assert_eq ! ( command. args. len( ) , 3 ) ;
319
311
assert_eq ! ( command. args[ 1 ] , "=" , "Expected an `=`" ) ;
320
- let val = cache. get_value ( & command . args [ 2 ] ) ? ;
321
- let results = select ( & val, & command. args [ 3 ] ) . unwrap ( ) ;
312
+ let val = cache. value ( ) . clone ( ) ;
313
+ let results = select ( & val, & command. args [ 2 ] ) . unwrap ( ) ;
322
314
assert_eq ! (
323
315
results. len( ) ,
324
316
1 ,
325
317
"Expected 1 match for `{}` (because of @set): matched to {:?}" ,
326
- command. args[ 3 ] ,
318
+ command. args[ 2 ] ,
327
319
results
328
320
) ;
329
321
match results. len ( ) {
@@ -336,7 +328,7 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
336
328
_ => {
337
329
panic ! (
338
330
"Got multiple results in `@set` for `{}`: {:?}" ,
339
- & command. args[ 3 ] , results
331
+ & command. args[ 2 ] , results,
340
332
) ;
341
333
}
342
334
}
0 commit comments