//! Table-of-contents creation.
-use std::fmt;
-use std::string::String;
-
/// A (recursive) table of contents
-#[derive(PartialEq)]
+#[derive(Debug, PartialEq)]
pub struct Toc {
/// The levels are strictly decreasing, i.e.
///
/// ### A
/// ## B
/// ```
- entries: Vec<TocEntry>
+ entries: Vec<TocEntry>,
}
impl Toc {
}
}
-#[derive(PartialEq)]
+#[derive(Debug, PartialEq)]
pub struct TocEntry {
level: u32,
sec_number: String,
/// it is the most recent one).
///
/// We also have `chain[0].level <= top_level.entries[last]`.
- chain: Vec<TocEntry>
+ chain: Vec<TocEntry>,
}
impl TocBuilder {
TocBuilder { top_level: Toc { entries: Vec::new() }, chain: Vec::new() }
}
-
/// Converts into a true `Toc` struct.
pub fn into_toc(mut self) -> Toc {
// we know all levels are >= 1.
// this is the parent we want, so return it to
// its rightful place.
self.chain.push(next);
- return
+ return;
} else {
this = Some(next);
}
}
None => {
this.map(|e| self.top_level.entries.push(e));
- return
+ return;
}
}
}
name,
sec_number,
id,
- children: Toc { entries: Vec::new() }
+ children: Toc { entries: Vec::new() },
});
// get the thing we just pushed, so we can borrow the string
}
}
-impl fmt::Debug for Toc {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- fmt::Display::fmt(self, f)
- }
-}
-
-impl fmt::Display for Toc {
- fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
- write!(fmt, "<ul>")?;
+impl Toc {
+ fn print_inner(&self, v: &mut String) {
+ v.push_str("<ul>");
for entry in &self.entries {
- // recursively format this table of contents (the
- // `{children}` is the key).
- write!(fmt,
- "\n<li><a href=\"#{id}\">{num} {name}</a>{children}</li>",
- id = entry.id,
- num = entry.sec_number, name = entry.name,
- children = entry.children)?
+ // recursively format this table of contents
+ v.push_str(&format!(
+ "\n<li><a href=\"#{id}\">{num} {name}</a>",
+ id = entry.id,
+ num = entry.sec_number,
+ name = entry.name
+ ));
+ entry.children.print_inner(&mut *v);
+ v.push_str("</li>");
}
- write!(fmt, "</ul>")
+ v.push_str("</ul>");
+ }
+ crate fn print(&self) -> String {
+ let mut v = String::new();
+ self.print_inner(&mut v);
+ v
}
}