@@ -39,6 +39,10 @@ use crate::html::{highlight, static_files};
3939use askama:: Template ;
4040use itertools:: Itertools ;
4141
42+ trait ItemTemplate < ' a , ' cx : ' a > : askama:: Template + fmt:: Display {
43+ fn item_and_mut_cx ( & self ) -> ( & ' a clean:: Item , RefMut < ' _ , & ' a mut Context < ' cx > > ) ;
44+ }
45+
4246/// Generates an Askama template struct for rendering items with common methods.
4347///
4448/// Usage:
@@ -124,6 +128,16 @@ macro_rules! item_template_methods {
124128 }
125129 item_template_methods!( $( $rest) * ) ;
126130 } ;
131+ ( render_attributes_in_code $( $rest: tt) * ) => {
132+ fn render_attributes_in_code<' b>( & ' b self ) -> impl fmt:: Display + Captures <' a> + ' b + Captures <' cx> {
133+ display_fn( move |f| {
134+ let ( it, cx) = self . item_and_mut_cx( ) ;
135+ let v = render_attributes_in_code( it, cx. tcx( ) ) ;
136+ write!( f, "{v}" )
137+ } )
138+ }
139+ item_template_methods!( $( $rest) * ) ;
140+ } ;
127141 ( render_assoc_items $( $rest: tt) * ) => {
128142 fn render_assoc_items<' b>( & ' b self ) -> impl fmt:: Display + Captures <' a> + ' b + Captures <' cx> {
129143 display_fn( move |f| {
@@ -321,63 +335,6 @@ fn toggle_close(mut w: impl fmt::Write) {
321335 w. write_str ( "</details>" ) . unwrap ( ) ;
322336}
323337
324- trait ItemTemplate < ' a , ' cx : ' a > : askama:: Template + fmt:: Display {
325- fn item_and_mut_cx ( & self ) -> ( & ' a clean:: Item , RefMut < ' _ , & ' a mut Context < ' cx > > ) ;
326- }
327-
328- fn item_template_document < ' a : ' b , ' b , ' cx : ' a > (
329- templ : & ' b impl ItemTemplate < ' a , ' cx > ,
330- ) -> impl fmt:: Display + Captures < ' a > + ' b + Captures < ' cx > {
331- display_fn ( move |f| {
332- let ( item, mut cx) = templ. item_and_mut_cx ( ) ;
333- let v = document ( * cx, item, None , HeadingOffset :: H2 ) ;
334- write ! ( f, "{v}" )
335- } )
336- }
337-
338- fn item_template_document_type_layout < ' a : ' b , ' b , ' cx : ' a > (
339- templ : & ' b impl ItemTemplate < ' a , ' cx > ,
340- ) -> impl fmt:: Display + Captures < ' a > + ' b + Captures < ' cx > {
341- display_fn ( move |f| {
342- let ( item, cx) = templ. item_and_mut_cx ( ) ;
343- let def_id = item. item_id . expect_def_id ( ) ;
344- let v = document_type_layout ( * cx, def_id) ;
345- write ! ( f, "{v}" )
346- } )
347- }
348-
349- fn item_template_render_attributes_in_pre < ' a : ' b , ' b , ' cx : ' a > (
350- templ : & ' b impl ItemTemplate < ' a , ' cx > ,
351- ) -> impl fmt:: Display + Captures < ' a > + ' b + Captures < ' cx > {
352- display_fn ( move |f| {
353- let ( item, cx) = templ. item_and_mut_cx ( ) ;
354- let tcx = cx. tcx ( ) ;
355- let v = render_attributes_in_pre ( item, "" , tcx) ;
356- write ! ( f, "{v}" )
357- } )
358- }
359-
360- fn item_template_render_attributes_in_code < ' a : ' b , ' b , ' cx : ' a > (
361- templ : & ' b impl ItemTemplate < ' a , ' cx > ,
362- ) -> impl fmt:: Display + Captures < ' a > + ' b + Captures < ' cx > {
363- display_fn ( move |f| {
364- let ( it, cx) = templ. item_and_mut_cx ( ) ;
365- let v = render_attributes_in_code ( it, cx. tcx ( ) ) ;
366- write ! ( f, "{v}" )
367- } )
368- }
369-
370- fn item_template_render_assoc_items < ' a : ' b , ' b , ' cx : ' a > (
371- templ : & ' b impl ItemTemplate < ' a , ' cx > ,
372- ) -> impl fmt:: Display + Captures < ' a > + ' b + Captures < ' cx > {
373- display_fn ( move |f| {
374- let ( item, mut cx) = templ. item_and_mut_cx ( ) ;
375- let def_id = item. item_id . expect_def_id ( ) ;
376- let v = render_assoc_items ( * cx, item, def_id, AssocItemRender :: All , None ) ;
377- write ! ( f, "{v}" )
378- } )
379- }
380-
381338fn item_module ( w : & mut Buffer , cx : & mut Context < ' _ > , item : & clean:: Item , items : & [ clean:: Item ] ) {
382339 write ! ( w, "{}" , document( cx, item, None , HeadingOffset :: H2 ) ) ;
383340
@@ -1642,14 +1599,16 @@ fn item_constant(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, c: &cle
16421599}
16431600
16441601fn item_struct ( w : & mut Buffer , cx : & mut Context < ' _ > , it : & clean:: Item , s : & clean:: Struct ) {
1645- #[ derive( Template ) ]
1646- #[ template( path = "item_struct.html" ) ]
1647- struct ItemStruct < ' a , ' cx > {
1648- cx : std:: cell:: RefCell < & ' a mut Context < ' cx > > ,
1649- it : & ' a clean:: Item ,
1650- s : & ' a clean:: Struct ,
1651- should_render_fields : bool ,
1652- }
1602+ item_template ! (
1603+ #[ template( path = "item_struct.html" ) ]
1604+ struct ItemStruct <' a, ' cx> {
1605+ cx: RefCell <& ' a mut Context <' cx>>,
1606+ it: & ' a clean:: Item ,
1607+ s: & ' a clean:: Struct ,
1608+ should_render_fields: bool ,
1609+ } ,
1610+ methods = [ render_attributes_in_code, document, render_assoc_items, document_type_layout]
1611+ ) ;
16531612
16541613 struct Field < ' a > {
16551614 item : & ' a clean:: Item ,
@@ -1658,12 +1617,6 @@ fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean
16581617 ty : String ,
16591618 }
16601619
1661- impl < ' a , ' cx : ' a > ItemTemplate < ' a , ' cx > for ItemStruct < ' a , ' cx > {
1662- fn item_and_mut_cx ( & self ) -> ( & ' a clean:: Item , RefMut < ' _ , & ' a mut Context < ' cx > > ) {
1663- ( self . it , self . cx . borrow_mut ( ) )
1664- }
1665- }
1666-
16671620 impl < ' a , ' cx : ' a > ItemStruct < ' a , ' cx > {
16681621 fn new (
16691622 cx : std:: cell:: RefCell < & ' a mut Context < ' cx > > ,
0 commit comments