Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 052d65b

Browse files
committed
Refactor remove DisplayListFormatter in favor of std::fmt::Display
1 parent e7cfe84 commit 052d65b

File tree

17 files changed

+386
-453
lines changed

17 files changed

+386
-453
lines changed

‎benches/simple.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
#[macro_use]
33
extern crate criterion;
44

5-
use criterion::black_box;
6-
use criterion::Criterion;
5+
use criterion::{black_box, Criterion};
76

8-
use annotate_snippets::display_list::DisplayList;
9-
use annotate_snippets::formatter::DisplayListFormatter;
10-
use annotate_snippets::snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation};
7+
use annotate_snippets::{
8+
display_list::{DisplayList, FormatOptions},
9+
snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation},
10+
};
1111

1212
fn create_snippet() {
1313
let snippet = Snippet {
@@ -57,11 +57,14 @@ fn create_snippet() {
5757
annotation_type: AnnotationType::Error,
5858
}),
5959
footer: vec![],
60+
opt: FormatOptions {
61+
color: true,
62+
anonymized_line_numbers: false,
63+
},
6064
};
6165

6266
let dl = DisplayList::from(snippet);
63-
let dlf = DisplayListFormatter::new(true, false);
64-
let _result = dlf.format(&dl);
67+
let _result = dl.to_string();
6568
}
6669

6770
pub fn criterion_benchmark(c: &mut Criterion) {

‎examples/expected_type.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use annotate_snippets::display_list::DisplayList;
2-
use annotate_snippets::formatter::DisplayListFormatter;
3-
use annotate_snippets::snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation};
1+
use annotate_snippets::{
2+
display_list::DisplayList,
3+
snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation},
4+
};
45

56
fn main() {
67
let snippet = Snippet {
@@ -32,9 +33,9 @@ fn main() {
3233
},
3334
],
3435
}],
36+
opt: Default::default(),
3537
};
3638

3739
let dl = DisplayList::from(snippet);
38-
let dlf = DisplayListFormatter::new(true, false);
39-
println!("{}", dlf.format(&dl));
40+
println!("{}", dl);
4041
}

‎examples/footer.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use annotate_snippets::display_list::DisplayList;
2-
use annotate_snippets::formatter::DisplayListFormatter;
3-
use annotate_snippets::snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation};
1+
use annotate_snippets::{
2+
display_list::DisplayList,
3+
snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation},
4+
};
45

56
fn main() {
67
let snippet = Snippet {
@@ -29,9 +30,9 @@ fn main() {
2930
annotation_type: AnnotationType::Error,
3031
}],
3132
}],
33+
opt: Default::default(),
3234
};
3335

3436
let dl = DisplayList::from(snippet);
35-
let dlf = DisplayListFormatter::new(true, false);
36-
println!("{}", dlf.format(&dl));
37+
println!("{}", dl);
3738
}

‎examples/format.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use annotate_snippets::display_list::DisplayList;
2-
use annotate_snippets::formatter::DisplayListFormatter;
3-
use annotate_snippets::snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation};
1+
use annotate_snippets::{
2+
display_list::DisplayList,
3+
snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation},
4+
};
45

56
fn main() {
67
let snippet = Snippet {
@@ -50,9 +51,9 @@ fn main() {
5051
annotation_type: AnnotationType::Error,
5152
}),
5253
footer: vec![],
54+
opt: Default::default(),
5355
};
5456

5557
let dl = DisplayList::from(snippet);
56-
let dlf = DisplayListFormatter::new(true, false);
57-
println!("{}", dlf.format(&dl));
58+
println!("{}", dl);
5859
}

‎examples/multislice.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use annotate_snippets::display_list::DisplayList;
2-
use annotate_snippets::formatter::DisplayListFormatter;
3-
use annotate_snippets::snippet::{Annotation, AnnotationType, Slice, Snippet};
1+
use annotate_snippets::{
2+
display_list::DisplayList,
3+
snippet::{Annotation, AnnotationType, Slice, Snippet},
4+
};
45

56
fn main() {
67
let snippet = Snippet {
@@ -26,9 +27,9 @@ fn main() {
2627
annotations: vec![],
2728
},
2829
],
30+
opt: Default::default(),
2931
};
3032

3133
let dl = DisplayList::from(snippet);
32-
let dlf = DisplayListFormatter::new(true, false);
33-
println!("{}", dlf.format(&dl));
34+
println!("{}", dl);
3435
}

‎src/display_list/from_snippet.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Trait for converting `Snippet` to `DisplayList`.
22
use super::*;
3-
use crate::snippet;
3+
use crate::{formatter::get_term_style,snippet};
44

55
fn format_label(label: Option<&str>, style: Option<DisplayTextStyle>) -> Vec<DisplayTextFragment> {
66
let mut result = vec![];
@@ -388,12 +388,14 @@ fn format_body(slice: snippet::Slice, has_footer: bool) -> Vec<DisplayLine> {
388388
body
389389
}
390390

391+
// TODO: From reference to DisplayList<'a>
391392
impl From<snippet::Snippet> for DisplayList {
392393
fn from(
393394
snippet::Snippet {
394395
title,
395396
footer,
396397
slices,
398+
opt,
397399
}: snippet::Snippet,
398400
) -> Self {
399401
let mut body = vec![];
@@ -409,7 +411,16 @@ impl From<snippet::Snippet> for DisplayList {
409411
body.append(&mut format_annotation(annotation));
410412
}
411413

412-
Self { body }
414+
let FormatOptions {
415+
color,
416+
anonymized_line_numbers,
417+
} = opt;
418+
419+
Self {
420+
body,
421+
stylesheet: get_term_style(color),
422+
anonymized_line_numbers,
423+
}
413424
}
414425
}
415426

‎src/display_list/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
//!
3333
//! The above snippet has been built out of the following structure:
3434
//!
35-
//! ```
35+
//! ```rust,ignore
3636
//! use annotate_snippets::display_list::*;
3737
//!
3838
//! let dl = DisplayList {

‎src/display_list/structs.rs

Lines changed: 33 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,45 @@
1+
use std::fmt;
2+
3+
use crate::formatter::{get_term_style, style::Stylesheet};
4+
15
/// List of lines to be displayed.
2-
#[derive(Debug, Clone, PartialEq)]
36
pub struct DisplayList {
47
pub body: Vec<DisplayLine>,
8+
pub stylesheet: Box<dyn Stylesheet>,
9+
pub anonymized_line_numbers: bool,
510
}
611

712
impl From<Vec<DisplayLine>> for DisplayList {
813
fn from(body: Vec<DisplayLine>) -> Self {
9-
Self { body }
14+
Self {
15+
body,
16+
anonymized_line_numbers: false,
17+
stylesheet: get_term_style(false),
18+
}
19+
}
20+
}
21+
22+
impl PartialEq for DisplayList {
23+
fn eq(&self, other: &Self) -> bool {
24+
self.body == other.body && self.anonymized_line_numbers == other.anonymized_line_numbers
1025
}
1126
}
1227

28+
impl fmt::Debug for DisplayList {
29+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30+
f.debug_struct("DisplayList")
31+
.field("body", &self.body)
32+
.field("anonymized_line_numbers", &self.anonymized_line_numbers)
33+
.finish()
34+
}
35+
}
36+
37+
#[derive(Debug, Default, Copy, Clone)]
38+
pub struct FormatOptions {
39+
pub color: bool,
40+
pub anonymized_line_numbers: bool,
41+
}
42+
1343
/// Inline annotation which can be used in either Raw or Source line.
1444
#[derive(Debug, Clone, PartialEq)]
1545
pub struct Annotation {
@@ -125,63 +155,8 @@ pub struct DisplayMark {
125155
#[derive(Debug, Clone, PartialEq)]
126156
pub enum DisplayMarkType {
127157
/// A mark indicating a multiline annotation going through the current line.
128-
///
129-
/// Example:
130-
/// ```
131-
/// use annotate_snippets::display_list::*;
132-
/// use annotate_snippets::formatter::DisplayListFormatter;
133-
///
134-
/// let dlf = DisplayListFormatter::new(false, false); // Don't use colors
135-
///
136-
/// let dl = DisplayList {
137-
/// body: vec![
138-
/// DisplayLine::Source {
139-
/// lineno: Some(51),
140-
/// inline_marks: vec![
141-
/// DisplayMark {
142-
/// mark_type: DisplayMarkType::AnnotationThrough,
143-
/// annotation_type: DisplayAnnotationType::Error,
144-
/// }
145-
/// ],
146-
/// line: DisplaySourceLine::Content {
147-
/// text: "Example".to_string(),
148-
/// range: (0, 7),
149-
/// }
150-
/// }
151-
/// ]
152-
/// };
153-
/// assert_eq!(dlf.format(&dl), "51 | | Example");
154-
/// ```
155158
AnnotationThrough,
156-
157159
/// A mark indicating a multiline annotation starting on the given line.
158-
///
159-
/// Example:
160-
/// ```
161-
/// use annotate_snippets::display_list::*;
162-
/// use annotate_snippets::formatter::DisplayListFormatter;
163-
///
164-
/// let dlf = DisplayListFormatter::new(false, false); // Don't use colors
165-
///
166-
/// let dl = DisplayList {
167-
/// body: vec![
168-
/// DisplayLine::Source {
169-
/// lineno: Some(51),
170-
/// inline_marks: vec![
171-
/// DisplayMark {
172-
/// mark_type: DisplayMarkType::AnnotationStart,
173-
/// annotation_type: DisplayAnnotationType::Error,
174-
/// }
175-
/// ],
176-
/// line: DisplaySourceLine::Content {
177-
/// text: "Example".to_string(),
178-
/// range: (0, 7),
179-
/// }
180-
/// }
181-
/// ]
182-
/// };
183-
/// assert_eq!(dlf.format(&dl), "51 | / Example");
184-
/// ```
185160
AnnotationStart,
186161
}
187162

@@ -205,49 +180,12 @@ pub enum DisplayAnnotationType {
205180

206181
/// Information whether the header is the initial one or a consequitive one
207182
/// for multi-slice cases.
183+
// TODO: private
208184
#[derive(Debug, Clone, PartialEq)]
209185
pub enum DisplayHeaderType {
210186
/// Initial header is the first header in the snippet.
211-
///
212-
/// Example:
213-
/// ```
214-
/// use annotate_snippets::display_list::*;
215-
/// use annotate_snippets::formatter::DisplayListFormatter;
216-
///
217-
/// let dlf = DisplayListFormatter::new(false, false); // Don't use colors
218-
///
219-
/// let dl = DisplayList {
220-
/// body: vec![
221-
/// DisplayLine::Raw(DisplayRawLine::Origin {
222-
/// path: "file1.rs".to_string(),
223-
/// pos: Some((51, 5)),
224-
/// header_type: DisplayHeaderType::Initial,
225-
/// })
226-
/// ]
227-
/// };
228-
/// assert_eq!(dlf.format(&dl), "--> file1.rs:51:5");
229-
/// ```
230187
Initial,
231188

232189
/// Continuation marks all headers of following slices in the snippet.
233-
///
234-
/// Example:
235-
/// ```
236-
/// use annotate_snippets::display_list::*;
237-
/// use annotate_snippets::formatter::DisplayListFormatter;
238-
///
239-
/// let dlf = DisplayListFormatter::new(false, false); // Don't use colors
240-
///
241-
/// let dl = DisplayList {
242-
/// body: vec![
243-
/// DisplayLine::Raw(DisplayRawLine::Origin {
244-
/// path: "file1.rs".to_string(),
245-
/// pos: Some((51, 5)),
246-
/// header_type: DisplayHeaderType::Continuation,
247-
/// })
248-
/// ]
249-
/// };
250-
/// assert_eq!(dlf.format(&dl), "::: file1.rs:51:5");
251-
/// ```
252190
Continuation,
253191
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /