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::numpy::{NumPyDocstring, parse_numpy};
6
7fn main() {
8    let docstring = r#"
9Calculate the area of a rectangle.
10
11This function takes the width and height of a rectangle
12and returns its area.
13
14Parameters
15----------
16width : float
17    The width of the rectangle.
18height : float
19    The height of the rectangle.
20
21Returns
22-------
23float
24    The area of the rectangle.
25
26Raises
27------
28ValueError
29    If width or height is negative.
30
31Examples
32--------
33>>> calculate_area(5.0, 3.0)
3415.0
35"#;
36
37    let parsed = parse_numpy(docstring);
38    let doc = NumPyDocstring::cast(parsed.root()).unwrap();
39
40    println!("╔══════════════════════════════════════════════════╗");
41    println!("║          NumPy-style Docstring Example           ║");
42    println!("╚══════════════════════════════════════════════════╝");
43
44    println!();
45
46    // Display: raw source text
47    println!("── raw text ────────────────────────────────────────");
48    println!("{}", doc.syntax().range().source_text(parsed.source()));
49
50    println!();
51
52    // pretty_print: structured AST
53    println!("── parsed AST ──────────────────────────────────────");
54    print!("{}", parsed.pretty_print());
55}