Skip to content

Commit cc0b060

Browse files
calebcartwrighttopecongiro
authored andcommitted
fix: (de)ser of FileLines between lib and cli opt (#3954)
1 parent a178776 commit cc0b060

File tree

3 files changed

+74
-7
lines changed

3 files changed

+74
-7
lines changed

src/config/file_lines.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

tests/source/file-lines-8.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// rustfmt-file_lines: null
2+
// https://github.com/rust-lang/rustfmt/issues/3649
3+
4+
struct A {
5+
t: i64,
6+
}
7+
8+
mod foo {
9+
fn bar() {
10+
11+
12+
13+
// test
14+
let i = 12;
15+
// test
16+
}
17+
18+
fn baz() {
19+
20+
21+
22+
///
23+
let j = 15;
24+
}
25+
}

tests/target/file-lines-8.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// rustfmt-file_lines: null
2+
// https://github.com/rust-lang/rustfmt/issues/3649
3+
4+
struct A {
5+
t: i64,
6+
}
7+
8+
mod foo {
9+
fn bar() {
10+
// test
11+
let i = 12;
12+
// test
13+
}
14+
15+
fn baz() {
16+
///
17+
let j = 15;
18+
}
19+
}

0 commit comments

Comments
 (0)