Skip to content

Commit 2e007da

Browse files
author
Zibi Braniecki
committed
Enable multiple footer annotations
1 parent c10b7c0 commit 2e007da

File tree

7 files changed

+22
-17
lines changed

7 files changed

+22
-17
lines changed

examples/footer.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ fn main() {
1010
id: Some("E0308".to_string()),
1111
annotation_type: AnnotationType::Error,
1212
}),
13-
footer: Some(Annotation {
13+
footer: vec![Annotation {
1414
label: Some(
1515
"expected type: `snippet::Annotation`\n found type: `__&__snippet::Annotation`"
1616
.to_string(),
1717
),
1818
id: None,
1919
annotation_type: AnnotationType::Note,
20-
}),
20+
}],
2121
slices: vec![Slice {
2222
source: " slices: vec![\"A\",".to_string(),
2323
line_start: 13,

examples/format.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn main() {
4949
id: Some("E0308".to_string()),
5050
annotation_type: AnnotationType::Error,
5151
}),
52-
footer: None,
52+
footer: vec![],
5353
};
5454

5555
println!("{}", DisplayList::from(snippet));

examples/multislice.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn main() {
1010
id: None,
1111
annotation_type: AnnotationType::Error,
1212
}),
13-
footer: None,
13+
footer: vec![],
1414
slices: vec![
1515
Slice {
1616
source: "Foo".to_string(),

src/display_list.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,12 @@ impl From<Snippet> for DisplayList {
358358
body.append(&mut format_slice(
359359
&slice,
360360
slice_idx == 0,
361-
snippet.footer.is_some(),
361+
!snippet.footer.is_empty(),
362362
));
363363
slice_idx += 1;
364364
}
365-
if let Some(annotation) = snippet.footer {
365+
366+
for annotation in snippet.footer {
366367
body.push(format_annotation(&annotation));
367368
}
368369

src/format.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,12 @@ impl DisplayListFormatting for Formatter {
9797
.split_first()
9898
{
9999
let indent = prefix.len() + name.len() + 2;
100-
writeln!(
101-
f,
102-
"{}{}{}",
103-
prefix,
104-
name,
105-
format!(": {}", first)
106-
)?;
100+
writeln!(f, "{}{}{}", prefix, name, format!(": {}", first))?;
107101
for line in rest {
108102
writeln!(f, "{}{}", " ".repeat(indent), format!("{}", line))?;
109103
}
110104
}
111105
Ok(())
112-
113106
}
114107
DisplayLine::Origin {
115108
path,

src/snippet.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#[derive(Debug, Clone)]
33
pub struct Snippet {
44
pub title: Option<Annotation>,
5-
pub footer: Option<Annotation>,
5+
pub footer: Vec<Annotation>,
66
pub slices: Vec<Slice>,
77
}
88

tests/snippet/mod.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ pub struct SnippetDef {
1212
#[serde(deserialize_with = "deserialize_annotation")]
1313
#[serde(default)]
1414
pub title: Option<Annotation>,
15-
#[serde(deserialize_with = "deserialize_annotation")]
15+
#[serde(deserialize_with = "deserialize_annotations")]
1616
#[serde(default)]
17-
pub footer: Option<Annotation>,
17+
pub footer: Vec<Annotation>,
1818
#[serde(deserialize_with = "deserialize_slices")]
1919
pub slices: Vec<Slice>,
2020
}
@@ -41,6 +41,17 @@ where
4141
.map(|opt_wrapped: Option<Wrapper>| opt_wrapped.map(|wrapped: Wrapper| wrapped.0))
4242
}
4343

44+
fn deserialize_annotations<'de, D>(deserializer: D) -> Result<Vec<Annotation>, D::Error>
45+
where
46+
D: Deserializer<'de>,
47+
{
48+
#[derive(Deserialize)]
49+
struct Wrapper(#[serde(with = "AnnotationDef")] Annotation);
50+
51+
let v = Vec::deserialize(deserializer)?;
52+
Ok(v.into_iter().map(|Wrapper(a)| a).collect())
53+
}
54+
4455
#[derive(Deserialize)]
4556
#[serde(remote = "Slice")]
4657
pub struct SliceDef {

0 commit comments

Comments
 (0)