@@ -42,6 +42,10 @@ use crate::html::{highlight, static_files};
42
42
use askama:: Template ;
43
43
use itertools:: Itertools ;
44
44
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
+
45
49
/// Generates an Askama template struct for rendering items with common methods.
46
50
///
47
51
/// Usage:
@@ -126,6 +130,16 @@ macro_rules! item_template_methods {
126
130
}
127
131
item_template_methods!( $( $rest) * ) ;
128
132
} ;
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
+ } ;
129
143
( render_assoc_items $( $rest: tt) * ) => {
130
144
fn render_assoc_items<' b>( & ' b self ) -> impl fmt:: Display + Captures <' a> + ' b + Captures <' cx> {
131
145
display_fn( move |f| {
@@ -322,63 +336,6 @@ fn toggle_close(mut w: impl fmt::Write) {
322
336
w. write_str ( "</details>" ) . unwrap ( ) ;
323
337
}
324
338
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
-
382
339
fn item_module ( w : & mut Buffer , cx : & mut Context < ' _ > , item : & clean:: Item , items : & [ clean:: Item ] ) {
383
340
write ! ( w, "{}" , document( cx, item, None , HeadingOffset :: H2 ) ) ;
384
341
@@ -1864,14 +1821,16 @@ fn item_constant(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, c: &cle
1864
1821
}
1865
1822
1866
1823
fn 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
+ ) ;
1875
1834
1876
1835
struct Field < ' a > {
1877
1836
item : & ' a clean:: Item ,
@@ -1880,12 +1839,6 @@ fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean
1880
1839
ty : String ,
1881
1840
}
1882
1841
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
-
1889
1842
impl < ' a , ' cx : ' a > ItemStruct < ' a , ' cx > {
1890
1843
fn new (
1891
1844
cx : std:: cell:: RefCell < & ' a mut Context < ' cx > > ,
0 commit comments