2017-11-18 19:50:47 +08:00
|
|
|
use std::collections::VecDeque;
|
2018-07-24 01:45:01 +08:00
|
|
|
use std::fmt::{self, Display, Formatter};
|
2017-12-11 14:29:32 +08:00
|
|
|
use std::fs::{self, File};
|
2017-12-11 07:32:35 +08:00
|
|
|
use std::io::{Read, Write};
|
2018-07-24 01:45:01 +08:00
|
|
|
use std::path::{Path, PathBuf};
|
2017-11-18 19:50:47 +08:00
|
|
|
|
2017-11-18 20:01:50 +08:00
|
|
|
use super::summary::{parse_summary, Link, SectionNumber, Summary, SummaryItem};
|
2019-05-26 02:50:41 +08:00
|
|
|
use crate::config::BuildConfig;
|
|
|
|
use crate::errors::*;
|
2017-11-18 19:50:47 +08:00
|
|
|
|
|
|
|
/// Load a book into memory from its `src/` directory.
|
2017-12-11 07:32:35 +08:00
|
|
|
pub fn load_book<P: AsRef<Path>>(src_dir: P, cfg: &BuildConfig) -> Result<Book> {
|
2017-11-18 19:50:47 +08:00
|
|
|
let src_dir = src_dir.as_ref();
|
|
|
|
let summary_md = src_dir.join("SUMMARY.md");
|
|
|
|
|
|
|
|
let mut summary_content = String::new();
|
2017-12-11 07:32:35 +08:00
|
|
|
File::open(summary_md)
|
2020-05-21 05:32:00 +08:00
|
|
|
.with_context(|| "Couldn't open SUMMARY.md")?
|
2017-12-11 07:32:35 +08:00
|
|
|
.read_to_string(&mut summary_content)?;
|
2017-11-18 19:50:47 +08:00
|
|
|
|
2020-05-21 05:32:00 +08:00
|
|
|
let summary = parse_summary(&summary_content).with_context(|| "Summary parsing failed")?;
|
2017-11-18 19:50:47 +08:00
|
|
|
|
2017-12-11 07:32:35 +08:00
|
|
|
if cfg.create_missing {
|
2020-05-21 05:32:00 +08:00
|
|
|
create_missing(&src_dir, &summary).with_context(|| "Unable to create missing chapters")?;
|
2017-12-11 07:32:35 +08:00
|
|
|
}
|
|
|
|
|
2017-11-18 20:01:50 +08:00
|
|
|
load_book_from_disk(&summary, src_dir)
|
2017-11-18 19:50:47 +08:00
|
|
|
}
|
|
|
|
|
2017-12-11 07:32:35 +08:00
|
|
|
fn create_missing(src_dir: &Path, summary: &Summary) -> Result<()> {
|
|
|
|
let mut items: Vec<_> = summary
|
|
|
|
.prefix_chapters
|
|
|
|
.iter()
|
2020-05-19 00:18:14 +08:00
|
|
|
.chain(summary.numbered_chapters.iter())
|
2017-12-11 07:32:35 +08:00
|
|
|
.chain(summary.suffix_chapters.iter())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
while !items.is_empty() {
|
|
|
|
let next = items.pop().expect("already checked");
|
|
|
|
|
|
|
|
if let SummaryItem::Link(ref link) = *next {
|
2020-03-01 00:55:45 +08:00
|
|
|
if let Some(ref location) = link.location {
|
|
|
|
let filename = src_dir.join(location);
|
|
|
|
if !filename.exists() {
|
|
|
|
if let Some(parent) = filename.parent() {
|
|
|
|
if !parent.exists() {
|
|
|
|
fs::create_dir_all(parent)?;
|
|
|
|
}
|
2017-12-11 14:29:32 +08:00
|
|
|
}
|
2020-03-01 00:55:45 +08:00
|
|
|
debug!("Creating missing file {}", filename.display());
|
2017-12-11 07:32:35 +08:00
|
|
|
|
2020-03-01 00:55:45 +08:00
|
|
|
let mut f = File::create(&filename)?;
|
|
|
|
writeln!(f, "# {}", link.name)?;
|
|
|
|
}
|
2017-12-11 07:32:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
items.extend(&link.nested_items);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-11-18 19:50:47 +08:00
|
|
|
/// A dumb tree structure representing a book.
|
|
|
|
///
|
2018-01-22 20:47:29 +08:00
|
|
|
/// For the moment a book is just a collection of `BookItems` which are
|
2018-01-22 06:44:28 +08:00
|
|
|
/// accessible by either iterating (immutably) over the book with [`iter()`], or
|
|
|
|
/// recursively applying a closure to each section to mutate the chapters, using
|
|
|
|
/// [`for_each_mut()`].
|
2018-01-22 20:47:29 +08:00
|
|
|
///
|
2018-01-22 06:44:28 +08:00
|
|
|
/// [`iter()`]: #method.iter
|
|
|
|
/// [`for_each_mut()`]: #method.for_each_mut
|
2017-11-18 19:50:47 +08:00
|
|
|
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub struct Book {
|
|
|
|
/// The sections in this book.
|
2018-03-14 23:47:17 +08:00
|
|
|
pub sections: Vec<BookItem>,
|
|
|
|
__non_exhaustive: (),
|
2017-11-18 19:50:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Book {
|
|
|
|
/// Create an empty book.
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Default::default()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a depth-first iterator over the items in the book.
|
2019-05-07 04:50:34 +08:00
|
|
|
pub fn iter(&self) -> BookItems<'_> {
|
2017-11-18 20:01:50 +08:00
|
|
|
BookItems {
|
|
|
|
items: self.sections.iter().collect(),
|
|
|
|
}
|
2017-11-18 19:50:47 +08:00
|
|
|
}
|
2018-01-22 06:44:28 +08:00
|
|
|
|
|
|
|
/// Recursively apply a closure to each item in the book, allowing you to
|
|
|
|
/// mutate them.
|
|
|
|
///
|
|
|
|
/// # Note
|
|
|
|
///
|
|
|
|
/// Unlike the `iter()` method, this requires a closure instead of returning
|
|
|
|
/// an iterator. This is because using iterators can possibly allow you
|
|
|
|
/// to have iterator invalidation errors.
|
|
|
|
pub fn for_each_mut<F>(&mut self, mut func: F)
|
|
|
|
where
|
|
|
|
F: FnMut(&mut BookItem),
|
|
|
|
{
|
|
|
|
for_each_mut(&mut func, &mut self.sections);
|
|
|
|
}
|
2018-01-26 01:11:48 +08:00
|
|
|
|
|
|
|
/// Append a `BookItem` to the `Book`.
|
|
|
|
pub fn push_item<I: Into<BookItem>>(&mut self, item: I) -> &mut Self {
|
|
|
|
self.sections.push(item.into());
|
|
|
|
self
|
|
|
|
}
|
2018-01-22 06:44:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn for_each_mut<'a, F, I>(func: &mut F, items: I)
|
|
|
|
where
|
|
|
|
F: FnMut(&mut BookItem),
|
|
|
|
I: IntoIterator<Item = &'a mut BookItem>,
|
|
|
|
{
|
|
|
|
for item in items {
|
2019-05-07 02:20:58 +08:00
|
|
|
if let BookItem::Chapter(ch) = item {
|
2018-01-22 06:44:28 +08:00
|
|
|
for_each_mut(func, &mut ch.sub_items);
|
|
|
|
}
|
|
|
|
|
|
|
|
func(item);
|
|
|
|
}
|
2017-11-18 19:50:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Enum representing any type of item which can be added to a book.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub enum BookItem {
|
|
|
|
/// A nested chapter.
|
|
|
|
Chapter(Chapter),
|
|
|
|
/// A section separator.
|
|
|
|
Separator,
|
2020-03-21 10:18:07 +08:00
|
|
|
/// A part title.
|
|
|
|
PartTitle(String),
|
2017-11-18 19:50:47 +08:00
|
|
|
}
|
|
|
|
|
2018-01-26 01:11:48 +08:00
|
|
|
impl From<Chapter> for BookItem {
|
|
|
|
fn from(other: Chapter) -> BookItem {
|
|
|
|
BookItem::Chapter(other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-18 19:50:47 +08:00
|
|
|
/// The representation of a "chapter", usually mapping to a single file on
|
|
|
|
/// disk however it may contain multiple sub-chapters.
|
|
|
|
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub struct Chapter {
|
|
|
|
/// The chapter's name.
|
|
|
|
pub name: String,
|
|
|
|
/// The chapter's contents.
|
|
|
|
pub content: String,
|
|
|
|
/// The chapter's section number, if it has one.
|
|
|
|
pub number: Option<SectionNumber>,
|
|
|
|
/// Nested items.
|
|
|
|
pub sub_items: Vec<BookItem>,
|
|
|
|
/// The chapter's location, relative to the `SUMMARY.md` file.
|
2020-03-01 00:55:45 +08:00
|
|
|
pub path: Option<PathBuf>,
|
2018-03-07 21:02:06 +08:00
|
|
|
/// An ordered list of the names of each chapter above this one, in the hierarchy.
|
|
|
|
pub parent_names: Vec<String>,
|
2017-11-18 19:50:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Chapter {
|
|
|
|
/// Create a new chapter with the provided content.
|
2018-03-14 23:47:17 +08:00
|
|
|
pub fn new<P: Into<PathBuf>>(
|
|
|
|
name: &str,
|
|
|
|
content: String,
|
|
|
|
path: P,
|
|
|
|
parent_names: Vec<String>,
|
|
|
|
) -> Chapter {
|
2017-11-18 19:50:47 +08:00
|
|
|
Chapter {
|
|
|
|
name: name.to_string(),
|
2018-12-04 07:10:09 +08:00
|
|
|
content,
|
2020-03-01 00:55:45 +08:00
|
|
|
path: Some(path.into()),
|
|
|
|
parent_names,
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new draft chapter that is not attached to a source markdown file and has
|
|
|
|
/// thus no content.
|
|
|
|
pub fn new_draft(name: &str, parent_names: Vec<String>) -> Self {
|
|
|
|
Chapter {
|
|
|
|
name: name.to_string(),
|
|
|
|
content: String::new(),
|
|
|
|
path: None,
|
2018-12-04 07:10:09 +08:00
|
|
|
parent_names,
|
2017-11-18 19:50:47 +08:00
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
2020-03-01 00:55:45 +08:00
|
|
|
|
|
|
|
/// Check if the chapter is a draft chapter, meaning it has no path to a source markdown file
|
|
|
|
pub fn is_draft_chapter(&self) -> bool {
|
|
|
|
match self.path {
|
|
|
|
Some(_) => false,
|
|
|
|
None => true,
|
|
|
|
}
|
|
|
|
}
|
2017-11-18 19:50:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Use the provided `Summary` to load a `Book` from disk.
|
|
|
|
///
|
|
|
|
/// You need to pass in the book's source directory because all the links in
|
|
|
|
/// `SUMMARY.md` give the chapter locations relative to it.
|
2019-03-05 03:44:00 +08:00
|
|
|
pub(crate) fn load_book_from_disk<P: AsRef<Path>>(summary: &Summary, src_dir: P) -> Result<Book> {
|
2018-01-23 01:28:37 +08:00
|
|
|
debug!("Loading the book from disk");
|
2017-11-18 19:50:47 +08:00
|
|
|
let src_dir = src_dir.as_ref();
|
|
|
|
|
2020-05-19 00:18:14 +08:00
|
|
|
let prefix = summary.prefix_chapters.iter();
|
|
|
|
let numbered = summary.numbered_chapters.iter();
|
|
|
|
let suffix = summary.suffix_chapters.iter();
|
2017-11-18 19:50:47 +08:00
|
|
|
|
2020-05-19 00:18:14 +08:00
|
|
|
let summary_items = prefix.chain(numbered).chain(suffix);
|
2020-03-21 10:18:07 +08:00
|
|
|
|
2020-05-19 00:18:14 +08:00
|
|
|
let mut chapters = Vec::new();
|
2017-11-18 19:50:47 +08:00
|
|
|
|
2020-05-19 00:18:14 +08:00
|
|
|
for summary_item in summary_items {
|
|
|
|
let chapter = load_summary_item(summary_item, src_dir, Vec::new())?;
|
|
|
|
chapters.push(chapter);
|
2017-11-18 19:50:47 +08:00
|
|
|
}
|
|
|
|
|
2018-03-14 23:47:17 +08:00
|
|
|
Ok(Book {
|
|
|
|
sections: chapters,
|
|
|
|
__non_exhaustive: (),
|
|
|
|
})
|
2017-11-18 19:50:47 +08:00
|
|
|
}
|
|
|
|
|
2020-03-01 00:55:45 +08:00
|
|
|
fn load_summary_item<P: AsRef<Path> + Clone>(
|
2018-03-14 23:47:17 +08:00
|
|
|
item: &SummaryItem,
|
|
|
|
src_dir: P,
|
|
|
|
parent_names: Vec<String>,
|
|
|
|
) -> Result<BookItem> {
|
2020-05-19 00:18:14 +08:00
|
|
|
match item {
|
2017-11-18 19:50:47 +08:00
|
|
|
SummaryItem::Separator => Ok(BookItem::Separator),
|
2018-03-07 21:02:06 +08:00
|
|
|
SummaryItem::Link(ref link) => {
|
2018-12-04 07:10:09 +08:00
|
|
|
load_chapter(link, src_dir, parent_names).map(BookItem::Chapter)
|
2018-03-14 23:47:17 +08:00
|
|
|
}
|
2020-05-19 00:18:14 +08:00
|
|
|
SummaryItem::PartTitle(title) => Ok(BookItem::PartTitle(title.clone())),
|
2017-11-18 19:50:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-10 05:34:28 +08:00
|
|
|
fn load_chapter<P: AsRef<Path>>(
|
2018-03-14 23:47:17 +08:00
|
|
|
link: &Link,
|
|
|
|
src_dir: P,
|
|
|
|
parent_names: Vec<String>,
|
|
|
|
) -> Result<Chapter> {
|
2020-03-10 05:34:28 +08:00
|
|
|
let src_dir = src_dir.as_ref();
|
|
|
|
|
2020-03-01 00:55:45 +08:00
|
|
|
let mut ch = if let Some(ref link_location) = link.location {
|
|
|
|
debug!("Loading {} ({})", link.name, link_location.display());
|
|
|
|
|
|
|
|
let location = if link_location.is_absolute() {
|
|
|
|
link_location.clone()
|
|
|
|
} else {
|
|
|
|
src_dir.join(link_location)
|
|
|
|
};
|
2017-11-18 19:50:47 +08:00
|
|
|
|
2020-03-01 00:55:45 +08:00
|
|
|
let mut f = File::open(&location)
|
2020-05-21 05:32:00 +08:00
|
|
|
.with_context(|| format!("Chapter file not found, {}", link_location.display()))?;
|
2017-11-18 19:50:47 +08:00
|
|
|
|
2020-03-01 00:55:45 +08:00
|
|
|
let mut content = String::new();
|
2020-05-21 05:32:00 +08:00
|
|
|
f.read_to_string(&mut content).with_context(|| {
|
|
|
|
format!("Unable to read \"{}\" ({})", link.name, location.display())
|
|
|
|
})?;
|
2017-11-18 19:50:47 +08:00
|
|
|
|
2020-07-25 13:02:44 +08:00
|
|
|
if content.as_bytes().starts_with(b"\xef\xbb\xbf") {
|
2020-09-29 18:01:06 +08:00
|
|
|
content.replace_range(..3, "");
|
2020-07-25 13:02:44 +08:00
|
|
|
}
|
|
|
|
|
2020-03-01 00:55:45 +08:00
|
|
|
let stripped = location
|
|
|
|
.strip_prefix(&src_dir)
|
|
|
|
.expect("Chapters are always inside a book");
|
2017-11-18 19:50:47 +08:00
|
|
|
|
2020-03-01 00:55:45 +08:00
|
|
|
Chapter::new(&link.name, content, stripped, parent_names.clone())
|
|
|
|
} else {
|
|
|
|
Chapter::new_draft(&link.name, parent_names.clone())
|
|
|
|
};
|
2017-11-18 19:50:47 +08:00
|
|
|
|
2018-03-07 21:02:06 +08:00
|
|
|
let mut sub_item_parents = parent_names.clone();
|
2020-03-01 00:55:45 +08:00
|
|
|
|
2017-11-18 19:50:47 +08:00
|
|
|
ch.number = link.number.clone();
|
|
|
|
|
2018-03-07 21:02:06 +08:00
|
|
|
sub_item_parents.push(link.name.clone());
|
2018-08-03 09:22:49 +08:00
|
|
|
let sub_items = link
|
|
|
|
.nested_items
|
2017-12-11 07:32:35 +08:00
|
|
|
.iter()
|
2020-03-10 05:34:28 +08:00
|
|
|
.map(|i| load_summary_item(i, src_dir, sub_item_parents.clone()))
|
2017-12-11 07:32:35 +08:00
|
|
|
.collect::<Result<Vec<_>>>()?;
|
2017-11-18 19:50:47 +08:00
|
|
|
|
|
|
|
ch.sub_items = sub_items;
|
|
|
|
|
|
|
|
Ok(ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A depth-first iterator over the items in a book.
|
|
|
|
///
|
|
|
|
/// # Note
|
|
|
|
///
|
|
|
|
/// This struct shouldn't be created directly, instead prefer the
|
|
|
|
/// [`Book::iter()`] method.
|
|
|
|
///
|
|
|
|
/// [`Book::iter()`]: struct.Book.html#method.iter
|
|
|
|
pub struct BookItems<'a> {
|
|
|
|
items: VecDeque<&'a BookItem>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Iterator for BookItems<'a> {
|
|
|
|
type Item = &'a BookItem;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
let item = self.items.pop_front();
|
|
|
|
|
|
|
|
if let Some(&BookItem::Chapter(ref ch)) = item {
|
|
|
|
// if we wanted a breadth-first iterator we'd `extend()` here
|
|
|
|
for sub_item in ch.sub_items.iter().rev() {
|
|
|
|
self.items.push_front(sub_item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
item
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Chapter {
|
2019-05-07 04:50:34 +08:00
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
2017-11-18 19:50:47 +08:00
|
|
|
if let Some(ref section_number) = self.number {
|
|
|
|
write!(f, "{} ", section_number)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, "{}", self.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use std::io::Write;
|
2018-07-24 01:45:01 +08:00
|
|
|
use tempfile::{Builder as TempFileBuilder, TempDir};
|
2017-11-18 19:50:47 +08:00
|
|
|
|
2019-05-07 02:20:58 +08:00
|
|
|
const DUMMY_SRC: &str = "
|
2017-11-18 19:50:47 +08:00
|
|
|
# Dummy Chapter
|
|
|
|
|
|
|
|
this is some dummy text.
|
|
|
|
|
2017-11-18 22:07:08 +08:00
|
|
|
And here is some \
|
|
|
|
more text.
|
2017-11-18 19:50:47 +08:00
|
|
|
";
|
|
|
|
|
|
|
|
/// Create a dummy `Link` in a temporary directory.
|
|
|
|
fn dummy_link() -> (Link, TempDir) {
|
2018-03-27 07:47:37 +08:00
|
|
|
let temp = TempFileBuilder::new().prefix("book").tempdir().unwrap();
|
2017-11-18 19:50:47 +08:00
|
|
|
|
|
|
|
let chapter_path = temp.path().join("chapter_1.md");
|
2017-12-11 07:32:35 +08:00
|
|
|
File::create(&chapter_path)
|
|
|
|
.unwrap()
|
2019-05-07 02:20:58 +08:00
|
|
|
.write_all(DUMMY_SRC.as_bytes())
|
2017-12-11 07:32:35 +08:00
|
|
|
.unwrap();
|
2017-11-18 19:50:47 +08:00
|
|
|
|
2020-03-25 06:52:24 +08:00
|
|
|
let link = Link::new("Chapter 1", chapter_path);
|
2017-11-18 19:50:47 +08:00
|
|
|
|
|
|
|
(link, temp)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a nested `Link` written to a temporary directory.
|
|
|
|
fn nested_links() -> (Link, TempDir) {
|
|
|
|
let (mut root, temp_dir) = dummy_link();
|
|
|
|
|
|
|
|
let second_path = temp_dir.path().join("second.md");
|
|
|
|
|
2017-12-11 07:32:35 +08:00
|
|
|
File::create(&second_path)
|
|
|
|
.unwrap()
|
2019-05-07 02:20:58 +08:00
|
|
|
.write_all(b"Hello World!")
|
2017-12-11 07:32:35 +08:00
|
|
|
.unwrap();
|
2017-11-18 19:50:47 +08:00
|
|
|
|
2020-03-25 06:52:24 +08:00
|
|
|
let mut second = Link::new("Nested Chapter 1", &second_path);
|
2017-11-18 19:50:47 +08:00
|
|
|
second.number = Some(SectionNumber(vec![1, 2]));
|
|
|
|
|
|
|
|
root.nested_items.push(second.clone().into());
|
|
|
|
root.nested_items.push(SummaryItem::Separator);
|
|
|
|
root.nested_items.push(second.clone().into());
|
|
|
|
|
|
|
|
(root, temp_dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn load_a_single_chapter_from_disk() {
|
|
|
|
let (link, temp_dir) = dummy_link();
|
2018-03-14 23:47:17 +08:00
|
|
|
let should_be = Chapter::new(
|
|
|
|
"Chapter 1",
|
|
|
|
DUMMY_SRC.to_string(),
|
|
|
|
"chapter_1.md",
|
|
|
|
Vec::new(),
|
|
|
|
);
|
2017-11-18 19:50:47 +08:00
|
|
|
|
2018-03-07 21:02:06 +08:00
|
|
|
let got = load_chapter(&link, temp_dir.path(), Vec::new()).unwrap();
|
2017-11-18 19:50:47 +08:00
|
|
|
assert_eq!(got, should_be);
|
|
|
|
}
|
|
|
|
|
2020-10-07 22:50:25 +08:00
|
|
|
#[test]
|
|
|
|
fn load_a_single_chapter_with_utf8_bom_from_disk() {
|
|
|
|
let temp_dir = TempFileBuilder::new().prefix("book").tempdir().unwrap();
|
|
|
|
|
|
|
|
let chapter_path = temp_dir.path().join("chapter_1.md");
|
|
|
|
File::create(&chapter_path)
|
|
|
|
.unwrap()
|
|
|
|
.write_all(("\u{feff}".to_owned() + DUMMY_SRC).as_bytes())
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let link = Link::new("Chapter 1", chapter_path);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-11-18 19:50:47 +08:00
|
|
|
#[test]
|
|
|
|
fn cant_load_a_nonexistent_chapter() {
|
2020-03-25 06:52:24 +08:00
|
|
|
let link = Link::new("Chapter 1", "/foo/bar/baz.md");
|
2017-11-18 19:50:47 +08:00
|
|
|
|
2018-03-07 21:02:06 +08:00
|
|
|
let got = load_chapter(&link, "", Vec::new());
|
2017-11-18 19:50:47 +08:00
|
|
|
assert!(got.is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn load_recursive_link_with_separators() {
|
|
|
|
let (root, temp) = nested_links();
|
|
|
|
|
|
|
|
let nested = Chapter {
|
|
|
|
name: String::from("Nested Chapter 1"),
|
|
|
|
content: String::from("Hello World!"),
|
|
|
|
number: Some(SectionNumber(vec![1, 2])),
|
2020-03-01 00:55:45 +08:00
|
|
|
path: Some(PathBuf::from("second.md")),
|
2018-03-07 21:02:06 +08:00
|
|
|
parent_names: vec![String::from("Chapter 1")],
|
2017-11-18 19:50:47 +08:00
|
|
|
sub_items: Vec::new(),
|
|
|
|
};
|
|
|
|
let should_be = BookItem::Chapter(Chapter {
|
|
|
|
name: String::from("Chapter 1"),
|
|
|
|
content: String::from(DUMMY_SRC),
|
|
|
|
number: None,
|
2020-03-01 00:55:45 +08:00
|
|
|
path: Some(PathBuf::from("chapter_1.md")),
|
2018-03-07 21:02:06 +08:00
|
|
|
parent_names: Vec::new(),
|
2017-11-18 19:50:47 +08:00
|
|
|
sub_items: vec![
|
|
|
|
BookItem::Chapter(nested.clone()),
|
|
|
|
BookItem::Separator,
|
|
|
|
BookItem::Chapter(nested.clone()),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
2018-03-07 21:02:06 +08:00
|
|
|
let got = load_summary_item(&SummaryItem::Link(root), temp.path(), Vec::new()).unwrap();
|
2017-11-18 19:50:47 +08:00
|
|
|
assert_eq!(got, should_be);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn load_a_book_with_a_single_chapter() {
|
|
|
|
let (link, temp) = dummy_link();
|
|
|
|
let summary = Summary {
|
2020-05-19 00:18:14 +08:00
|
|
|
numbered_chapters: vec![SummaryItem::Link(link)],
|
2017-11-18 19:50:47 +08:00
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let should_be = Book {
|
2018-07-24 01:45:01 +08:00
|
|
|
sections: vec![BookItem::Chapter(Chapter {
|
|
|
|
name: String::from("Chapter 1"),
|
|
|
|
content: String::from(DUMMY_SRC),
|
2020-03-01 00:55:45 +08:00
|
|
|
path: Some(PathBuf::from("chapter_1.md")),
|
2018-07-24 01:45:01 +08:00
|
|
|
..Default::default()
|
|
|
|
})],
|
2018-03-14 23:47:17 +08:00
|
|
|
..Default::default()
|
2017-11-18 19:50:47 +08:00
|
|
|
};
|
|
|
|
|
2017-11-18 20:01:50 +08:00
|
|
|
let got = load_book_from_disk(&summary, temp.path()).unwrap();
|
2017-11-18 19:50:47 +08:00
|
|
|
|
|
|
|
assert_eq!(got, should_be);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn book_iter_iterates_over_sequential_items() {
|
|
|
|
let book = Book {
|
|
|
|
sections: vec![
|
|
|
|
BookItem::Chapter(Chapter {
|
|
|
|
name: String::from("Chapter 1"),
|
|
|
|
content: String::from(DUMMY_SRC),
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
BookItem::Separator,
|
|
|
|
],
|
2018-03-14 23:47:17 +08:00
|
|
|
..Default::default()
|
2017-11-18 19:50:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
let should_be: Vec<_> = book.sections.iter().collect();
|
|
|
|
|
|
|
|
let got: Vec<_> = book.iter().collect();
|
|
|
|
|
|
|
|
assert_eq!(got, should_be);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn iterate_over_nested_book_items() {
|
|
|
|
let book = Book {
|
|
|
|
sections: vec![
|
|
|
|
BookItem::Chapter(Chapter {
|
|
|
|
name: String::from("Chapter 1"),
|
|
|
|
content: String::from(DUMMY_SRC),
|
|
|
|
number: None,
|
2020-03-01 00:55:45 +08:00
|
|
|
path: Some(PathBuf::from("Chapter_1/index.md")),
|
2018-03-07 21:02:06 +08:00
|
|
|
parent_names: Vec::new(),
|
2017-11-18 19:50:47 +08:00
|
|
|
sub_items: vec![
|
2017-11-18 22:07:08 +08:00
|
|
|
BookItem::Chapter(Chapter::new(
|
|
|
|
"Hello World",
|
|
|
|
String::new(),
|
|
|
|
"Chapter_1/hello.md",
|
2018-03-07 21:02:06 +08:00
|
|
|
Vec::new(),
|
2017-11-18 22:07:08 +08:00
|
|
|
)),
|
2017-11-18 19:50:47 +08:00
|
|
|
BookItem::Separator,
|
2017-11-18 22:07:08 +08:00
|
|
|
BookItem::Chapter(Chapter::new(
|
|
|
|
"Goodbye World",
|
|
|
|
String::new(),
|
|
|
|
"Chapter_1/goodbye.md",
|
2018-03-07 21:02:06 +08:00
|
|
|
Vec::new(),
|
2017-11-18 22:07:08 +08:00
|
|
|
)),
|
2017-11-18 19:50:47 +08:00
|
|
|
],
|
|
|
|
}),
|
|
|
|
BookItem::Separator,
|
|
|
|
],
|
2018-03-14 23:47:17 +08:00
|
|
|
..Default::default()
|
2017-11-18 19:50:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
let got: Vec<_> = book.iter().collect();
|
|
|
|
|
|
|
|
assert_eq!(got.len(), 5);
|
|
|
|
|
|
|
|
// checking the chapter names are in the order should be sufficient here...
|
2018-08-03 09:22:49 +08:00
|
|
|
let chapter_names: Vec<String> = got
|
|
|
|
.into_iter()
|
2017-12-11 07:32:35 +08:00
|
|
|
.filter_map(|i| match *i {
|
|
|
|
BookItem::Chapter(ref ch) => Some(ch.name.clone()),
|
|
|
|
_ => None,
|
2019-05-05 22:57:43 +08:00
|
|
|
})
|
|
|
|
.collect();
|
2017-11-18 19:50:47 +08:00
|
|
|
let should_be: Vec<_> = vec![
|
|
|
|
String::from("Chapter 1"),
|
|
|
|
String::from("Hello World"),
|
|
|
|
String::from("Goodbye World"),
|
|
|
|
];
|
|
|
|
|
|
|
|
assert_eq!(chapter_names, should_be);
|
|
|
|
}
|
2018-01-22 06:44:28 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn for_each_mut_visits_all_items() {
|
|
|
|
let mut book = Book {
|
|
|
|
sections: vec![
|
|
|
|
BookItem::Chapter(Chapter {
|
|
|
|
name: String::from("Chapter 1"),
|
|
|
|
content: String::from(DUMMY_SRC),
|
|
|
|
number: None,
|
2020-03-01 00:55:45 +08:00
|
|
|
path: Some(PathBuf::from("Chapter_1/index.md")),
|
2018-03-07 21:02:06 +08:00
|
|
|
parent_names: Vec::new(),
|
2018-01-22 06:44:28 +08:00
|
|
|
sub_items: vec![
|
|
|
|
BookItem::Chapter(Chapter::new(
|
|
|
|
"Hello World",
|
|
|
|
String::new(),
|
|
|
|
"Chapter_1/hello.md",
|
2018-03-07 21:02:06 +08:00
|
|
|
Vec::new(),
|
2018-01-22 06:44:28 +08:00
|
|
|
)),
|
|
|
|
BookItem::Separator,
|
|
|
|
BookItem::Chapter(Chapter::new(
|
|
|
|
"Goodbye World",
|
|
|
|
String::new(),
|
|
|
|
"Chapter_1/goodbye.md",
|
2018-03-07 21:02:06 +08:00
|
|
|
Vec::new(),
|
2018-01-22 06:44:28 +08:00
|
|
|
)),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
BookItem::Separator,
|
|
|
|
],
|
2018-03-14 23:47:17 +08:00
|
|
|
..Default::default()
|
2018-01-22 06:44:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
let num_items = book.iter().count();
|
|
|
|
let mut visited = 0;
|
|
|
|
|
|
|
|
book.for_each_mut(|_| visited += 1);
|
|
|
|
|
|
|
|
assert_eq!(visited, num_items);
|
|
|
|
}
|
2018-01-22 20:47:29 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cant_load_chapters_with_an_empty_path() {
|
|
|
|
let (_, temp) = dummy_link();
|
|
|
|
let summary = Summary {
|
2020-05-19 00:18:14 +08:00
|
|
|
numbered_chapters: vec![SummaryItem::Link(Link {
|
|
|
|
name: String::from("Empty"),
|
|
|
|
location: Some(PathBuf::from("")),
|
|
|
|
..Default::default()
|
|
|
|
})],
|
|
|
|
|
2018-01-22 20:47:29 +08:00
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
let got = load_book_from_disk(&summary, temp.path());
|
|
|
|
assert!(got.is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cant_load_chapters_when_the_link_is_a_directory() {
|
|
|
|
let (_, temp) = dummy_link();
|
|
|
|
let dir = temp.path().join("nested");
|
|
|
|
fs::create_dir(&dir).unwrap();
|
|
|
|
|
|
|
|
let summary = Summary {
|
2020-05-19 00:18:14 +08:00
|
|
|
numbered_chapters: vec![SummaryItem::Link(Link {
|
|
|
|
name: String::from("nested"),
|
|
|
|
location: Some(dir),
|
|
|
|
..Default::default()
|
|
|
|
})],
|
2018-01-22 20:47:29 +08:00
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
let got = load_book_from_disk(&summary, temp.path());
|
|
|
|
assert!(got.is_err());
|
|
|
|
}
|
2017-11-18 19:50:47 +08:00
|
|
|
}
|