Skip to content

Commit

Permalink
implement with_file and new constructors on Label
Browse files Browse the repository at this point in the history
  • Loading branch information
Johann150 committed Aug 22, 2021
1 parent 973dfc9 commit 95f12bb
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
2 changes: 2 additions & 0 deletions codespan-reporting/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ This is because some testing dependencies now require this Rust version.
```
</details>
- `Label`s can now be created without specifying a file id and instead later setting
the file id on a `Label` or all labels in a `Diagnostic`.
## [0.11.1] - 2021-01-18
Expand Down
65 changes: 64 additions & 1 deletion codespan-reporting/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,58 @@ impl<FileId> Label<FileId> {
Label::new(LabelStyle::Secondary, file_id, range)
}

/// Add a message to the diagnostic.
/// Set the message for the diagnostic. The old message (if any) is discarded.
pub fn with_message(mut self, message: impl ToString) -> Label<FileId> {
self.message = message.to_string();
self
}

/// Set the file id. The old file id (if any) is discarded.
pub fn with_file<NewFileId>(self, file_id: NewFileId) -> Label<NewFileId> {
Label {
style: self.style,
file_id,
range: self.range,
message: self.message,
}
}
}

// use a separate impl so we do not have to specify the type like this in e.g.
// `primary_anon`:
// ```
// Label::<()>::new_anon(..)
// ```
impl Label<()> {
/// Create a new label without specifying a [`file_id`].
///
/// [`file_id`]: Label::file_id
pub fn new_anon(style: LabelStyle, range: impl Into<Range<usize>>) -> Label<()> {
Label {
style,
file_id: (),
range: range.into(),
message: String::new(),
}
}

/// Create a new label with a style of [`LabelStyle::Primary`] and without
/// specifying a [`file_id`].
///
/// [`LabelStyle::Primary`]: LabelStyle::Primary
/// [`file_id`]: Label::file_id
pub fn primary_anon(range: impl Into<Range<usize>>) -> Label<()> {
Label::new_anon(LabelStyle::Primary, range)
}

/// Create a new label with a style of [`LabelStyle::Secondary`] and without
/// specifying a [`file_id`].
///
/// [`LabelStyle::Secondary`]: LabelStyle::Secondary
/// [`file_id`]: Label::file_id
pub fn secondary_anon(range: impl Into<Range<usize>>) -> Label<()> {
Label::new_anon(LabelStyle::Secondary, range)
}
}

/// Represents a diagnostic message that can provide information like errors and
Expand Down Expand Up @@ -188,4 +235,20 @@ impl<FileId> Diagnostic<FileId> {
self.notes.append(&mut notes);
self
}

/// Set the file id for all labels in this Diagnostic by calling
/// [`Label::with_file`] on each label.
pub fn with_file<NewFileId: Clone>(mut self, file_id: NewFileId) -> Diagnostic<NewFileId> {
Diagnostic {
severity: self.severity,
code: self.code,
message: self.message,
labels: self
.labels
.drain(..)
.map(|label| label.with_file(file_id.clone()))
.collect(),
notes: self.notes,
}
}
}

0 comments on commit 95f12bb

Please sign in to comment.