1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//! Parsing: source text in, [`Parsed`] out.
//!
//! [`parse`] auto-detects the style; [`parse_google`], [`parse_numpy`] and
//! [`parse_plain`] force one. All four return the same [`Parsed`], so nothing
//! downstream branches on which one you called — read it through the
//! style-independent [`Document`] view, the raw CST ([`Parsed::root`]), or the
//! normalized model ([`Parsed::to_model`]).
//!
//! The per-style parsers are an implementation detail: the tree they build has
//! no per-style structure, and [`detect_style`] is the only thing that cares
//! which one runs.
use fmt;
use GoogleSectionKind;
use crateDocstring;
use crateParsed;
pub
pub
pub
pub
pub
pub use parse_google;
pub use parse_numpy;
pub use parse_plain;
pub use TextBlock;
pub use TokenRef;
pub use Citation;
pub use DefaultMarker;
pub use Directive;
pub use Document;
pub use Entry;
pub use Section;
// =============================================================================
// Style
// =============================================================================
/// Docstring style identifier.
///
/// This enum is `#[non_exhaustive]`: new styles may be added in minor
/// releases (see [`detect_style`]), so downstream `match`es need a wildcard
/// arm.
// =============================================================================
// EntryRole
// =============================================================================
/// The role of the `ENTRY` nodes in a section body, derived from the section
/// kind.
///
/// This is the single mapping used both by the visitor (to route an `ENTRY`
/// to the right `visit_*` method) and by the typed section accessors (to
/// return empty for sections outside the accessor's role, e.g. `args()` on a
/// `Raises:` section).
pub
// =============================================================================
// Style detection
// =============================================================================
/// Detect the docstring style from its content.
///
/// Uses heuristics to identify the style:
/// 1. **NumPy**: Section headers followed by `---` underlines
/// 2. **Google**: Section headers ending with `:` (e.g., `Args:`, `Returns:`)
/// 3. Falls back to [`Style::Plain`] if no style-specific patterns are found.
/// This includes summary-only docstrings and unrecognised styles such as
/// Sphinx.
///
/// The set of recognised styles may grow in minor releases (e.g. Sphinx
/// field lists): input that detects as [`Style::Plain`] today may detect as
/// a new, more specific [`Style`] variant later. `Style` is
/// `#[non_exhaustive]` for exactly this reason.
///
/// # Example
///
/// ```rust
/// use pydocstring::parse::detect_style;
/// use pydocstring::parse::Style;
///
/// let numpy = "Summary.\n\nParameters\n----------\nx : int\n Description.";
/// assert_eq!(detect_style(numpy), Style::NumPy);
///
/// let google = "Summary.\n\nArgs:\n x: Description.";
/// assert_eq!(detect_style(google), Style::Google);
///
/// let plain = "Just a summary.";
/// assert_eq!(detect_style(plain), Style::Plain);
/// ```
// =============================================================================
// Unified parse entry point
// =============================================================================
/// Parse a docstring, auto-detecting its style.
///
/// Internally calls [`detect_style`] and dispatches to the appropriate parser.
/// The root node kind is always the style-neutral
/// [`SyntaxKind::DOCUMENT`](crate::syntax::SyntaxKind::DOCUMENT); the detected
/// style is recorded on the result and reported by
/// [`Parsed::style`](crate::syntax::Parsed::style).
///
/// # Example
///
/// ```rust
/// use pydocstring::parse::parse;
/// use pydocstring::parse::Style;
/// use pydocstring::syntax::SyntaxKind;
///
/// let result = parse("Summary.\n\nArgs:\n x: Description.");
/// assert_eq!(result.root().kind(), SyntaxKind::DOCUMENT);
/// assert_eq!(result.style(), Style::Google);
///
/// let plain = parse("Just a summary.");
/// assert_eq!(plain.style(), Style::Plain);
/// ```