Track the current section as a vector of i32 rather than a String.

The vector is the "natural" format of the section (i.e. how it is
initially created) and is more suitable for further processing than a
string is. Creation of a string representation of the section is delayed
until it's actually required.
This commit is contained in:
Chris Spiegel 2017-12-05 18:53:17 -08:00
parent e735bc6d3e
commit cd3b37593b
3 changed files with 7 additions and 6 deletions

View File

@ -2,10 +2,11 @@ use serde::{Serialize, Serializer};
use serde::ser::SerializeStruct;
use std::path::PathBuf;
type Section = Vec<i32>;
#[derive(Debug, Clone)]
pub enum BookItem {
Chapter(String, Chapter), // String = section
Chapter(Section, Chapter),
Affix(Chapter),
Spacer,
}

View File

@ -110,9 +110,7 @@ fn parse_level(summary: &mut Vec<&str>,
// Increment section
let len = section.len() - 1;
section[len] += 1;
let s = section.iter()
.fold("".to_owned(), |s, i| s + &i.to_string() + ".");
BookItem::Chapter(s, ch)
BookItem::Chapter(section.clone(), ch)
}
_ => parsed_item,
}
@ -181,7 +179,7 @@ fn parse_line(l: &str) -> Option<BookItem> {
debug!("[*]: Line is list element");
if let Some((name, path)) = read_link(line) {
return Some(BookItem::Chapter("0".to_owned(), Chapter::new(name, path)));
return Some(BookItem::Chapter(vec![0], Chapter::new(name, path)));
} else {
return None;
}

View File

@ -407,7 +407,9 @@ fn make_data(book: &MDBook, config: &Config) -> Result<serde_json::Map<String, s
chapter.insert("path".to_owned(), json!(path));
}
BookItem::Chapter(ref s, ref ch) => {
chapter.insert("section".to_owned(), json!(s));
let section = s.iter()
.fold("".to_owned(), |s, i| s + &i.to_string() + ".");
chapter.insert("section".to_owned(), json!(section));
chapter.insert("name".to_owned(), json!(ch.name));
let path = ch.path.to_str().ok_or_else(|| {
io::Error::new(io::ErrorKind::Other,