Skip to main content

rustpython_ruff_python_ast/
docstrings.rs

1//! Utilities for parsing Python docstrings.
2
3/// Extract the leading words from a line of text within a Python docstring.
4pub fn leading_words(line: &str) -> &str {
5    let line = line.trim();
6    line.find(|char: char| !char.is_alphanumeric() && !char.is_whitespace())
7        .map_or(line, |index| &line[..index])
8}
9
10/// Extract the leading whitespace from a line of text within a Python docstring.
11pub fn leading_space(line: &str) -> &str {
12    line.find(|char: char| !char.is_whitespace())
13        .map_or(line, |index| &line[..index])
14}
15
16/// Replace any non-whitespace characters from an indentation string within a Python docstring.
17pub fn clean_space(indentation: &str) -> String {
18    indentation
19        .chars()
20        .map(|char| if char.is_whitespace() { char } else { ' ' })
21        .collect()
22}