From 42909cf0124b5b536d9af842e3166b800e0a7957 Mon Sep 17 00:00:00 2001 From: Mathieu David Date: Tue, 28 Jun 2016 18:59:13 +0200 Subject: [PATCH] Add the new book struct --- src/book/book.rs | 77 +++++++++++++++++++++++++++++++++++++++++++++ src/book/chapter.rs | 2 -- src/book/mod.rs | 7 +++++ 3 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 src/book/book.rs diff --git a/src/book/book.rs b/src/book/book.rs new file mode 100644 index 00000000..c5e46de8 --- /dev/null +++ b/src/book/book.rs @@ -0,0 +1,77 @@ +use book::metadata::BookMetadata; +use book::chapter::Chapter; + + +/// The `Book` struct contains the metadata and chapters for one language of the book. +/// Multiple `Book` structs are combined in the `MDBook` struct to support multi-language books. +pub struct Book { + metadata: BookMetadata, + + preface: Vec, + chapters: Vec, + appendix: Vec, +} + +impl Book { + /// Creates a new book with the given title, chapters are added with the `add_chapter` method + pub fn new(title: &str) -> Self { + Book { + metadata: BookMetadata::new(title), + + preface: Vec::new(), + chapters: Vec::new(), + appendix: Vec::new(), + } + } + + /// Adds a new chapter at the end of the book + pub fn add_chapter(&mut self, chapter: Chapter) -> &mut Self { + self.chapters.push(chapter); + self + } + + /// Adds a new preface chapter to the book, the preface chapters are in the order they were added + pub fn add_preface_chapter(&mut self, chapter: Chapter) -> &mut Self { + self.preface.push(chapter); + self + } + + /// Adds a new appendix chapter to the book, they are in de order they are added + pub fn add_appendix_chapter(&mut self, chapter: Chapter) -> &mut Self { + self.appendix.push(chapter); + self + } + + + /// This method takes a slice `&[x, y, z]` as parameter and returns the corresponding chapter. + /// For example, to retrieve chapter 2.3 we would use: + /// ``` + /// #extern crate mdbook; + /// #use mdbook::book::Book; + /// #fn main() { + /// #let book = Book::new("Test"); + /// let chapter_2_3 = book.get_chapter(&[2, 3]); + /// #} + /// ``` + pub fn get_chapter(&self, section: &[usize]) -> Option<&Chapter> { + match section.len() { + 0 => None, + 1 => self.chapters.get(section[0]), + _ => { + self.chapters + .get(section[0]) + .and_then(|ch| ch.get_sub_chapter(§ion[1..])) + }, + } + } + + /// Returns a mutable reference to the metadata for modification + pub fn mut_metadata(&mut self) -> &mut BookMetadata { + &mut self.metadata + } + + // Returns a reference to the metadata + pub fn metadata(&self) -> &BookMetadata { + &self.metadata + } +} diff --git a/src/book/chapter.rs b/src/book/chapter.rs index bd37ccac..23244cb5 100644 --- a/src/book/chapter.rs +++ b/src/book/chapter.rs @@ -1,5 +1,3 @@ -use book::metadata::BookMetadata; - use std::path; /// The Chapter struct holds the title of the chapter as written in the SUMMARY.md file, diff --git a/src/book/mod.rs b/src/book/mod.rs index c3594300..cf9147f0 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -1,5 +1,12 @@ pub mod bookitem; pub mod bookconfig; +pub mod metadata; +pub mod chapter; +pub mod book; + +pub use self::metadata::{Author, Language, BookMetadata}; +pub use self::chapter::Chapter; +pub use self::book::Book; pub use self::bookitem::{BookItem, BookItems}; pub use self::bookconfig::BookConfig;