@@ -39,6 +39,10 @@ use crate::html::{highlight, static_files};
39
39
use askama:: Template ;
40
40
use itertools:: Itertools ;
41
41
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
+
42
46
/// Generates an Askama template struct for rendering items with common methods.
43
47
///
44
48
/// Usage:
@@ -124,6 +128,16 @@ macro_rules! item_template_methods {
124
128
}
125
129
item_template_methods!( $( $rest) * ) ;
126
130
} ;
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
+ } ;
127
141
( render_assoc_items $( $rest: tt) * ) => {
128
142
fn render_assoc_items<' b>( & ' b self ) -> impl fmt:: Display + Captures <' a> + ' b + Captures <' cx> {
129
143
display_fn( move |f| {
@@ -321,63 +335,6 @@ fn toggle_close(mut w: impl fmt::Write) {
321
335
w. write_str ( "</details>" ) . unwrap ( ) ;
322
336
}
323
337
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
-
381
338
fn item_module ( w : & mut Buffer , cx : & mut Context < ' _ > , item : & clean:: Item , items : & [ clean:: Item ] ) {
382
339
write ! ( w, "{}" , document( cx, item, None , HeadingOffset :: H2 ) ) ;
383
340
@@ -1642,14 +1599,16 @@ fn item_constant(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, c: &cle
1642
1599
}
1643
1600
1644
1601
fn 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
+ ) ;
1653
1612
1654
1613
struct Field < ' a > {
1655
1614
item : & ' a clean:: Item ,
@@ -1658,12 +1617,6 @@ fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean
1658
1617
ty : String ,
1659
1618
}
1660
1619
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
-
1667
1620
impl < ' a , ' cx : ' a > ItemStruct < ' a , ' cx > {
1668
1621
fn new (
1669
1622
cx : std:: cell:: RefCell < & ' a mut Context < ' cx > > ,
0 commit comments