Skip to content

Commit f9931d1

Browse files
committed
rustdoc: refactor Tooltip rendering logic
1 parent 4ef35bc commit f9931d1

File tree

1 file changed

+21
-43
lines changed

1 file changed

+21
-43
lines changed

src/librustdoc/html/highlight.rs

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -81,51 +81,29 @@ fn write_header(
8181
);
8282

8383
if tooltip != Tooltip::None {
84-
// variable for extending lifetimes of temporaries
85-
let tmp;
86-
write_str(
87-
out,
88-
format_args!(
89-
"<a href=\"#\" class=\"tooltip\" title=\"{}\">ⓘ</a>",
90-
match tooltip {
91-
Tooltip::IgnoreAll => "This example is not tested",
92-
Tooltip::IgnoreSome(platforms) => {
93-
tmp = format!(
94-
"This example is not tested on {}",
95-
fmt::from_fn(|f| {
96-
match platforms.len() {
97-
0 => unreachable!(),
98-
1 => f.write_str(&platforms[0]),
99-
2 => write!(f, "{} or {}", &platforms[0], &platforms[1]),
100-
_ => {
101-
for (i, plat) in platforms.iter().enumerate() {
102-
match (platforms.len() - 2).cmp(&i) {
103-
std::cmp::Ordering::Greater => {
104-
write!(f, "{}, ", plat)?
105-
}
106-
std::cmp::Ordering::Equal => {
107-
write!(f, "{}, or ", plat)?
108-
}
109-
std::cmp::Ordering::Less => f.write_str(&plat)?,
110-
}
111-
}
112-
Ok(())
113-
}
114-
}
115-
})
116-
);
117-
&tmp
118-
}
119-
Tooltip::CompileFail => "This example deliberately fails to compile",
120-
Tooltip::ShouldPanic => "This example panics",
121-
Tooltip::Edition(edition) => {
122-
tmp = format!("This example runs with edition {edition}");
123-
&tmp
84+
let tooltip = fmt::from_fn(|f| match &tooltip {
85+
Tooltip::IgnoreAll => f.write_str("This example is not tested"),
86+
Tooltip::IgnoreSome(platforms) => {
87+
f.write_str("This example is not tested on ")?;
88+
match &platforms[..] {
89+
[] => unreachable!(),
90+
[platform] => f.write_str(platform)?,
91+
[first, second] => write!(f, "{first} or {second}")?,
92+
[platforms @ .., last] => {
93+
for platform in platforms {
94+
write!(f, "{platform}, ")?;
95+
}
96+
write!(f, "or {last}")?;
12497
}
125-
Tooltip::None => unreachable!(),
12698
}
127-
),
128-
);
99+
Ok(())
100+
}
101+
Tooltip::CompileFail => f.write_str("This example deliberately fails to compile"),
102+
Tooltip::ShouldPanic => f.write_str("This example panics"),
103+
Tooltip::Edition(edition) => write!(f, "This example runs with edition {edition}"),
104+
Tooltip::None => unreachable!(),
105+
});
106+
write_str(out, format_args!("<a href=\"#\" class=\"tooltip\" title=\"{tooltip}\">ⓘ</a>"));
129107
}
130108

131109
if let Some(extra) = extra_content {

0 commit comments

Comments
 (0)