From 5716c08e2e0c03774efbf0b1136c661462b54d33 Mon Sep 17 00:00:00 2001 From: mcarton Date: Tue, 30 May 2017 20:33:08 +0200 Subject: [PATCH] Expose a function to parse `pulldown-mark` Events This might be useful for a tool (who said Clippy?) that already parses markdown using `pulldown-cmark` in order not to parse the documentation twice. --- src/lib.rs | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 84b5f34..53ae6d0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,6 +10,7 @@ extern crate pulldown_cmark; #[macro_use] extern crate quick_error; use pulldown_cmark::Parser; +use pulldown_cmark::Event; mod types; mod errors; @@ -19,6 +20,8 @@ mod extractors; pub use errors::ParseError; pub use types::*; +use ::std::iter::Peekable; + /// Parse documentation and extract data /// /// # Parameters @@ -62,10 +65,29 @@ pub use types::*; /// ``` pub fn parse_md_docblock(md: &str) -> Result { let mut md_events = Parser::new(md).peekable(); + parse_md_docblock_events(&mut md_events) +} +/// Parse documentation and extract data +/// +/// # Parameters +/// +/// - `md`: Markdown +/// +/// # Returns +/// +/// A `Result`, which is either +/// +/// - `Ok(DocBlock)`: A type that contains all extracted information (including +/// all unknown sections as `Custom` sections). +/// - `Err(ParseError)`: The first encountered error while parsing the +/// documentation string. +pub fn parse_md_docblock_events<'a, I>(events: &mut Peekable) -> Result where + I: Iterator>, +{ Ok(DocBlock { - teaser: try!(extractors::teaser(&mut md_events)), - description: try!(extractors::description(&mut md_events)), - sections: try!(extractors::sections(&mut md_events)), + teaser: try!(extractors::teaser(events)), + description: try!(extractors::description(events)), + sections: try!(extractors::sections(events)), }) }