Skip to main content

parse_numpy/
parse_numpy.rs

1//! Example: Parsing NumPy-style docstrings
2//!
3//! Shows the raw docstring text, then the detailed parsed AST.
4
5use pydocstring::parse::parse_numpy;
6use pydocstring::parse::unified::Document;
7
8fn main() {
9    let docstring = r#"
10Calculate the area of a rectangle.
11
12This function takes the width and height of a rectangle
13and returns its area.
14
15Parameters
16----------
17width : float
18    The width of the rectangle.
19height : float
20    The height of the rectangle.
21
22Returns
23-------
24float
25    The area of the rectangle.
26
27Raises
28------
29ValueError
30    If width or height is negative.
31
32Examples
33--------
34>>> calculate_area(5.0, 3.0)
3515.0
36"#;
37
38    let parsed = parse_numpy(docstring);
39    let doc = Document::new(&parsed);
40
41    println!("╔══════════════════════════════════════════════════╗");
42    println!("║          NumPy-style Docstring Example           ║");
43    println!("╚══════════════════════════════════════════════════╝");
44
45    println!();
46
47    // Display: raw source text
48    println!("── raw text ────────────────────────────────────────");
49    println!("{}", doc.syntax().range().source_text(parsed.source()));
50
51    println!();
52
53    // pretty_print: structured AST
54    println!("── parsed AST ──────────────────────────────────────");
55    print!("{}", parsed.pretty_print());
56}