]> git.lizzy.rs Git - rust.git/blobdiff - src/librustdoc/html/toc.rs
Rollup merge of #69379 - jumbatm:llvm-sigsegv, r=pnkfelix
[rust.git] / src / librustdoc / html / toc.rs
index 2da7aceae8bf4fd23c19b9aee537c0aaf03bf679..034fb27300027b82a1db4f041c7f6809e18772c0 100644 (file)
@@ -1,10 +1,7 @@
 //! 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.
     ///
@@ -19,7 +16,7 @@ pub struct Toc {
     /// ### A
     /// ## B
     /// ```
-    entries: Vec<TocEntry>
+    entries: Vec<TocEntry>,
 }
 
 impl Toc {
@@ -28,7 +25,7 @@ fn count_entries_with_level(&self, level: u32) -> usize {
     }
 }
 
-#[derive(PartialEq)]
+#[derive(Debug, PartialEq)]
 pub struct TocEntry {
     level: u32,
     sec_number: String,
@@ -49,7 +46,7 @@ pub struct TocBuilder {
     /// it is the most recent one).
     ///
     /// We also have `chain[0].level <= top_level.entries[last]`.
-    chain: Vec<TocEntry>
+    chain: Vec<TocEntry>,
 }
 
 impl TocBuilder {
@@ -57,7 +54,6 @@ pub fn new() -> 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.
@@ -103,14 +99,14 @@ fn fold_until(&mut self, level: u32) {
                         // 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;
                 }
             }
         }
@@ -155,7 +151,7 @@ pub fn push(&mut self, level: u32, name: String, id: String) -> &str {
             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
@@ -165,25 +161,26 @@ pub fn push(&mut self, level: u32, name: String, id: String) -> &str {
     }
 }
 
-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
     }
 }