@@ -241,9 +241,8 @@ impl<'a> SummaryParser<'a> {
241
241
242
242
match self . state {
243
243
State :: Begin => self . step_start ( next_event) ?,
244
- State :: PrefixChapters => self . step_prefix ( next_event) ?,
244
+ State :: PrefixChapters | State :: SuffixChapters => self . step_affix ( next_event) ?,
245
245
State :: NumberedChapters ( _) => self . step_numbered ( next_event) ?,
246
- State :: SuffixChapters => self . step_suffix ( next_event) ?,
247
246
State :: End => { } ,
248
247
}
249
248
} else {
@@ -266,31 +265,22 @@ impl<'a> SummaryParser<'a> {
266
265
Ok ( ( ) )
267
266
}
268
267
269
- /// In the second step we look out for links and horizontal rules to add
270
- /// to the prefix .
268
+ /// Try to step through an "affix" section (recognising prefix and suffix
269
+ /// chapters) .
271
270
///
272
- /// This state should only progress when it encounters a list. All other
273
- /// events will either be separators (horizontal rule), prefix chapters
274
- /// (the links), or skipped.
275
- fn step_prefix ( & mut self , event : Event < ' a > ) -> Result < ( ) > {
276
- match event {
277
- Event :: Start ( Tag :: Link ( location, _) ) => {
278
- let content = collect_events ! ( self . stream, Tag :: Link ( _, _) ) ;
279
- let text = stringify_events ( content) ;
280
- let link = Link :: new ( text, location. as_ref ( ) ) ;
271
+ /// If we encounter a link or horizontal line, it'll get added to the
272
+ /// section. If we encounter a list, we'll either change to
273
+ /// `State::NumberedChapter` (for prefix) or throw an error (suffix chapters).
274
+ ///
275
+ /// Anything else will be ignored.
276
+ fn step_affix ( & mut self , event : Event < ' a > ) -> Result < ( ) > {
281
277
282
- debug ! ( "[*] Found a prefix chapter: {:?}" , link. name) ;
283
- self . summary . prefix_chapters . push ( SummaryItem :: Link ( link) ) ;
284
- } ,
278
+ match event {
279
+ Event :: Start ( tag) => self . handle_start_tag_in_affix_chapter ( tag) ?,
285
280
Event :: End ( Tag :: Rule ) => {
286
- debug ! ( "[*] Found a prefix chapter separator" ) ;
287
- self . summary . prefix_chapters . push ( SummaryItem :: Separator ) ;
281
+ debug ! ( "[*] Found an affix chapter separator" ) ;
282
+ self . affix_chapter_list ( ) . push ( SummaryItem :: Separator ) ;
288
283
} ,
289
- Event :: Start ( Tag :: List ( _) ) => {
290
- debug ! ( "[*] Changing from prefix chapters to numbered chapters" ) ;
291
- self . state = State :: NumberedChapters ( 0 ) ;
292
- } ,
293
-
294
284
other => {
295
285
trace ! ( "[*] Skipping unexpected token in summary: {:?}" , other) ;
296
286
} ,
@@ -299,6 +289,42 @@ impl<'a> SummaryParser<'a> {
299
289
Ok ( ( ) )
300
290
}
301
291
292
+ /// A helper function to get the `SummaryItem` list we should add items to
293
+ /// when parsing an affix chapter (i.e. prefix or suffix chapters).
294
+ fn affix_chapter_list ( & mut self ) -> & mut Vec < SummaryItem > {
295
+ match self . state {
296
+ State :: PrefixChapters => & mut self . summary . prefix_chapters ,
297
+ State :: SuffixChapters => & mut self . summary . suffix_chapters ,
298
+ other => panic ! ( "affix_chapter_list() called with invalid state: {:?}" , other) ,
299
+ }
300
+ }
301
+
302
+ fn handle_start_tag_in_affix_chapter ( & mut self , tag : Tag ) -> Result < ( ) > {
303
+ match tag {
304
+ Tag :: Link ( location, _) => {
305
+ let content = collect_events ! ( self . stream, Tag :: Link ( _, _) ) ;
306
+ let text = stringify_events ( content) ;
307
+ let link = Link :: new ( text, location. as_ref ( ) ) ;
308
+
309
+ debug ! ( "[*] Found an affix chapter: {:?}" , link. name) ;
310
+ self . affix_chapter_list ( ) . push ( SummaryItem :: Link ( link) ) ;
311
+ } ,
312
+ Tag :: List ( _) => {
313
+ match self . state {
314
+ State :: PrefixChapters => {
315
+ debug ! ( "[*] Changing from prefix chapters to numbered chapters" ) ;
316
+ self . state = State :: NumberedChapters ( 0 ) ;
317
+ } ,
318
+ State :: SuffixChapters => bail ! ( "Suffix chapters can't be followed by a list" ) ,
319
+ _ => unreachable ! ( ) ,
320
+ }
321
+ } ,
322
+ other => trace ! ( "[*] Skipping unknown start tag while parsing affix chapters: {:?}" , other) ,
323
+ }
324
+
325
+ Ok ( ( ) )
326
+ }
327
+
302
328
/// Parse the numbered chapters.
303
329
///
304
330
/// If the event is the start of a list item, consume the entire item and
@@ -352,30 +378,6 @@ impl<'a> SummaryParser<'a> {
352
378
Ok ( ( ) )
353
379
}
354
380
355
- fn step_suffix ( & mut self , event : Event < ' a > ) -> Result < ( ) > {
356
- // FIXME: This has been copy/pasted from step_prefix. make DRY.
357
- match event {
358
- Event :: Start ( Tag :: Link ( location, _) ) => {
359
- let content = collect_events ! ( self . stream, Tag :: Link ( _, _) ) ;
360
- let text = stringify_events ( content) ;
361
- let link = Link :: new ( text, location. as_ref ( ) ) ;
362
-
363
- debug ! ( "[*] Found a suffix chapter: {:?}" , link. name) ;
364
- self . summary . suffix_chapters . push ( SummaryItem :: Link ( link) ) ;
365
- } ,
366
- Event :: End ( Tag :: Rule ) => {
367
- debug ! ( "[*] Found a suffix chapter separator" ) ;
368
- self . summary . suffix_chapters . push ( SummaryItem :: Separator ) ;
369
- } ,
370
- other => {
371
- trace ! ( "[*] Skipping unexpected token in summary: {:?}" , other) ;
372
- } ,
373
- }
374
-
375
- Ok ( ( ) )
376
- }
377
-
378
-
379
381
/// Parse a single item (`[Some Chapter Name](./path/to/chapter.md)`).
380
382
fn parse_item ( & mut self ) -> Result < Link > {
381
383
let next = self . stream . next ( ) ;
@@ -493,7 +495,7 @@ impl Display for SectionNumber {
493
495
fn fmt ( & self , f : & mut Formatter ) -> fmt:: Result {
494
496
let dotted_number: String = self . 0
495
497
. iter ( )
496
- . map ( |i| format ! ( "{}" , i ) )
498
+ . map ( |i| i . to_string ( ) )
497
499
. collect :: < Vec < String > > ( )
498
500
. join ( "." ) ;
499
501
@@ -527,10 +529,8 @@ mod tests {
527
529
] ;
528
530
529
531
for ( input, should_be) in inputs {
530
- let section_number = SectionNumber ( input) ;
531
- let string_repr = format ! ( "{}" , section_number) ;
532
-
533
- assert_eq ! ( string_repr, should_be) ;
532
+ let section_number = SectionNumber ( input) . to_string ( ) ;
533
+ assert_eq ! ( section_number, should_be) ;
534
534
}
535
535
}
536
536
0 commit comments