plissken-core::docstring
Rust
Docstring parser for Google, NumPy, and Rust doc comment styles
This module parses docstrings into structured ParsedDocstring objects,
extracting summary, parameters, returns, raises, and examples.
Supported formats:
- Google style:
Args:,Returns:,Raises:,Example: - NumPy style: Underlined section headers
- Rust style:
# Arguments,# Returns,# Errors,# Panics,# Examples
Functions
Section titled “Functions”fn parse_docstring
Section titled “fn parse_docstring”pub
fn parse_docstring(docstring: &str) -> ParsedDocstringParse a docstring into structured form
Source
pub fn parse_docstring(docstring: &str) -> ParsedDocstring { let docstring = docstring.trim(); if docstring.is_empty() { return ParsedDocstring::empty(); }
// Detect style based on section format let style = detect_style(docstring);
match style { DocstringStyle::Google => parse_google_style(docstring), DocstringStyle::NumPy => parse_numpy_style(docstring), DocstringStyle::Plain => parse_plain(docstring), }}fn parse_rust_doc
Section titled “fn parse_rust_doc”pub
fn parse_rust_doc(doc: &str) -> ParsedDocstringParse a Rust doc comment into structured form
Looks for conventional markdown sections:
# Arguments/# Parameters- function parameters# Returns- return value documentation# Errors- error conditions (raises of kindRUST_ERROR_KIND)# Panics- panic conditions (raises of kindRUST_PANIC_KIND)# Examples- code examples
Other sections (# Safety, # Overview, …) stay in the description,
with the heading as a bold line. The description keeps its Markdown:
paragraphs, lists and code blocks. Code blocks hold Rust unless they name
another language, as in rustdoc: they get the language rust, and the
hidden lines of a doc test (# use foo;) are left out.
Source
pub fn parse_rust_doc(doc: &str) -> ParsedDocstring { let doc = normalize_rust_doc_code(doc); let doc = doc.trim_matches(|c: char| c == '\n' || c == '\r'); if doc.trim().is_empty() { return ParsedDocstring::empty(); }
let lines: Vec<&str> = doc.lines().collect();
let mut text: Vec<String> = Vec::new(); let mut params = Vec::new(); let mut returns = None; let mut raises = Vec::new(); let mut examples = Vec::new();
for (header, body) in split_rust_sections(&lines) { let Some(header) = header else { text.extend(body.iter().map(|l| l.to_string())); continue; }; match header.to_lowercase().as_str() { "arguments" | "parameters" | "args" | "params" => { params = parse_rust_arguments(body); } "returns" | "return" => { returns = parse_rust_returns(body); } "errors" | "error" => { raises.extend(parse_rust_errors(body, RUST_ERROR_KIND)); } "panics" | "panic" => { raises.extend(parse_rust_errors(body, RUST_PANIC_KIND)); } "examples" | "example" => { examples = parse_rust_examples(body); } _ => { // Any other section stays in the text, its heading in bold text.push(String::new()); text.push(format!("**{}**", header)); text.push(String::new()); text.extend(body.iter().map(|l| l.to_string())); } } }
let (summary, description) = split_rust_summary(&text);
ParsedDocstring { summary, description, params, returns, raises, examples, }}fn normalize_rust_doc
Section titled “fn normalize_rust_doc”pub
fn normalize_rust_doc(doc: &str) -> StringRust doc text made ready for Markdown output.
Code blocks get the language rust (see parse_rust_doc), and
runs of blank lines become one. For doc comments that are not parsed into
sections: fields, variants.
Source
pub fn normalize_rust_doc(doc: &str) -> String { let doc = normalize_rust_doc_code(doc); let lines: Vec<&str> = doc.lines().collect(); tidy_markdown_lines(&lines).join("\n")}Constants
Section titled “Constants”| Name | Type | Value | Description |
|---|---|---|---|
RUST_ERROR_KIND |
&str |
"Error" |
The RaisesDoc kind of an entry of a Rust # Errors section. |
RUST_PANIC_KIND |
&str |
"Panic" |
The RaisesDoc kind of an entry of a Rust # Panics section. |
