@@ -42,6 +42,10 @@ use crate::html::{highlight, static_files};
4242use askama:: Template ;
4343use itertools:: Itertools ;
4444
45+ trait ItemTemplate < ' a , ' cx : ' a > : askama:: Template + fmt:: Display {
46+ fn item_and_mut_cx ( & self ) -> ( & ' a clean:: Item , RefMut < ' _ , & ' a mut Context < ' cx > > ) ;
47+ }
48+
4549/// Generates an Askama template struct for rendering items with common methods.
4650///
4751/// Usage:
@@ -126,6 +130,16 @@ macro_rules! item_template_methods {
126130 }
127131 item_template_methods!( $( $rest) * ) ;
128132 } ;
133+ ( render_attributes_in_code $( $rest: tt) * ) => {
134+ fn render_attributes_in_code<' b>( & ' b self ) -> impl fmt:: Display + Captures <' a> + ' b + Captures <' cx> {
135+ display_fn( move |f| {
136+ let ( it, cx) = self . item_and_mut_cx( ) ;
137+ let v = render_attributes_in_code( it, cx. tcx( ) ) ;
138+ write!( f, "{v}" )
139+ } )
140+ }
141+ item_template_methods!( $( $rest) * ) ;
142+ } ;
129143 ( render_assoc_items $( $rest: tt) * ) => {
130144 fn render_assoc_items<' b>( & ' b self ) -> impl fmt:: Display + Captures <' a> + ' b + Captures <' cx> {
131145 display_fn( move |f| {
@@ -322,63 +336,6 @@ fn toggle_close(mut w: impl fmt::Write) {
322336 w. write_str ( "</details>" ) . unwrap ( ) ;
323337}
324338
325- trait ItemTemplate < ' a , ' cx : ' a > : askama:: Template + fmt:: Display {
326- fn item_and_mut_cx ( & self ) -> ( & ' a clean:: Item , RefMut < ' _ , & ' a mut Context < ' cx > > ) ;
327- }
328-
329- fn item_template_document < ' a : ' b , ' b , ' cx : ' a > (
330- templ : & ' b impl ItemTemplate < ' a , ' cx > ,
331- ) -> impl fmt:: Display + Captures < ' a > + ' b + Captures < ' cx > {
332- display_fn ( move |f| {
333- let ( item, mut cx) = templ. item_and_mut_cx ( ) ;
334- let v = document ( * cx, item, None , HeadingOffset :: H2 ) ;
335- write ! ( f, "{v}" )
336- } )
337- }
338-
339- fn item_template_document_type_layout < ' a : ' b , ' b , ' cx : ' a > (
340- templ : & ' b impl ItemTemplate < ' a , ' cx > ,
341- ) -> impl fmt:: Display + Captures < ' a > + ' b + Captures < ' cx > {
342- display_fn ( move |f| {
343- let ( item, cx) = templ. item_and_mut_cx ( ) ;
344- let def_id = item. item_id . expect_def_id ( ) ;
345- let v = document_type_layout ( * cx, def_id) ;
346- write ! ( f, "{v}" )
347- } )
348- }
349-
350- fn item_template_render_attributes_in_pre < ' a : ' b , ' b , ' cx : ' a > (
351- templ : & ' b impl ItemTemplate < ' a , ' cx > ,
352- ) -> impl fmt:: Display + Captures < ' a > + ' b + Captures < ' cx > {
353- display_fn ( move |f| {
354- let ( item, cx) = templ. item_and_mut_cx ( ) ;
355- let tcx = cx. tcx ( ) ;
356- let v = render_attributes_in_pre ( item, "" , tcx) ;
357- write ! ( f, "{v}" )
358- } )
359- }
360-
361- fn item_template_render_attributes_in_code < ' a : ' b , ' b , ' cx : ' a > (
362- templ : & ' b impl ItemTemplate < ' a , ' cx > ,
363- ) -> impl fmt:: Display + Captures < ' a > + ' b + Captures < ' cx > {
364- display_fn ( move |f| {
365- let ( it, cx) = templ. item_and_mut_cx ( ) ;
366- let v = render_attributes_in_code ( it, cx. tcx ( ) ) ;
367- write ! ( f, "{v}" )
368- } )
369- }
370-
371- fn item_template_render_assoc_items < ' a : ' b , ' b , ' cx : ' a > (
372- templ : & ' b impl ItemTemplate < ' a , ' cx > ,
373- ) -> impl fmt:: Display + Captures < ' a > + ' b + Captures < ' cx > {
374- display_fn ( move |f| {
375- let ( item, mut cx) = templ. item_and_mut_cx ( ) ;
376- let def_id = item. item_id . expect_def_id ( ) ;
377- let v = render_assoc_items ( * cx, item, def_id, AssocItemRender :: All , None ) ;
378- write ! ( f, "{v}" )
379- } )
380- }
381-
382339fn item_module ( w : & mut Buffer , cx : & mut Context < ' _ > , item : & clean:: Item , items : & [ clean:: Item ] ) {
383340 write ! ( w, "{}" , document( cx, item, None , HeadingOffset :: H2 ) ) ;
384341
@@ -1864,14 +1821,16 @@ fn item_constant(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, c: &cle
18641821}
18651822
18661823fn item_struct ( w : & mut Buffer , cx : & mut Context < ' _ > , it : & clean:: Item , s : & clean:: Struct ) {
1867- #[ derive( Template ) ]
1868- #[ template( path = "item_struct.html" ) ]
1869- struct ItemStruct < ' a , ' cx > {
1870- cx : std:: cell:: RefCell < & ' a mut Context < ' cx > > ,
1871- it : & ' a clean:: Item ,
1872- s : & ' a clean:: Struct ,
1873- should_render_fields : bool ,
1874- }
1824+ item_template ! (
1825+ #[ template( path = "item_struct.html" ) ]
1826+ struct ItemStruct <' a, ' cx> {
1827+ cx: RefCell <& ' a mut Context <' cx>>,
1828+ it: & ' a clean:: Item ,
1829+ s: & ' a clean:: Struct ,
1830+ should_render_fields: bool ,
1831+ } ,
1832+ methods = [ render_attributes_in_code, document, render_assoc_items, document_type_layout]
1833+ ) ;
18751834
18761835 struct Field < ' a > {
18771836 item : & ' a clean:: Item ,
@@ -1880,12 +1839,6 @@ fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean
18801839 ty : String ,
18811840 }
18821841
1883- impl < ' a , ' cx : ' a > ItemTemplate < ' a , ' cx > for ItemStruct < ' a , ' cx > {
1884- fn item_and_mut_cx ( & self ) -> ( & ' a clean:: Item , RefMut < ' _ , & ' a mut Context < ' cx > > ) {
1885- ( self . it , self . cx . borrow_mut ( ) )
1886- }
1887- }
1888-
18891842 impl < ' a , ' cx : ' a > ItemStruct < ' a , ' cx > {
18901843 fn new (
18911844 cx : std:: cell:: RefCell < & ' a mut Context < ' cx > > ,
0 commit comments