diff --git a/src/book/book.rs b/src/book/book.rs index 7dc0d110..8af70e9a 100644 --- a/src/book/book.rs +++ b/src/book/book.rs @@ -31,12 +31,7 @@ fn create_missing(src_dir: &Path, summary: &Summary) -> Result<()> { let mut items: Vec<_> = summary .prefix_chapters .iter() - .chain( - summary - .parts - .iter() - .flat_map(|part| part.numbered_chapters.iter()), - ) + .chain(summary.numbered_chapters.iter()) .chain(summary.suffix_chapters.iter()) .collect(); @@ -212,24 +207,17 @@ pub(crate) fn load_book_from_disk>(summary: &Summary, src_dir: P) debug!("Loading the book from disk"); let src_dir = src_dir.as_ref(); + let prefix = summary.prefix_chapters.iter(); + let numbered = summary.numbered_chapters.iter(); + let suffix = summary.suffix_chapters.iter(); + + let summary_items = prefix.chain(numbered).chain(suffix); + let mut chapters = Vec::new(); - for prefix_chapter in &summary.prefix_chapters { - chapters.push(load_summary_item(prefix_chapter, src_dir, Vec::new())?); - } - - for part in &summary.parts { - if let Some(title) = &part.title { - chapters.push(BookItem::PartTitle(title.clone())); - } - - for numbered_chapter in &part.numbered_chapters { - chapters.push(load_summary_item(numbered_chapter, src_dir, Vec::new())?); - } - } - - for suffix_chapter in &summary.suffix_chapters { - chapters.push(load_summary_item(suffix_chapter, src_dir, Vec::new())?); + for summary_item in summary_items { + let chapter = load_summary_item(summary_item, src_dir, Vec::new())?; + chapters.push(chapter); } Ok(Book { @@ -243,11 +231,12 @@ fn load_summary_item + Clone>( src_dir: P, parent_names: Vec, ) -> Result { - match *item { + match item { SummaryItem::Separator => Ok(BookItem::Separator), SummaryItem::Link(ref link) => { load_chapter(link, src_dir, parent_names).map(BookItem::Chapter) } + SummaryItem::PartTitle(title) => Ok(BookItem::PartTitle(title.clone())), } } @@ -341,7 +330,6 @@ impl Display for Chapter { #[cfg(test)] mod tests { use super::*; - use crate::book::summary::Part; use std::io::Write; use tempfile::{Builder as TempFileBuilder, TempDir}; @@ -445,10 +433,7 @@ And here is some \ fn load_a_book_with_a_single_chapter() { let (link, temp) = dummy_link(); let summary = Summary { - parts: vec![Part { - title: None, - numbered_chapters: vec![SummaryItem::Link(link)], - }], + numbered_chapters: vec![SummaryItem::Link(link)], ..Default::default() }; let should_be = Book { @@ -582,14 +567,12 @@ And here is some \ fn cant_load_chapters_with_an_empty_path() { let (_, temp) = dummy_link(); let summary = Summary { - parts: vec![Part { - title: None, - numbered_chapters: vec![SummaryItem::Link(Link { - name: String::from("Empty"), - location: Some(PathBuf::from("")), - ..Default::default() - })], - }], + numbered_chapters: vec![SummaryItem::Link(Link { + name: String::from("Empty"), + location: Some(PathBuf::from("")), + ..Default::default() + })], + ..Default::default() }; @@ -604,14 +587,11 @@ And here is some \ fs::create_dir(&dir).unwrap(); let summary = Summary { - parts: vec![Part { - title: None, - numbered_chapters: vec![SummaryItem::Link(Link { - name: String::from("nested"), - location: Some(dir), - ..Default::default() - })], - }], + numbered_chapters: vec![SummaryItem::Link(Link { + name: String::from("nested"), + location: Some(dir), + ..Default::default() + })], ..Default::default() }; diff --git a/src/book/summary.rs b/src/book/summary.rs index fb1dece0..12fb2cd6 100644 --- a/src/book/summary.rs +++ b/src/book/summary.rs @@ -61,22 +61,11 @@ pub struct Summary { /// Chapters before the main text (e.g. an introduction). pub prefix_chapters: Vec, /// The main numbered chapters of the book, broken into one or more possibly named parts. - pub parts: Vec, + pub numbered_chapters: Vec, /// Items which come after the main document (e.g. a conclusion). pub suffix_chapters: Vec, } -/// A struct representing a "part" in the `SUMMARY.md`. This is a possibly-titled section with -/// numbered chapters in it. -#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)] -pub struct Part { - /// An optional title for the `SUMMARY.md`, currently just ignored. - pub title: Option, - - /// The main chapters in the document. - pub numbered_chapters: Vec, -} - /// A struct representing an entry in the `SUMMARY.md`, possibly with nested /// entries. /// @@ -124,6 +113,8 @@ pub enum SummaryItem { Link(Link), /// A separator (`---`). Separator, + /// A part title. + PartTitle(String), } impl SummaryItem { @@ -246,7 +237,7 @@ impl<'a> SummaryParser<'a> { let prefix_chapters = self .parse_affix(true) .chain_err(|| "There was an error parsing the prefix chapters")?; - let parts = self + let numbered_chapters = self .parse_parts() .chain_err(|| "There was an error parsing the numbered chapters")?; let suffix_chapters = self @@ -256,7 +247,7 @@ impl<'a> SummaryParser<'a> { Ok(Summary { title, prefix_chapters, - parts, + numbered_chapters, suffix_chapters, }) } @@ -295,7 +286,7 @@ impl<'a> SummaryParser<'a> { Ok(items) } - fn parse_parts(&mut self) -> Result> { + fn parse_parts(&mut self) -> Result> { let mut parts = vec![]; // We want the section numbers to be continues through all parts. @@ -331,10 +322,10 @@ impl<'a> SummaryParser<'a> { .parse_numbered(&mut root_items, &mut root_number) .chain_err(|| "There was an error parsing the numbered chapters")?; - parts.push(Part { - title, - numbered_chapters, - }); + if let Some(title) = title { + parts.push(SummaryItem::PartTitle(title)); + } + parts.extend(numbered_chapters); } Ok(parts) @@ -817,37 +808,30 @@ mod tests { # Title 2\n- [Third](./third.md)\n\t- [Fourth](./fourth.md)"; let should_be = vec![ - Part { - title: None, - numbered_chapters: vec![ - SummaryItem::Link(Link { - name: String::from("First"), - location: Some(PathBuf::from("./first.md")), - number: Some(SectionNumber(vec![1])), - nested_items: Vec::new(), - }), - SummaryItem::Link(Link { - name: String::from("Second"), - location: Some(PathBuf::from("./second.md")), - number: Some(SectionNumber(vec![2])), - nested_items: Vec::new(), - }), - ], - }, - Part { - title: Some(String::from("Title 2")), - numbered_chapters: vec![SummaryItem::Link(Link { - name: String::from("Third"), - location: Some(PathBuf::from("./third.md")), - number: Some(SectionNumber(vec![3])), - nested_items: vec![SummaryItem::Link(Link { - name: String::from("Fourth"), - location: Some(PathBuf::from("./fourth.md")), - number: Some(SectionNumber(vec![3, 1])), - nested_items: Vec::new(), - })], + SummaryItem::Link(Link { + name: String::from("First"), + location: Some(PathBuf::from("./first.md")), + number: Some(SectionNumber(vec![1])), + nested_items: Vec::new(), + }), + SummaryItem::Link(Link { + name: String::from("Second"), + location: Some(PathBuf::from("./second.md")), + number: Some(SectionNumber(vec![2])), + nested_items: Vec::new(), + }), + SummaryItem::PartTitle(String::from("Title 2")), + SummaryItem::Link(Link { + name: String::from("Third"), + location: Some(PathBuf::from("./third.md")), + number: Some(SectionNumber(vec![3])), + nested_items: vec![SummaryItem::Link(Link { + name: String::from("Fourth"), + location: Some(PathBuf::from("./fourth.md")), + number: Some(SectionNumber(vec![3, 1])), + nested_items: Vec::new(), })], - }, + }), ]; let mut parser = SummaryParser::new(src);