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
Expand render_field_in_span in template
nicklimmm committed Oct 27, 2023
commit 97aca510732b41913d4c6b518876b735b579e27b
33 changes: 15 additions & 18 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
@@ -1820,6 +1820,13 @@ fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean
should_render_fields: bool,
}

struct Field<'a> {
item: &'a clean::Item,
name: String,
id: String,
ty: String,
}

impl<'a, 'cx: 'a> ItemStruct<'a, 'cx> {
fn new(
cx: std::cell::RefCell<&'a mut Context<'cx>>,
@@ -1864,26 +1871,16 @@ fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean
})
}

fn render_field_in_span<'b>(
fn struct_field_items_iter<'b>(
&'b self,
index: &'b usize,
field: &'b clean::Item,
ty: &'b clean::Type,
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
display_fn(move |f| {
) -> impl Iterator<Item = Field<'a>> + Captures<'a> + 'b + Captures<'cx> {
struct_field_items(self.s).enumerate().map(|(index, (item, ty))| {
let mut cx = self.cx.borrow_mut();
let field_name =
field.name.map_or_else(|| index.to_string(), |sym| sym.as_str().to_string());
let id = cx.derive_id(format!("{}.{}", ItemType::StructField, field_name));
let ty = ty.print(*cx);
write!(
f,
"<span id=\"{id}\" class=\"{item_type} small-section-header\">\
<a href=\"#{id}\" class=\"anchor field\">§</a>\
<code>{field_name}: {ty}</code>\
</span>",
item_type = ItemType::StructField,
)
let name =
item.name.map_or_else(|| index.to_string(), |sym| sym.as_str().to_string());
let id = cx.derive_id(format!("{}.{}", ItemType::StructField, name));
let ty = ty.print(*cx).to_string();
Field { item, name, id, ty }
})
}

9 changes: 6 additions & 3 deletions src/librustdoc/html/templates/item_struct.html
Original file line number Diff line number Diff line change
@@ -14,9 +14,12 @@ <h2 id="fields" class="fields small-section header">
<a href="#fields" class="anchor">§</a>
</h2>
{{ self::document_non_exhaustive(self.it) | safe }}
{% for (index, (field, ty)) in self::struct_field_items(self.s).enumerate() %}
{{ self.render_field_in_span(index, field, ty) | safe }}
{{ self.document_field(field) | safe }}
{% for field in self.struct_field_items_iter() %}
<span id="{{ field.id }}" class="{{ ItemType::StructField ~}} small-section-header">
<a href="#{{ field.id }}" class="anchor field">§</a>
<code>{{ field.name|safe }}: {{~ field.ty|safe }}</code>
</span>
{{ self.document_field(field.item) | safe }}
{% endfor %}
{% endif %}
{{ self.render_assoc_items() | safe }}