Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustdoc: migrate item_struct to an Askama template #111430

Closed
Prev Previous commit
Next Next commit
Use ItemTemplate trait
  • Loading branch information
nicklimmm committed Oct 27, 2023
commit 1270d0060592b9824db5c29bad5766d4c0e26357
98 changes: 59 additions & 39 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,59 @@ trait ItemTemplate<'a, 'cx: 'a>: askama::Template + fmt::Display {
fn item_and_mut_cx(&self) -> (&'a clean::Item, RefMut<'_, &'a mut Context<'cx>>);
}

fn item_template_document<'a: 'b, 'b, 'cx: 'a>(
templ: &'b impl ItemTemplate<'a, 'cx>,
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
display_fn(move |f| {
let (item, mut cx) = templ.item_and_mut_cx();
let v = document(*cx, item, None, HeadingOffset::H2);
write!(f, "{v}")
})
}

fn item_template_document_type_layout<'a: 'b, 'b, 'cx: 'a>(
templ: &'b impl ItemTemplate<'a, 'cx>,
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
display_fn(move |f| {
let (item, cx) = templ.item_and_mut_cx();
let def_id = item.item_id.expect_def_id();
let v = document_type_layout(*cx, def_id);
write!(f, "{v}")
})
}

fn item_template_render_attributes_in_pre<'a: 'b, 'b, 'cx: 'a>(
templ: &'b impl ItemTemplate<'a, 'cx>,
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
display_fn(move |f| {
let (item, cx) = templ.item_and_mut_cx();
let tcx = cx.tcx();
let v = render_attributes_in_pre(item, "", tcx);
write!(f, "{v}")
})
}

fn item_template_render_attributes_in_code<'a: 'b, 'b, 'cx: 'a>(
templ: &'b impl ItemTemplate<'a, 'cx>,
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
display_fn(move |f| {
let (it, cx) = templ.item_and_mut_cx();
let v = render_attributes_in_code(it, cx.tcx());
write!(f, "{v}")
})
}

fn item_template_render_assoc_items<'a: 'b, 'b, 'cx: 'a>(
templ: &'b impl ItemTemplate<'a, 'cx>,
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
display_fn(move |f| {
let (item, mut cx) = templ.item_and_mut_cx();
let def_id = item.item_id.expect_def_id();
let v = render_assoc_items(*cx, item, def_id, AssocItemRender::All, None);
write!(f, "{v}")
})
}

fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items: &[clean::Item]) {
write!(w, "{}", document(cx, item, None, HeadingOffset::H2));

Expand Down Expand Up @@ -1827,6 +1880,12 @@ fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean
ty: String,
}

impl<'a, 'cx: 'a> ItemTemplate<'a, 'cx> for ItemStruct<'a, 'cx> {
fn item_and_mut_cx(&self) -> (&'a clean::Item, RefMut<'_, &'a mut Context<'cx>>) {
(self.it, self.cx.borrow_mut())
}
}

impl<'a, 'cx: 'a> ItemStruct<'a, 'cx> {
fn new(
cx: std::cell::RefCell<&'a mut Context<'cx>>,
Expand All @@ -1838,15 +1897,6 @@ fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean
Self { cx, it, s, should_render_fields }
}

fn render_attributes_in_code<'b>(
&'b self,
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
display_fn(move |f| {
let tcx = self.cx.borrow().tcx();
write!(f, "{}", render_attributes_in_code(self.it, tcx))
})
}

fn render_struct<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
display_fn(move |f| {
let cx = self.cx.borrow();
Expand All @@ -1863,14 +1913,6 @@ fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean
})
}

fn document<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
display_fn(move |f| {
let mut cx = self.cx.borrow_mut();
let v = document(*cx, self.it, None, HeadingOffset::H2);
write!(f, "{v}")
})
}

fn struct_field_items_iter<'b>(
&'b self,
) -> impl Iterator<Item = Field<'a>> + Captures<'a> + 'b + Captures<'cx> {
Expand All @@ -1894,28 +1936,6 @@ fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean
write!(f, "{v}")
})
}

fn render_assoc_items<'b>(
&'b self,
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
display_fn(move |f| {
let mut cx = self.cx.borrow_mut();
let def_id = self.it.item_id.expect_def_id();
let v = render_assoc_items(*cx, self.it, def_id, AssocItemRender::All);
write!(f, "{v}")
})
}

fn document_type_layout<'b>(
&'b self,
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
display_fn(move |f| {
let cx = self.cx.borrow();
let def_id = self.it.item_id.expect_def_id();
let v = document_type_layout(*cx, def_id);
write!(f, "{v}")
})
}
}

ItemStruct::new(std::cell::RefCell::new(cx), it, s).render_into(w).unwrap();
Expand Down
8 changes: 4 additions & 4 deletions src/librustdoc/html/templates/item_struct.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<pre class="rust item-decl"><code>
{{ self.render_attributes_in_code() | safe }}
{{ self::item_template_render_attributes_in_code(self.borrow()) | safe }}
{{ self.render_struct() | safe }}
</code></pre>
{{ self.document() | safe }}
{{ self::item_template_document(self.borrow()) | safe }}
{% if self.should_render_fields %}
<h2 id="fields" class="fields small-section header">
{% if self.s.ctor_kind.is_none() %}
Expand All @@ -22,5 +22,5 @@ <h2 id="fields" class="fields small-section header">
{{ self.document_field(field.item) | safe }}
{% endfor %}
{% endif %}
{{ self.render_assoc_items() | safe }}
{{ self.document_type_layout() | safe }}
{{ self::item_template_render_assoc_items(self.borrow()) | safe }}
{{ self::item_template_document_type_layout(self.borrow()) | safe }}