Skip to content

Commit

Permalink
feat: Allow bulk-adding of snippets, footers, annotations
Browse files Browse the repository at this point in the history
This will make it easier for diagnostic systems to convert their
messages to a `Message`
  • Loading branch information
epage committed Mar 12, 2024
1 parent 42d8df6 commit 373ab44
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
15 changes: 15 additions & 0 deletions src/snippet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,20 @@ impl<'a> Message<'a> {
self
}

pub fn snippets(mut self, slice: impl IntoIterator<Item = Snippet<'a>>) -> Self {
self.snippets.extend(slice);
self
}

pub fn footer(mut self, footer: Label<'a>) -> Self {
self.footer.push(footer);
self
}

pub fn footers(mut self, footer: impl IntoIterator<Item = Label<'a>>) -> Self {
self.footer.extend(footer);
self
}
}

pub struct Label<'a> {
Expand Down Expand Up @@ -116,6 +126,11 @@ impl<'a> Snippet<'a> {
self
}

pub fn annotations(mut self, annotation: impl IntoIterator<Item = Annotation<'a>>) -> Self {
self.annotations.extend(annotation);
self
}

pub fn fold(mut self, fold: bool) -> Self {
self.fold = fold;
self
Expand Down
14 changes: 3 additions & 11 deletions tests/fixtures/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,8 @@ impl<'a> From<MessageDef<'a>> for Message<'a> {
if let Some(id) = id {
message = message.id(id);
}
message = snippets
.into_iter()
.fold(message, |message, snippet| message.snippet(snippet));
message = footer
.into_iter()
.fold(message, |message, label| message.footer(label));
message = message.snippets(snippets);
message = message.footers(footer);
message
}
}
Expand Down Expand Up @@ -111,11 +107,7 @@ impl<'a> From<SnippetDef<'a>> for Snippet<'a> {
if let Some(origin) = origin {
snippet = snippet.origin(origin)
}
snippet = annotations
.into_iter()
.fold(snippet, |snippet, annotation| {
snippet.annotation(annotation)
});
snippet = snippet.annotations(annotations);
snippet
}
}
Expand Down

0 comments on commit 373ab44

Please sign in to comment.