@@ -40,90 +40,6 @@ use crate::html::render::sidebar::filters;
4040use crate :: html:: render:: { document_full, document_item_info} ;
4141use crate :: html:: url_parts_builder:: UrlPartsBuilder ;
4242
43- /// Generates an Askama template struct for rendering items with common methods.
44- ///
45- /// Usage:
46- /// ```ignore (illustrative)
47- /// item_template!(
48- /// #[template(path = "<template.html>", /* additional values */)]
49- /// /* additional meta items */
50- /// struct MyItem<'a, 'cx> {
51- /// cx: RefCell<&'a mut Context<'cx>>,
52- /// it: &'a clean::Item,
53- /// /* additional fields */
54- /// },
55- /// methods = [ /* method names (comma separated; refer to macro definition of `item_template_methods!()`) */ ]
56- /// )
57- /// ```
58- ///
59- /// NOTE: ensure that the generic lifetimes (`'a`, `'cx`) and
60- /// required fields (`cx`, `it`) are identical (in terms of order and definition).
61- macro_rules! item_template {
62- (
63- $( #[ $meta: meta] ) *
64- struct $name: ident<' a, ' cx> {
65- cx: & ' a Context <' cx>,
66- it: & ' a clean:: Item ,
67- $( $field_name: ident: $field_ty: ty) ,* ,
68- } ,
69- methods = [ $( $methods: tt) ,* $( , ) ?]
70- ) => {
71- #[ derive( Template ) ]
72- $( #[ $meta] ) *
73- struct $name<' a, ' cx> {
74- cx: & ' a Context <' cx>,
75- it: & ' a clean:: Item ,
76- $( $field_name: $field_ty) ,*
77- }
78-
79- impl <' a, ' cx: ' a> ItemTemplate <' a, ' cx> for $name<' a, ' cx> {
80- fn item_and_cx( & self ) -> ( & ' a clean:: Item , & ' a Context <' cx>) {
81- ( & self . it, & self . cx)
82- }
83- }
84-
85- impl <' a, ' cx: ' a> $name<' a, ' cx> {
86- item_template_methods!( $( $methods) * ) ;
87- }
88- } ;
89- }
90-
91- /// Implement common methods for item template structs generated by `item_template!()`.
92- ///
93- /// NOTE: this macro is intended to be used only by `item_template!()`.
94- macro_rules! item_template_methods {
95- ( ) => { } ;
96- ( document $( $rest: tt) * ) => {
97- fn document( & self ) -> impl fmt:: Display {
98- let ( item, cx) = self . item_and_cx( ) ;
99- document( cx, item, None , HeadingOffset :: H2 )
100- }
101- item_template_methods!( $( $rest) * ) ;
102- } ;
103- ( document_type_layout $( $rest: tt) * ) => {
104- fn document_type_layout( & self ) -> impl fmt:: Display {
105- let ( item, cx) = self . item_and_cx( ) ;
106- let def_id = item. item_id. expect_def_id( ) ;
107- document_type_layout( cx, def_id)
108- }
109- item_template_methods!( $( $rest) * ) ;
110- } ;
111- ( render_assoc_items $( $rest: tt) * ) => {
112- fn render_assoc_items( & self ) -> impl fmt:: Display {
113- let ( item, cx) = self . item_and_cx( ) ;
114- let def_id = item. item_id. expect_def_id( ) ;
115- render_assoc_items( cx, item, def_id, AssocItemRender :: All )
116- }
117- item_template_methods!( $( $rest) * ) ;
118- } ;
119- ( $method: ident $( $rest: tt) * ) => {
120- compile_error!( concat!( "unknown method: " , stringify!( $method) ) ) ;
121- } ;
122- ( $token: tt $( $rest: tt) * ) => {
123- compile_error!( concat!( "unexpected token: " , stringify!( $token) ) ) ;
124- } ;
125- }
126-
12743const ITEM_TABLE_OPEN : & str = "<dl class=\" item-table\" >" ;
12844const REEXPORTS_TABLE_OPEN : & str = "<dl class=\" item-table reexports\" >" ;
12945const ITEM_TABLE_CLOSE : & str = "</dl>" ;
@@ -300,10 +216,6 @@ fn toggle_close(mut w: impl fmt::Write) {
300216 w. write_str ( "</details>" ) . unwrap ( ) ;
301217}
302218
303- trait ItemTemplate < ' a , ' cx : ' a > : askama:: Template + Display {
304- fn item_and_cx ( & self ) -> ( & ' a clean:: Item , & ' a Context < ' cx > ) ;
305- }
306-
307219fn item_module ( cx : & Context < ' _ > , item : & clean:: Item , items : & [ clean:: Item ] ) -> impl fmt:: Display {
308220 fmt:: from_fn ( |w| {
309221 write ! ( w, "{}" , document( cx, item, None , HeadingOffset :: H2 ) ) ?;
@@ -1482,20 +1394,32 @@ fn item_type_alias(cx: &Context<'_>, it: &clean::Item, t: &clean::TypeAlias) ->
14821394 } )
14831395}
14841396
1485- item_template ! (
1486- #[ template( path = "item_union.html" ) ]
1487- struct ItemUnion <' a, ' cx> {
1488- cx: & ' a Context <' cx>,
1489- it: & ' a clean:: Item ,
1490- fields: & ' a [ clean:: Item ] ,
1491- generics: & ' a clean:: Generics ,
1492- is_type_alias: bool ,
1493- def_id: DefId ,
1494- } ,
1495- methods = [ document, document_type_layout, render_assoc_items]
1496- ) ;
1397+ #[ derive( Template ) ]
1398+ #[ template( path = "item_union.html" ) ]
1399+ struct ItemUnion < ' a , ' cx > {
1400+ cx : & ' a Context < ' cx > ,
1401+ it : & ' a clean:: Item ,
1402+ fields : & ' a [ clean:: Item ] ,
1403+ generics : & ' a clean:: Generics ,
1404+ is_type_alias : bool ,
1405+ def_id : DefId ,
1406+ }
14971407
14981408impl < ' a , ' cx : ' a > ItemUnion < ' a , ' cx > {
1409+ fn document ( & self ) -> impl fmt:: Display {
1410+ document ( self . cx , self . it , None , HeadingOffset :: H2 )
1411+ }
1412+
1413+ fn document_type_layout ( & self ) -> impl fmt:: Display {
1414+ let def_id = self . it . item_id . expect_def_id ( ) ;
1415+ document_type_layout ( self . cx , def_id)
1416+ }
1417+
1418+ fn render_assoc_items ( & self ) -> impl fmt:: Display {
1419+ let def_id = self . it . item_id . expect_def_id ( ) ;
1420+ render_assoc_items ( self . cx , self . it , def_id, AssocItemRender :: All )
1421+ }
1422+
14991423 fn render_union ( & self ) -> impl Display {
15001424 render_union (
15011425 self . it ,
0 commit comments