@@ -217,18 +217,17 @@ impl FileLines {
217
217
}
218
218
219
219
/// Returns JSON representation as accepted by the `--file-lines JSON` arg.
220
- pub fn to_json_spans ( & self ) -> Vec < JsonSpan > {
221
- match & self . 0 {
222
- None => vec ! [ ] ,
223
- Some ( file_ranges) => file_ranges
220
+ pub fn to_json_spans ( & self ) -> Option < Vec < JsonSpan > > {
221
+ self . 0 . as_ref ( ) . map ( |file_ranges| {
222
+ file_ranges
224
223
. iter ( )
225
224
. flat_map ( |( file, ranges) | ranges. iter ( ) . map ( move |r| ( file, r) ) )
226
225
. map ( |( file, range) | JsonSpan {
227
226
file : file. to_owned ( ) ,
228
227
range : ( range. lo , range. hi ) ,
229
228
} )
230
- . collect ( ) ,
231
- }
229
+ . collect ( )
230
+ } )
232
231
}
233
232
234
233
/// Returns `true` if `self` includes all lines in all files. Otherwise runs `f` on all ranges
@@ -302,6 +301,10 @@ impl str::FromStr for FileLines {
302
301
type Err = FileLinesError ;
303
302
304
303
fn from_str ( s : & str ) -> Result < FileLines , Self :: Err > {
304
+ // https://github.com/rust-lang/rustfmt/issues/3649
305
+ if s == "null" {
306
+ return Ok ( FileLines :: all ( ) ) ;
307
+ }
305
308
let v: Vec < JsonSpan > = json:: from_str ( s) . map_err ( FileLinesError :: Json ) ?;
306
309
let mut m = HashMap :: new ( ) ;
307
310
for js in v {
@@ -407,6 +410,7 @@ mod test {
407
410
408
411
use super :: json:: { self , json} ;
409
412
use super :: { FileLines , FileName } ;
413
+ use std:: str:: FromStr ;
410
414
use std:: { collections:: HashMap , path:: PathBuf } ;
411
415
412
416
#[ test]
@@ -426,7 +430,7 @@ mod test {
426
430
. collect ( ) ;
427
431
428
432
let file_lines = FileLines :: from_ranges ( ranges) ;
429
- let mut spans = file_lines. to_json_spans ( ) ;
433
+ let mut spans = file_lines. to_json_spans ( ) . unwrap ( ) ;
430
434
spans. sort ( ) ;
431
435
let json = json:: to_value ( & spans) . unwrap ( ) ;
432
436
assert_eq ! (
@@ -438,4 +442,23 @@ mod test {
438
442
] }
439
443
) ;
440
444
}
445
+
446
+ #[ test]
447
+ fn to_json_spans_with_file_lines_all ( ) {
448
+ assert ! ( FileLines :: all( ) . to_json_spans( ) . is_none( ) ) ;
449
+ }
450
+
451
+ #[ test]
452
+ fn file_lines_from_string_null ( ) {
453
+ assert_eq ! ( FileLines :: all( ) , FileLines :: from_str( "null" ) . unwrap( ) ) ;
454
+ }
455
+
456
+ #[ test]
457
+ fn file_lines_consistent_ser_and_der ( ) {
458
+ let all_spans = FileLines :: all ( ) . to_json_spans ( ) ;
459
+ assert_eq ! (
460
+ FileLines :: all( ) ,
461
+ FileLines :: from_str( & json:: to_string( & all_spans) . unwrap( ) ) . unwrap( ) ,
462
+ ) ;
463
+ }
441
464
}
0 commit comments