Exposed the sections inside a book (#642)

This commit is contained in:
Michael Bryan 2018-03-14 23:47:17 +08:00 committed by GitHub
parent 07719a8e0e
commit 4af155e963
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 33 additions and 13 deletions

View File

@ -71,7 +71,8 @@ fn create_missing(src_dir: &Path, summary: &Summary) -> Result<()> {
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)] #[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct Book { pub struct Book {
/// The sections in this book. /// The sections in this book.
sections: Vec<BookItem>, pub sections: Vec<BookItem>,
__non_exhaustive: (),
} }
impl Book { impl Book {
@ -158,9 +159,12 @@ pub struct Chapter {
impl Chapter { impl Chapter {
/// Create a new chapter with the provided content. /// Create a new chapter with the provided content.
pub fn new<P: Into<PathBuf>>(name: &str, content: String, path: P, parent_names: Vec<String>) pub fn new<P: Into<PathBuf>>(
-> Chapter name: &str,
{ content: String,
path: P,
parent_names: Vec<String>,
) -> Chapter {
Chapter { Chapter {
name: name.to_string(), name: name.to_string(),
content: content, content: content,
@ -192,23 +196,30 @@ fn load_book_from_disk<P: AsRef<Path>>(summary: &Summary, src_dir: P) -> Result<
chapters.push(chapter); chapters.push(chapter);
} }
Ok(Book { sections: chapters }) Ok(Book {
sections: chapters,
__non_exhaustive: (),
})
} }
fn load_summary_item<P: AsRef<Path>>(item: &SummaryItem, src_dir: P, parent_names: Vec<String>) fn load_summary_item<P: AsRef<Path>>(
-> Result<BookItem> item: &SummaryItem,
{ src_dir: P,
parent_names: Vec<String>,
) -> Result<BookItem> {
match *item { match *item {
SummaryItem::Separator => Ok(BookItem::Separator), SummaryItem::Separator => Ok(BookItem::Separator),
SummaryItem::Link(ref link) => { SummaryItem::Link(ref link) => {
load_chapter(link, src_dir, parent_names).map(|c| BookItem::Chapter(c)) load_chapter(link, src_dir, parent_names).map(|c| BookItem::Chapter(c))
}, }
} }
} }
fn load_chapter<P: AsRef<Path>>(link: &Link, src_dir: P, parent_names: Vec<String>) fn load_chapter<P: AsRef<Path>>(
-> Result<Chapter> link: &Link,
{ src_dir: P,
parent_names: Vec<String>,
) -> Result<Chapter> {
debug!("Loading {} ({})", link.name, link.location.display()); debug!("Loading {} ({})", link.name, link.location.display());
let src_dir = src_dir.as_ref(); let src_dir = src_dir.as_ref();
@ -337,7 +348,12 @@ And here is some \
#[test] #[test]
fn load_a_single_chapter_from_disk() { fn load_a_single_chapter_from_disk() {
let (link, temp_dir) = dummy_link(); 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(); let got = load_chapter(&link, temp_dir.path(), Vec::new()).unwrap();
assert_eq!(got, should_be); assert_eq!(got, should_be);
@ -396,6 +412,7 @@ And here is some \
..Default::default() ..Default::default()
}), }),
], ],
..Default::default()
}; };
let got = load_book_from_disk(&summary, temp.path()).unwrap(); let got = load_book_from_disk(&summary, temp.path()).unwrap();
@ -414,6 +431,7 @@ And here is some \
}), }),
BookItem::Separator, BookItem::Separator,
], ],
..Default::default()
}; };
let should_be: Vec<_> = book.sections.iter().collect(); let should_be: Vec<_> = book.sections.iter().collect();
@ -451,6 +469,7 @@ And here is some \
}), }),
BookItem::Separator, BookItem::Separator,
], ],
..Default::default()
}; };
let got: Vec<_> = book.iter().collect(); let got: Vec<_> = book.iter().collect();
@ -501,6 +520,7 @@ And here is some \
}), }),
BookItem::Separator, BookItem::Separator,
], ],
..Default::default()
}; };
let num_items = book.iter().count(); let num_items = book.iter().count();