Skip to content

Commit

Permalink
Use item_template macro
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklimmm committed Jun 12, 2023
1 parent af05b25 commit 6a33b54
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 75 deletions.
95 changes: 24 additions & 71 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ use crate::html::{highlight, static_files};
use askama::Template;
use itertools::Itertools;

trait ItemTemplate<'a, 'cx: 'a>: askama::Template + fmt::Display {
fn item_and_mut_cx(&self) -> (&'a clean::Item, RefMut<'_, &'a mut Context<'cx>>);
}

/// Generates an Askama template struct for rendering items with common methods.
///
/// Usage:
Expand Down Expand Up @@ -124,6 +128,16 @@ macro_rules! item_template_methods {
}
item_template_methods!($($rest)*);
};
(render_attributes_in_code $($rest:tt)*) => {
fn render_attributes_in_code<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
display_fn(move |f| {
let (it, cx) = self.item_and_mut_cx();
let v = render_attributes_in_code(it, cx.tcx());
write!(f, "{v}")
})
}
item_template_methods!($($rest)*);
};
(render_assoc_items $($rest:tt)*) => {
fn render_assoc_items<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
display_fn(move |f| {
Expand Down Expand Up @@ -321,63 +335,6 @@ fn toggle_close(mut w: impl fmt::Write) {
w.write_str("</details>").unwrap();
}

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 @@ -1642,14 +1599,16 @@ fn item_constant(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, c: &cle
}

fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Struct) {
#[derive(Template)]
#[template(path = "item_struct.html")]
struct ItemStruct<'a, 'cx> {
cx: std::cell::RefCell<&'a mut Context<'cx>>,
it: &'a clean::Item,
s: &'a clean::Struct,
should_render_fields: bool,
}
item_template!(
#[template(path = "item_struct.html")]
struct ItemStruct<'a, 'cx> {
cx: RefCell<&'a mut Context<'cx>>,
it: &'a clean::Item,
s: &'a clean::Struct,
should_render_fields: bool,
},
methods = [render_attributes_in_code, document, render_assoc_items, document_type_layout]
);

struct Field<'a> {
item: &'a clean::Item,
Expand All @@ -1658,12 +1617,6 @@ 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 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::item_template_render_attributes_in_code(self.borrow()) | safe }}
{{ self.render_attributes_in_code() | safe }}
{{ self.render_struct() | safe }}
</code></pre>
{{ self::item_template_document(self.borrow()) | safe }}
{{ self.document() | 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::item_template_render_assoc_items(self.borrow()) | safe }}
{{ self::item_template_document_type_layout(self.borrow()) | safe }}
{{ self.render_assoc_items() | safe }}
{{ self.document_type_layout() | safe }}

0 comments on commit 6a33b54

Please sign in to comment.