From 4af155e96346dc9583ec71c07285ce6ad4d7ec05 Mon Sep 17 00:00:00 2001 From: Michael Bryan Date: Wed, 14 Mar 2018 23:47:17 +0800 Subject: [PATCH] Exposed the sections inside a book (#642) --- src/book/book.rs | 46 +++++++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/src/book/book.rs b/src/book/book.rs index b8e61478..5987b8b5 100644 --- a/src/book/book.rs +++ b/src/book/book.rs @@ -71,7 +71,8 @@ fn create_missing(src_dir: &Path, summary: &Summary) -> Result<()> { #[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)] pub struct Book { /// The sections in this book. - sections: Vec, + pub sections: Vec, + __non_exhaustive: (), } impl Book { @@ -158,9 +159,12 @@ pub struct Chapter { impl Chapter { /// Create a new chapter with the provided content. - pub fn new>(name: &str, content: String, path: P, parent_names: Vec) - -> Chapter - { + pub fn new>( + name: &str, + content: String, + path: P, + parent_names: Vec, + ) -> Chapter { Chapter { name: name.to_string(), content: content, @@ -192,23 +196,30 @@ fn load_book_from_disk>(summary: &Summary, src_dir: P) -> Result< chapters.push(chapter); } - Ok(Book { sections: chapters }) + Ok(Book { + sections: chapters, + __non_exhaustive: (), + }) } -fn load_summary_item>(item: &SummaryItem, src_dir: P, parent_names: Vec) - -> Result -{ +fn load_summary_item>( + item: &SummaryItem, + src_dir: P, + parent_names: Vec, +) -> Result { match *item { SummaryItem::Separator => Ok(BookItem::Separator), SummaryItem::Link(ref link) => { load_chapter(link, src_dir, parent_names).map(|c| BookItem::Chapter(c)) - }, + } } } -fn load_chapter>(link: &Link, src_dir: P, parent_names: Vec) - -> Result -{ +fn load_chapter>( + link: &Link, + src_dir: P, + parent_names: Vec, +) -> Result { debug!("Loading {} ({})", link.name, link.location.display()); let src_dir = src_dir.as_ref(); @@ -337,7 +348,12 @@ And here is some \ #[test] fn load_a_single_chapter_from_disk() { let (link, temp_dir) = dummy_link(); - let should_be = Chapter::new("Chapter 1", DUMMY_SRC.to_string(), "chapter_1.md", Vec::new()); + let should_be = Chapter::new( + "Chapter 1", + DUMMY_SRC.to_string(), + "chapter_1.md", + Vec::new(), + ); let got = load_chapter(&link, temp_dir.path(), Vec::new()).unwrap(); assert_eq!(got, should_be); @@ -396,6 +412,7 @@ And here is some \ ..Default::default() }), ], + ..Default::default() }; let got = load_book_from_disk(&summary, temp.path()).unwrap(); @@ -414,6 +431,7 @@ And here is some \ }), BookItem::Separator, ], + ..Default::default() }; let should_be: Vec<_> = book.sections.iter().collect(); @@ -451,6 +469,7 @@ And here is some \ }), BookItem::Separator, ], + ..Default::default() }; let got: Vec<_> = book.iter().collect(); @@ -501,6 +520,7 @@ And here is some \ }), BookItem::Separator, ], + ..Default::default() }; let num_items = book.iter().count();