Skip to content

Commit 2621f7f

Browse files
committed
Rework StringPart.
When there are two possibilities, both of which use a `String`, it's nicer to use a struct than an enum. Especially when mapping the contents into a tuple.
1 parent 26eb6da commit 2621f7f

File tree

2 files changed

+17
-21
lines changed

2 files changed

+17
-21
lines changed

compiler/rustc_errors/src/diagnostic.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,10 @@ impl DiagnosticStyledString {
165165
DiagnosticStyledString(vec![])
166166
}
167167
pub fn push_normal<S: Into<String>>(&mut self, t: S) {
168-
self.0.push(StringPart::Normal(t.into()));
168+
self.0.push(StringPart::normal(t.into()));
169169
}
170170
pub fn push_highlighted<S: Into<String>>(&mut self, t: S) {
171-
self.0.push(StringPart::Highlighted(t.into()));
171+
self.0.push(StringPart::highlighted(t.into()));
172172
}
173173
pub fn push<S: Into<String>>(&mut self, t: S, highlight: bool) {
174174
if highlight {
@@ -178,29 +178,31 @@ impl DiagnosticStyledString {
178178
}
179179
}
180180
pub fn normal<S: Into<String>>(t: S) -> DiagnosticStyledString {
181-
DiagnosticStyledString(vec![StringPart::Normal(t.into())])
181+
DiagnosticStyledString(vec![StringPart::normal(t.into())])
182182
}
183183

184184
pub fn highlighted<S: Into<String>>(t: S) -> DiagnosticStyledString {
185-
DiagnosticStyledString(vec![StringPart::Highlighted(t.into())])
185+
DiagnosticStyledString(vec![StringPart::highlighted(t.into())])
186186
}
187187

188188
pub fn content(&self) -> String {
189-
self.0.iter().map(|x| x.content()).collect::<String>()
189+
self.0.iter().map(|x| x.content.as_str()).collect::<String>()
190190
}
191191
}
192192

193193
#[derive(Debug, PartialEq, Eq)]
194-
pub enum StringPart {
195-
Normal(String),
196-
Highlighted(String),
194+
pub struct StringPart {
195+
content: String,
196+
style: Style,
197197
}
198198

199199
impl StringPart {
200-
pub fn content(&self) -> &str {
201-
match self {
202-
&StringPart::Normal(ref s) | &StringPart::Highlighted(ref s) => s,
203-
}
200+
fn normal(content: String) -> StringPart {
201+
StringPart { content, style: Style::NoStyle }
202+
}
203+
204+
fn highlighted(content: String) -> StringPart {
205+
StringPart { content, style: Style::Highlight }
204206
}
205207
}
206208

@@ -394,16 +396,10 @@ impl Diagnostic {
394396
};
395397
let mut msg: Vec<_> =
396398
vec![(format!("{}{} `", " ".repeat(expected_padding), expected_label), Style::NoStyle)];
397-
msg.extend(expected.0.iter().map(|x| match *x {
398-
StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle),
399-
StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight),
400-
}));
399+
msg.extend(expected.0.into_iter().map(|p| (p.content, p.style)));
401400
msg.push((format!("`{expected_extra}\n"), Style::NoStyle));
402401
msg.push((format!("{}{} `", " ".repeat(found_padding), found_label), Style::NoStyle));
403-
msg.extend(found.0.iter().map(|x| match *x {
404-
StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle),
405-
StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight),
406-
}));
402+
msg.extend(found.0.into_iter().map(|p| (p.content, p.style)));
407403
msg.push((format!("`{found_extra}"), Style::NoStyle));
408404

409405
// For now, just attach these as notes.

compiler/rustc_errors/src/snippet.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ pub struct StyledString {
197197
pub style: Style,
198198
}
199199

200-
#[derive(Copy, Clone, Debug, PartialEq, Hash, Encodable, Decodable)]
200+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
201201
pub enum Style {
202202
MainHeaderMsg,
203203
HeaderMsg,

0 commit comments

Comments
 (0)