1
1
//! Table-of-contents creation.
2
2
3
- use std:: fmt;
4
- use std:: string:: String ;
5
-
6
3
/// A (recursive) table of contents
7
- #[ derive( PartialEq ) ]
4
+ #[ derive( Debug , PartialEq ) ]
8
5
pub struct Toc {
9
6
/// The levels are strictly decreasing, i.e.
10
7
///
@@ -28,7 +25,7 @@ impl Toc {
28
25
}
29
26
}
30
27
31
- #[ derive( PartialEq ) ]
28
+ #[ derive( Debug , PartialEq ) ]
32
29
pub struct TocEntry {
33
30
level : u32 ,
34
31
sec_number : String ,
@@ -165,25 +162,23 @@ impl TocBuilder {
165
162
}
166
163
}
167
164
168
- impl fmt:: Debug for Toc {
169
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
170
- fmt:: Display :: fmt ( self , f)
171
- }
172
- }
173
-
174
- impl fmt:: Display for Toc {
175
- fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
176
- write ! ( fmt, "<ul>" ) ?;
165
+ impl Toc {
166
+ fn print_inner ( & self , v : & mut String ) {
167
+ v. push_str ( "<ul>" ) ;
177
168
for entry in & self . entries {
178
- // recursively format this table of contents (the
179
- // `{children}` is the key).
180
- write ! ( fmt,
181
- "\n <li><a href=\" #{id}\" >{num} {name}</a>{children}</li>" ,
169
+ // recursively format this table of contents
170
+ v. push_str ( & format ! ( "\n <li><a href=\" #{id}\" >{num} {name}</a>" ,
182
171
id = entry. id,
183
- num = entry. sec_number, name = entry. name,
184
- children = entry. children) ?
172
+ num = entry. sec_number, name = entry. name) ) ;
173
+ entry. children . print_inner ( & mut * v) ;
174
+ v. push_str ( "</li>" ) ;
185
175
}
186
- write ! ( fmt, "</ul>" )
176
+ v. push_str ( "</ul>" ) ;
177
+ }
178
+ crate fn print ( & self ) -> String {
179
+ let mut v = String :: new ( ) ;
180
+ self . print_inner ( & mut v) ;
181
+ v
187
182
}
188
183
}
189
184
0 commit comments