Skip to content

Commit 7650ab4

Browse files
committed
implement with_file and new constructors on Label
1 parent 8d1762e commit 7650ab4

2 files changed

Lines changed: 66 additions & 1 deletion

File tree

codespan-reporting/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6060
```
6161
6262
</details>
63+
- `Label`s can now be created without specifying a file id and instead later setting
64+
the file id on a `Label` or all labels in a `Diagnostic`.
6365
6466
## [0.11.1] - 2021-01-18
6567

codespan-reporting/src/diagnostic.rs

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,58 @@ impl<FileId> Label<FileId> {
104104
Label::new(LabelStyle::Secondary, file_id, range)
105105
}
106106

107-
/// Add a message to the diagnostic.
107+
/// Set the message for the diagnostic. The old message (if any) is discarded.
108108
pub fn with_message(mut self, message: impl ToString) -> Label<FileId> {
109109
self.message = message.to_string();
110110
self
111111
}
112+
113+
/// Set the file id. The old file id (if any) is discarded.
114+
pub fn with_file<NewFileId>(self, file_id: NewFileId) -> Label<NewFileId> {
115+
Label {
116+
style: self.style,
117+
file_id,
118+
range: self.range,
119+
message: self.message,
120+
}
121+
}
122+
}
123+
124+
// use a separate impl so we do not have to specify the type like this in e.g.
125+
// `primary_anon`:
126+
// ```
127+
// Label::<()>::new_anon(..)
128+
// ```
129+
impl Label<()> {
130+
/// Create a new label without specifying a [`file_id`].
131+
///
132+
/// [`file_id`]: Label::file_id
133+
pub fn new_anon(style: LabelStyle, range: impl Into<Range<usize>>) -> Label<()> {
134+
Label {
135+
style,
136+
file_id: (),
137+
range: range.into(),
138+
message: String::new(),
139+
}
140+
}
141+
142+
/// Create a new label with a style of [`LabelStyle::Primary`] and without
143+
/// specifying a [`file_id`].
144+
///
145+
/// [`LabelStyle::Primary`]: LabelStyle::Primary
146+
/// [`file_id`]: Label::file_id
147+
pub fn primary_anon(range: impl Into<Range<usize>>) -> Label<()> {
148+
Label::new_anon(LabelStyle::Primary, range)
149+
}
150+
151+
/// Create a new label with a style of [`LabelStyle::Secondary`] and without
152+
/// specifying a [`file_id`].
153+
///
154+
/// [`LabelStyle::Secondary`]: LabelStyle::Secondary
155+
/// [`file_id`]: Label::file_id
156+
pub fn secondary_anon(range: impl Into<Range<usize>>) -> Label<()> {
157+
Label::new_anon(LabelStyle::Secondary, range)
158+
}
112159
}
113160

114161
/// Represents a diagnostic message that can provide information like errors and
@@ -207,4 +254,20 @@ impl<FileId> Diagnostic<FileId> {
207254
self.notes.append(&mut notes);
208255
self
209256
}
257+
258+
/// Set the file id for all labels in this Diagnostic by calling
259+
/// [`Label::with_file`] on each label.
260+
pub fn with_file<NewFileId: Clone>(mut self, file_id: NewFileId) -> Diagnostic<NewFileId> {
261+
Diagnostic {
262+
severity: self.severity,
263+
code: self.code,
264+
message: self.message,
265+
labels: self
266+
.labels
267+
.drain(..)
268+
.map(|label| label.with_file(file_id.clone()))
269+
.collect(),
270+
notes: self.notes,
271+
}
272+
}
210273
}

0 commit comments

Comments
 (0)