@@ -217,18 +217,17 @@ impl FileLines {
217217 }
218218
219219 /// 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
224223 . iter ( )
225224 . flat_map ( |( file, ranges) | ranges. iter ( ) . map ( move |r| ( file, r) ) )
226225 . map ( |( file, range) | JsonSpan {
227226 file : file. to_owned ( ) ,
228227 range : ( range. lo , range. hi ) ,
229228 } )
230- . collect ( ) ,
231- }
229+ . collect ( )
230+ } )
232231 }
233232
234233 /// 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 {
302301 type Err = FileLinesError ;
303302
304303 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+ }
305308 let v: Vec < JsonSpan > = json:: from_str ( s) . map_err ( FileLinesError :: Json ) ?;
306309 let mut m = HashMap :: new ( ) ;
307310 for js in v {
@@ -407,6 +410,7 @@ mod test {
407410
408411 use super :: json:: { self , json} ;
409412 use super :: { FileLines , FileName } ;
413+ use std:: str:: FromStr ;
410414 use std:: { collections:: HashMap , path:: PathBuf } ;
411415
412416 #[ test]
@@ -426,7 +430,7 @@ mod test {
426430 . collect ( ) ;
427431
428432 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 ( ) ;
430434 spans. sort ( ) ;
431435 let json = json:: to_value ( & spans) . unwrap ( ) ;
432436 assert_eq ! (
@@ -438,4 +442,23 @@ mod test {
438442 ] }
439443 ) ;
440444 }
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+ }
441464}
0 commit comments