Skip to main content

Document

Struct Document 

Source
pub struct Document<'a> { /* private fields */ }
Expand description

Style-independent view of a parsed docstring root.

Construct with Document::new from any Parsed result, whatever the source style.

Implementations§

Source§

impl<'a> Document<'a>

Source

pub fn new(parsed: &'a Parsed) -> Self

View the root of parsed as a style-independent document.

Examples found in repository?
examples/parse_numpy.rs (line 39)
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}
Source

pub fn style(&self) -> Style

The style the docstring was parsed as.

Source

pub fn syntax(&self) -> &'a SyntaxNode

Access the underlying SyntaxNode.

Examples found in repository?
examples/parse_numpy.rs (line 49)
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}
Source

pub fn summary(&self) -> Option<TextBlock<'a>>

Brief summary block, if present.

Source

pub fn extended_summary(&self) -> Option<TextBlock<'a>>

Extended summary block, if present.

Source

pub fn directives(&self) -> impl Iterator<Item = Directive<'a>>

Iterate over document-level directives (e.g. .. deprecated:: 1.0).

Source

pub fn sections(&self) -> impl Iterator<Item = Section<'a>>

Iterate over all sections, in source order.

Source

pub fn paragraphs(&self) -> impl Iterator<Item = TextBlock<'a>>

Iterate over stray-prose paragraph blocks (PARAGRAPH nodes) between sections, in source order.

Trait Implementations§

Source§

impl<'a> Clone for Document<'a>

Source§

fn clone(&self) -> Document<'a>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Copy for Document<'a>

Source§

impl<'a> Debug for Document<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Document<'a>

§

impl<'a> RefUnwindSafe for Document<'a>

§

impl<'a> Send for Document<'a>

§

impl<'a> Sync for Document<'a>

§

impl<'a> Unpin for Document<'a>

§

impl<'a> UnsafeUnpin for Document<'a>

§

impl<'a> UnwindSafe for Document<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.