Skip to content

Commit 8c64314

Browse files
author
Zibi Braniecki
committed
Finish formatter linting
1 parent 5f39b53 commit 8c64314

File tree

7 files changed

+30
-33
lines changed

7 files changed

+30
-33
lines changed

examples/expected_type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,5 @@ fn main() {
3939

4040
let dl = DisplayList::from(snippet);
4141
let dlf = DisplayListFormatter::new(true);
42-
println!("{}", dlf.format(dl));
42+
println!("{}", dlf.format(&dl));
4343
}

examples/footer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ fn main() {
3838

3939
let dl = DisplayList::from(snippet);
4040
let dlf = DisplayListFormatter::new(true);
41-
println!("{}", dlf.format(dl));
41+
println!("{}", dlf.format(&dl));
4242
}

examples/format.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ fn main() {
5757

5858
let dl = DisplayList::from(snippet);
5959
let dlf = DisplayListFormatter::new(true);
60-
println!("{}", dlf.format(dl));
60+
println!("{}", dlf.format(&dl));
6161
}

examples/multislice.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ fn main() {
3232

3333
let dl = DisplayList::from(snippet);
3434
let dlf = DisplayListFormatter::new(true);
35-
println!("{}", dlf.format(dl));
35+
println!("{}", dlf.format(&dl));
3636
}

src/formatter/mod.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,16 @@ fn repeat_char(c: char, n: usize) -> String {
4545
/// }
4646
/// ]
4747
/// };
48-
/// assert_eq!(dlf.format(dl), "192 | Example line of text");
48+
/// assert_eq!(dlf.format(&dl), "192 | Example line of text");
4949
/// ```
5050
pub struct DisplayListFormatter {
5151
stylesheet: Box<Stylesheet>,
5252
}
5353

5454
impl DisplayListFormatter {
55+
/// Constructor for the struct. The argument `color` selects
56+
/// the stylesheet depending on the user preferences and `ansi_term`
57+
/// crate availability.
5558
pub fn new(color: bool) -> Self {
5659
if color {
5760
Self {
@@ -67,7 +70,8 @@ impl DisplayListFormatter {
6770
}
6871
}
6972

70-
pub fn format(&self, dl: DisplayList) -> String {
73+
/// Formats a `DisplayList` into a String.
74+
pub fn format(&self, dl: &DisplayList) -> String {
7175
let lineno_width = dl.body.iter().fold(0, |max, line| match line {
7276
DisplayLine::Source {
7377
lineno: Some(lineno),
@@ -166,11 +170,7 @@ impl DisplayListFormatter {
166170
label_part
167171
)
168172
} else if !formatted_type.is_empty() {
169-
format!(
170-
"{}{}",
171-
color.paint(format!("{}", formatted_type)),
172-
label_part
173-
)
173+
format!("{}{}", color.paint(formatted_type), label_part)
174174
} else {
175175
label
176176
}
@@ -290,10 +290,7 @@ impl DisplayListFormatter {
290290
)
291291
}
292292
} else {
293-
format!(
294-
"{}",
295-
self.format_annotation(annotation, *continuation, false)
296-
)
293+
self.format_annotation(annotation, *continuation, false)
297294
}
298295
}
299296
}
@@ -329,7 +326,7 @@ impl DisplayListFormatter {
329326
if !marks.trim().is_empty() {
330327
prefix.push_str(&format!(" {}", marks));
331328
}
332-
format!("{}", prefix)
329+
prefix
333330
}
334331
}
335332
}
@@ -370,7 +367,7 @@ impl DisplayListFormatter {
370367
DisplayAnnotationType::None => StyleClass::None,
371368
};
372369
let color = self.stylesheet.get_style(style);
373-
format!("{}", color.paint(String::from(sigil)))
370+
color.paint(String::from(sigil))
374371
})
375372
.collect::<Vec<String>>()
376373
.join(""),

tests/fixtures.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fn test_fixtures() {
4747

4848
let dl = DisplayList::from(snippet);
4949
let dlf = DisplayListFormatter::new(true);
50-
let actual_out = dlf.format(dl);
50+
let actual_out = dlf.format(&dl);
5151
println!("{}", expected_out);
5252
println!("{}", actual_out.trim_right());
5353

tests/formatter.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn test_source_empty() {
1515

1616
let dlf = DisplayListFormatter::new(false);
1717

18-
assert_eq!(dlf.format(dl), " |");
18+
assert_eq!(dlf.format(&dl), " |");
1919
}
2020

2121
#[test]
@@ -42,7 +42,7 @@ fn test_source_content() {
4242
let dlf = DisplayListFormatter::new(false);
4343

4444
assert_eq!(
45-
dlf.format(dl),
45+
dlf.format(&dl),
4646
"56 | This is an example\n57 | of content lines"
4747
);
4848
}
@@ -73,7 +73,7 @@ fn test_source_annotation_standalone_singleline() {
7373

7474
let dlf = DisplayListFormatter::new(false);
7575

76-
assert_eq!(dlf.format(dl), " | ^^^^^ Example string");
76+
assert_eq!(dlf.format(&dl), " | ^^^^^ Example string");
7777
}
7878

7979
#[test]
@@ -122,7 +122,7 @@ fn test_source_annotation_standalone_multiline() {
122122
let dlf = DisplayListFormatter::new(false);
123123

124124
assert_eq!(
125-
dlf.format(dl),
125+
dlf.format(&dl),
126126
" | ----- help: Example string\n | Second line"
127127
);
128128
}
@@ -267,7 +267,7 @@ fn test_source_annotation_standalone_multi_annotation() {
267267

268268
let dlf = DisplayListFormatter::new(false);
269269

270-
assert_eq!(dlf.format(dl), " | ----- info: Example string\n | Second line\n | warning: This is a note\n | Second line of the warning\n | ----- info: This is an info\n | ----- help: This is help\n | This is an annotation of type none");
270+
assert_eq!(dlf.format(&dl), " | ----- info: Example string\n | Second line\n | warning: This is a note\n | Second line of the warning\n | ----- info: This is an info\n | ----- help: This is help\n | This is an annotation of type none");
271271
}
272272

273273
#[test]
@@ -297,7 +297,7 @@ fn test_fold_line() {
297297
let dlf = DisplayListFormatter::new(false);
298298

299299
assert_eq!(
300-
dlf.format(dl),
300+
dlf.format(&dl),
301301
" 5 | This is line 5\n...\n10021 | ... and now we're at line 10021"
302302
);
303303
}
@@ -314,7 +314,7 @@ fn test_raw_origin_initial_nopos() {
314314

315315
let dlf = DisplayListFormatter::new(false);
316316

317-
assert_eq!(dlf.format(dl), "--> src/test.rs");
317+
assert_eq!(dlf.format(&dl), "--> src/test.rs");
318318
}
319319

320320
#[test]
@@ -329,7 +329,7 @@ fn test_raw_origin_initial_pos() {
329329

330330
let dlf = DisplayListFormatter::new(false);
331331

332-
assert_eq!(dlf.format(dl), "--> src/test.rs:23:15");
332+
assert_eq!(dlf.format(&dl), "--> src/test.rs:23:15");
333333
}
334334

335335
#[test]
@@ -344,7 +344,7 @@ fn test_raw_origin_continuation() {
344344

345345
let dlf = DisplayListFormatter::new(false);
346346

347-
assert_eq!(dlf.format(dl), "::: src/test.rs:23:15");
347+
assert_eq!(dlf.format(&dl), "::: src/test.rs:23:15");
348348
}
349349

350350
#[test]
@@ -368,7 +368,7 @@ fn test_raw_annotation_unaligned() {
368368

369369
let dlf = DisplayListFormatter::new(false);
370370

371-
assert_eq!(dlf.format(dl), "error[E0001]: This is an error");
371+
assert_eq!(dlf.format(&dl), "error[E0001]: This is an error");
372372
}
373373

374374
#[test]
@@ -407,7 +407,7 @@ fn test_raw_annotation_unaligned_multiline() {
407407
let dlf = DisplayListFormatter::new(false);
408408

409409
assert_eq!(
410-
dlf.format(dl),
410+
dlf.format(&dl),
411411
"warning[E0001]: This is an error\n Second line of the error"
412412
);
413413
}
@@ -433,7 +433,7 @@ fn test_raw_annotation_aligned() {
433433

434434
let dlf = DisplayListFormatter::new(false);
435435

436-
assert_eq!(dlf.format(dl), " = error[E0001]: This is an error");
436+
assert_eq!(dlf.format(&dl), " = error[E0001]: This is an error");
437437
}
438438

439439
#[test]
@@ -472,7 +472,7 @@ fn test_raw_annotation_aligned_multiline() {
472472
let dlf = DisplayListFormatter::new(false);
473473

474474
assert_eq!(
475-
dlf.format(dl),
475+
dlf.format(&dl),
476476
" = warning[E0001]: This is an error\n Second line of the error"
477477
);
478478
}
@@ -527,7 +527,7 @@ fn test_different_annotation_types() {
527527
let dlf = DisplayListFormatter::new(false);
528528

529529
assert_eq!(
530-
dlf.format(dl),
530+
dlf.format(&dl),
531531
"note: This is a note\nThis is just a string\n Second line of none type annotation",
532532
);
533533
}
@@ -549,5 +549,5 @@ fn test_inline_marks_empty_line() {
549549

550550
let dlf = DisplayListFormatter::new(false);
551551

552-
assert_eq!(dlf.format(dl), " | |",);
552+
assert_eq!(dlf.format(&dl), " | |",);
553553
}

0 commit comments

Comments
 (0)