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 serde::ser::SerializeStruct;
use std::path::PathBuf; use std::path::PathBuf;
type Section = Vec<i32>;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum BookItem { pub enum BookItem {
Chapter(String, Chapter), // String = section Chapter(Section, Chapter),
Affix(Chapter), Affix(Chapter),
Spacer, Spacer,
} }

View File

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