diff --git a/src/loader/summary.rs b/src/loader/summary.rs index 52a1d7d5..d5ba64bf 100644 --- a/src/loader/summary.rs +++ b/src/loader/summary.rs @@ -1,9 +1,64 @@ use std::error::Error; +use std::fmt::{self, Formatter, Display}; +use std::ops::{Deref, DerefMut}; use pulldown_cmark; -/// The parsed `SUMMARY.md`, specifying how the book should be laid out. -pub struct Summary; +/// The parsed `SUMMARY.md`, specifying how the book should be laid out. +pub struct Summary { + title: Option, +} + +/// Parse the text from a `SUMMARY.md` file into a sort of "recipe" to be +/// used when loading a book from disk. pub fn parse_summary(summary: &str) -> Result> { unimplemented!() +} + +/// A section number like "1.2.3", basically just a newtype'd `Vec`. +#[derive(Debug, PartialEq, Clone, Default)] +struct SectionNumber(Vec); + +impl Display for SectionNumber { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + let dotted_number: String = self.0.iter().map(|i| format!("{}", i)) + .collect::>() + .join("."); + + write!(f, "{}", dotted_number) + } +} + +impl Deref for SectionNumber { + type Target = Vec; + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for SectionNumber { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn section_number_has_correct_dotted_representation() { + let inputs = vec![ + (vec![0], "0"), + (vec![1, 3], "1.3"), + (vec![1, 2, 3], "1.2.3"), + ]; + + for (input, should_be) in inputs { + let section_number = SectionNumber(input); + let string_repr = format!("{}", section_number); + + assert_eq!(string_repr, should_be); + } + } } \ No newline at end of file