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.
This commit is contained in:
Nicholas Nethercote 2024-01-30 17:10:48 +11:00
parent 26eb6da4e7
commit 2621f7fd9b
2 changed files with 17 additions and 21 deletions

View File

@ -165,10 +165,10 @@ impl DiagnosticStyledString {
DiagnosticStyledString(vec![]) DiagnosticStyledString(vec![])
} }
pub fn push_normal<S: Into<String>>(&mut self, t: S) { pub fn push_normal<S: Into<String>>(&mut self, t: S) {
self.0.push(StringPart::Normal(t.into())); self.0.push(StringPart::normal(t.into()));
} }
pub fn push_highlighted<S: Into<String>>(&mut self, t: S) { pub fn push_highlighted<S: Into<String>>(&mut self, t: S) {
self.0.push(StringPart::Highlighted(t.into())); self.0.push(StringPart::highlighted(t.into()));
} }
pub fn push<S: Into<String>>(&mut self, t: S, highlight: bool) { pub fn push<S: Into<String>>(&mut self, t: S, highlight: bool) {
if highlight { if highlight {
@ -178,29 +178,31 @@ impl DiagnosticStyledString {
} }
} }
pub fn normal<S: Into<String>>(t: S) -> DiagnosticStyledString { pub fn normal<S: Into<String>>(t: S) -> DiagnosticStyledString {
DiagnosticStyledString(vec![StringPart::Normal(t.into())]) DiagnosticStyledString(vec![StringPart::normal(t.into())])
} }
pub fn highlighted<S: Into<String>>(t: S) -> DiagnosticStyledString { pub fn highlighted<S: Into<String>>(t: S) -> DiagnosticStyledString {
DiagnosticStyledString(vec![StringPart::Highlighted(t.into())]) DiagnosticStyledString(vec![StringPart::highlighted(t.into())])
} }
pub fn content(&self) -> String { pub fn content(&self) -> String {
self.0.iter().map(|x| x.content()).collect::<String>() self.0.iter().map(|x| x.content.as_str()).collect::<String>()
} }
} }
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub enum StringPart { pub struct StringPart {
Normal(String), content: String,
Highlighted(String), style: Style,
} }
impl StringPart { impl StringPart {
pub fn content(&self) -> &str { fn normal(content: String) -> StringPart {
match self { StringPart { content, style: Style::NoStyle }
&StringPart::Normal(ref s) | &StringPart::Highlighted(ref s) => s, }
}
fn highlighted(content: String) -> StringPart {
StringPart { content, style: Style::Highlight }
} }
} }
@ -394,16 +396,10 @@ impl Diagnostic {
}; };
let mut msg: Vec<_> = let mut msg: Vec<_> =
vec![(format!("{}{} `", " ".repeat(expected_padding), expected_label), Style::NoStyle)]; vec![(format!("{}{} `", " ".repeat(expected_padding), expected_label), Style::NoStyle)];
msg.extend(expected.0.iter().map(|x| match *x { msg.extend(expected.0.into_iter().map(|p| (p.content, p.style)));
StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle),
StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight),
}));
msg.push((format!("`{expected_extra}\n"), Style::NoStyle)); msg.push((format!("`{expected_extra}\n"), Style::NoStyle));
msg.push((format!("{}{} `", " ".repeat(found_padding), found_label), Style::NoStyle)); msg.push((format!("{}{} `", " ".repeat(found_padding), found_label), Style::NoStyle));
msg.extend(found.0.iter().map(|x| match *x { msg.extend(found.0.into_iter().map(|p| (p.content, p.style)));
StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle),
StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight),
}));
msg.push((format!("`{found_extra}"), Style::NoStyle)); msg.push((format!("`{found_extra}"), Style::NoStyle));
// For now, just attach these as notes. // For now, just attach these as notes.

View File

@ -197,7 +197,7 @@ pub struct StyledString {
pub style: Style, pub style: Style,
} }
#[derive(Copy, Clone, Debug, PartialEq, Hash, Encodable, Decodable)] #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
pub enum Style { pub enum Style {
MainHeaderMsg, MainHeaderMsg,
HeaderMsg, HeaderMsg,