Skip to content

Commit 949c553

Browse files
authored
Rollup merge of #108757 - clubby789:askama-move, r=notriddle,jsha,GuillaumeGomez
rustdoc: Migrate `document_item_info` to Askama https://rust-lang.zulipchat.com/#narrow/stream/266220-rustdoc/topic/rustdoc.20allocations.20are.20slow Hoping to piece-by-piece migrate things to template. Had a few failed attempts at more complex parts of the code, so this is just a start.
2 parents ef84194 + bb37b60 commit 949c553

File tree

3 files changed

+84
-43
lines changed

3 files changed

+84
-43
lines changed

src/librustdoc/html/render/mod.rs

+54-43
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ use std::rc::Rc;
4646
use std::str;
4747
use std::string::ToString;
4848

49+
use askama::Template;
4950
use rustc_ast_pretty::pprust;
5051
use rustc_attr::{ConstStability, Deprecation, StabilityLevel};
5152
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
@@ -418,7 +419,7 @@ fn document(
418419
if let Some(ref name) = item.name {
419420
info!("Documenting {}", name);
420421
}
421-
document_item_info(w, cx, item, parent);
422+
document_item_info(cx, item, parent).render_into(w).unwrap();
422423
if parent.is_none() {
423424
document_full_collapsible(w, item, cx, heading_offset);
424425
} else {
@@ -460,7 +461,7 @@ fn document_short(
460461
parent: &clean::Item,
461462
show_def_docs: bool,
462463
) {
463-
document_item_info(w, cx, item, Some(parent));
464+
document_item_info(cx, item, Some(parent)).render_into(w).unwrap();
464465
if !show_def_docs {
465466
return;
466467
}
@@ -532,25 +533,23 @@ fn document_full_inner(
532533
}
533534
}
534535

536+
#[derive(Template)]
537+
#[template(path = "item_info.html")]
538+
struct ItemInfo {
539+
items: Vec<ShortItemInfo>,
540+
}
535541
/// Add extra information about an item such as:
536542
///
537543
/// * Stability
538544
/// * Deprecated
539545
/// * Required features (through the `doc_cfg` feature)
540546
fn document_item_info(
541-
w: &mut Buffer,
542547
cx: &mut Context<'_>,
543548
item: &clean::Item,
544549
parent: Option<&clean::Item>,
545-
) {
546-
let item_infos = short_item_info(item, cx, parent);
547-
if !item_infos.is_empty() {
548-
w.write_str("<span class=\"item-info\">");
549-
for info in item_infos {
550-
w.write_str(&info);
551-
}
552-
w.write_str("</span>");
553-
}
550+
) -> ItemInfo {
551+
let items = short_item_info(item, cx, parent);
552+
ItemInfo { items }
554553
}
555554

556555
fn portability(item: &clean::Item, parent: Option<&clean::Item>) -> Option<String> {
@@ -568,7 +567,25 @@ fn portability(item: &clean::Item, parent: Option<&clean::Item>) -> Option<Strin
568567
cfg
569568
);
570569

571-
Some(format!("<div class=\"stab portability\">{}</div>", cfg?.render_long_html()))
570+
Some(cfg?.render_long_html())
571+
}
572+
573+
#[derive(Template)]
574+
#[template(path = "short_item_info.html")]
575+
enum ShortItemInfo {
576+
/// A message describing the deprecation of this item
577+
Deprecation {
578+
message: String,
579+
},
580+
/// The feature corresponding to an unstable item, and optionally
581+
/// a tracking issue URL and number.
582+
Unstable {
583+
feature: String,
584+
tracking: Option<(String, u32)>,
585+
},
586+
Portability {
587+
message: String,
588+
},
572589
}
573590

574591
/// Render the stability, deprecation and portability information that is displayed at the top of
@@ -577,7 +594,7 @@ fn short_item_info(
577594
item: &clean::Item,
578595
cx: &mut Context<'_>,
579596
parent: Option<&clean::Item>,
580-
) -> Vec<String> {
597+
) -> Vec<ShortItemInfo> {
581598
let mut extra_info = vec![];
582599

583600
if let Some(depr @ Deprecation { note, since, is_since_rustc_version: _, suggestion: _ }) =
@@ -603,15 +620,10 @@ fn short_item_info(
603620
if let Some(note) = note {
604621
let note = note.as_str();
605622
let html = MarkdownItemInfo(note, &mut cx.id_map);
606-
message.push_str(&format!(": {}", html.into_string()));
623+
message.push_str(": ");
624+
message.push_str(&html.into_string());
607625
}
608-
extra_info.push(format!(
609-
"<div class=\"stab deprecated\">\
610-
<span class=\"emoji\">👎</span>\
611-
<span>{}</span>\
612-
</div>",
613-
message,
614-
));
626+
extra_info.push(ShortItemInfo::Deprecation { message });
615627
}
616628

617629
// Render unstable items. But don't render "rustc_private" crates (internal compiler crates).
@@ -622,26 +634,17 @@ fn short_item_info(
622634
.filter(|stab| stab.feature != sym::rustc_private)
623635
.map(|stab| (stab.level, stab.feature))
624636
{
625-
let mut message = "<span class=\"emoji\">🔬</span>\
626-
<span>This is a nightly-only experimental API."
627-
.to_owned();
628-
629-
let mut feature = format!("<code>{}</code>", Escape(feature.as_str()));
630-
if let (Some(url), Some(issue)) = (&cx.shared.issue_tracker_base_url, issue) {
631-
feature.push_str(&format!(
632-
"&nbsp;<a href=\"{url}{issue}\">#{issue}</a>",
633-
url = url,
634-
issue = issue
635-
));
636-
}
637-
638-
message.push_str(&format!(" ({})</span>", feature));
639-
640-
extra_info.push(format!("<div class=\"stab unstable\">{}</div>", message));
637+
let tracking = if let (Some(url), Some(issue)) = (&cx.shared.issue_tracker_base_url, issue)
638+
{
639+
Some((url.clone(), issue.get()))
640+
} else {
641+
None
642+
};
643+
extra_info.push(ShortItemInfo::Unstable { feature: feature.to_string(), tracking });
641644
}
642645

643-
if let Some(portability) = portability(item, parent) {
644-
extra_info.push(portability);
646+
if let Some(message) = portability(item, parent) {
647+
extra_info.push(ShortItemInfo::Portability { message });
645648
}
646649

647650
extra_info
@@ -1473,7 +1476,9 @@ fn render_impl(
14731476
// We need the stability of the item from the trait
14741477
// because impls can't have a stability.
14751478
if item.doc_value().is_some() {
1476-
document_item_info(&mut info_buffer, cx, it, Some(parent));
1479+
document_item_info(cx, it, Some(parent))
1480+
.render_into(&mut info_buffer)
1481+
.unwrap();
14771482
document_full(&mut doc_buffer, item, cx, HeadingOffset::H5);
14781483
short_documented = false;
14791484
} else {
@@ -1490,7 +1495,9 @@ fn render_impl(
14901495
}
14911496
}
14921497
} else {
1493-
document_item_info(&mut info_buffer, cx, item, Some(parent));
1498+
document_item_info(cx, item, Some(parent))
1499+
.render_into(&mut info_buffer)
1500+
.unwrap();
14941501
if rendering_params.show_def_docs {
14951502
document_full(&mut doc_buffer, item, cx, HeadingOffset::H5);
14961503
short_documented = false;
@@ -1863,7 +1870,11 @@ pub(crate) fn render_impl_summary(
18631870
let is_trait = inner_impl.trait_.is_some();
18641871
if is_trait {
18651872
if let Some(portability) = portability(&i.impl_item, Some(parent)) {
1866-
write!(w, "<span class=\"item-info\">{}</span>", portability);
1873+
write!(
1874+
w,
1875+
"<span class=\"item-info\"><div class=\"stab portability\">{}</div></span>",
1876+
portability
1877+
);
18671878
}
18681879
}
18691880

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{% if !items.is_empty() %}
2+
<span class="item-info"> {# #}
3+
{% for item in items %}
4+
{{item|safe}} {# #}
5+
{% endfor %}
6+
</span>
7+
{% endif %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{% match self %}
2+
{% when Self::Deprecation with { message } %}
3+
<div class="stab deprecated"> {# #}
4+
<span class="emoji">👎</span> {# #}
5+
<span>{{message}}</span> {# #}
6+
</div> {# #}
7+
{% when Self::Unstable with { feature, tracking } %}
8+
<div class="stab unstable"> {# #}
9+
<span class="emoji">🔬</span> {# #}
10+
<span> {# #}
11+
This is a nightly-only experimental API. ({# #}
12+
<code>{{feature}}</code> {# #}
13+
{% match tracking %}
14+
{% when Some with ((url, num)) %}
15+
&nbsp;<a href="{{url}}{{num}}">#{{num}}</a> {# #}
16+
{% when None %}
17+
{% endmatch %}
18+
) {# #}
19+
</span> {# #}
20+
</div> {# #}
21+
{% when Self::Portability with { message } %}
22+
<div class="stab portability">{{message|safe}}</div> {# #}
23+
{% endmatch %}

0 commit comments

Comments
 (0)