Skip to content

Commit 227f406

Browse files
Reduced some of the code duplication
1 parent e89e6a0 commit 227f406

File tree

1 file changed

+52
-52
lines changed

1 file changed

+52
-52
lines changed

src/book/summary.rs

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,8 @@ impl<'a> SummaryParser<'a> {
241241

242242
match self.state {
243243
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)?,
245245
State::NumberedChapters(_) => self.step_numbered(next_event)?,
246-
State::SuffixChapters => self.step_suffix(next_event)?,
247246
State::End => {},
248247
}
249248
} else {
@@ -266,31 +265,22 @@ impl<'a> SummaryParser<'a> {
266265
Ok(())
267266
}
268267

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).
271270
///
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<()> {
281277

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)?,
285280
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);
288283
},
289-
Event::Start(Tag::List(_)) => {
290-
debug!("[*] Changing from prefix chapters to numbered chapters");
291-
self.state = State::NumberedChapters(0);
292-
},
293-
294284
other => {
295285
trace!("[*] Skipping unexpected token in summary: {:?}", other);
296286
},
@@ -299,6 +289,42 @@ impl<'a> SummaryParser<'a> {
299289
Ok(())
300290
}
301291

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+
302328
/// Parse the numbered chapters.
303329
///
304330
/// If the event is the start of a list item, consume the entire item and
@@ -352,30 +378,6 @@ impl<'a> SummaryParser<'a> {
352378
Ok(())
353379
}
354380

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-
379381
/// Parse a single item (`[Some Chapter Name](./path/to/chapter.md)`).
380382
fn parse_item(&mut self) -> Result<Link> {
381383
let next = self.stream.next();
@@ -493,7 +495,7 @@ impl Display for SectionNumber {
493495
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
494496
let dotted_number: String = self.0
495497
.iter()
496-
.map(|i| format!("{}", i))
498+
.map(|i| i.to_string())
497499
.collect::<Vec<String>>()
498500
.join(".");
499501

@@ -527,10 +529,8 @@ mod tests {
527529
];
528530

529531
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);
534534
}
535535
}
536536

0 commit comments

Comments
 (0)