cargo-readme 3.4.0

A cargo subcommand to generate README.md content from doc comments
Documentation
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
//! Transform code blocks from rustdoc into markdown
//!
//! Rewrite code block start tags, changing rustdoc into equivalent in markdown:
//! - code blocks that rustdoc treats as rust are converted to "```rust"
//! - "```text" has its language stripped, becoming a plain "```"
//! - markdown heading are indentend to be one level lower, so the crate name is at the top level

use regex::Regex;
use std::{
    iter::{IntoIterator, Iterator},
    sync::LazyLock,
};

// A fenced code block opening: 3-4 backticks/tildes and an optional info string.
static RE_CODE_FENCE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"^(?P<delimiter>`{3,4}|~{3,4})(?P<info>.*)$").unwrap());

/// Does this code block info string denote rust?
///
/// Mirrors rustdoc's `LangString::parse` (an unpublished, `rustc_private`
/// function we cannot depend on directly), ported from rust-lang/rust@a9cf06b5.
/// A block is rust unless a `custom` tag appears, or an unrecognized tag appears
/// without an explicit `rust` tag. To check for drift, diff `LangString::parse`
/// against this function.
///
/// Typo variants like `should-panic` or `norun` are deliberately not rust:
/// rustdoc treats them as markers for other languages, so we keep them verbatim.
fn is_rust_code(info: &str) -> bool {
    let mut seen_rust_tags = false;
    let mut seen_other_tags = false;
    let mut seen_custom_tag = false;

    for token in info.split([' ', ',', '\t']).filter(|t| !t.is_empty()) {
        match token {
            "should_panic" | "no_run" | "ignore" => {
                seen_rust_tags = !seen_other_tags;
            }
            "rust" => {
                seen_rust_tags = true;
            }
            "custom" => {
                seen_custom_tag = true;
            }
            "test_harness" | "compile_fail" | "standalone_crate" => {
                seen_rust_tags = !seen_other_tags || seen_rust_tags;
            }
            _ if token.starts_with("ignore-") => {
                seen_rust_tags = !seen_other_tags;
            }
            // `edition*` sets the edition only; it is neither a rust nor an other tag.
            _ if token.starts_with("edition") => {}
            _ if is_error_code(token) => {
                seen_rust_tags = !seen_other_tags || seen_rust_tags;
            }
            _ => {
                seen_other_tags = true;
            }
        }
    }

    !seen_custom_tag && (!seen_other_tags || seen_rust_tags)
}

/// A rustdoc error-code tag: `E` followed by exactly four ASCII digits (e.g. `E0277`).
fn is_error_code(token: &str) -> bool {
    matches!(token.strip_prefix('E'), Some(rest) if rest.len() == 4 && rest.bytes().all(|b| b.is_ascii_digit()))
}

/// Process and concatenate the doc lines into a single String
///
/// The processing transforms doc tests into regular rust code blocks and optionally indent the
/// markdown headings in order to leave the top heading to the crate name
pub fn process_docs<S: Into<String>, L: Into<Vec<S>>>(
    lines: L,
    indent_headings: bool,
) -> Vec<String> {
    lines.into().into_iter().process_docs(indent_headings)
}

pub struct Processor {
    section: Section,
    indent_headings: bool,
    delimiter: Option<String>,
}

impl Processor {
    pub fn new(indent_headings: bool) -> Self {
        Processor {
            section: Section::None,
            indent_headings,
            delimiter: None,
        }
    }

    pub fn process_line(&mut self, mut line: String) -> Option<String> {
        // Skip lines that should be hidden in docs
        if self.section == Section::CodeRust && line.trim_ascii_start().starts_with("# ") {
            return None;
        }

        // indent heading when outside code
        if self.indent_headings && self.section == Section::None && line.starts_with("#") {
            line.insert(0, '#');
        } else if self.section == Section::None {
            let l = line.clone();
            if let Some(cap) = RE_CODE_FENCE.captures(&l) {
                let delimiter = cap.name("delimiter").unwrap().as_str();
                let info = cap.name("info").unwrap().as_str();
                self.delimiter = Some(delimiter.to_owned());
                if is_rust_code(info) {
                    self.section = Section::CodeRust;
                    line = format!("{delimiter}rust");
                } else {
                    self.section = Section::CodeOther;
                    // "```text" is stripped to a plain fence; other languages are kept as-is.
                    if info.trim() == "text" {
                        line = delimiter.to_owned();
                    }
                }
            }
        } else if self.section != Section::None && Some(&line) == self.delimiter.as_ref() {
            self.section = Section::None;
            line = self.delimiter.take().unwrap_or("```".to_owned());
        }

        Some(line)
    }
}

#[derive(PartialEq)]
enum Section {
    CodeRust,
    CodeOther,
    None,
}

pub trait DocProcess<S: Into<String>> {
    fn process_docs(self, indent_headings: bool) -> Vec<String>
    where
        Self: Sized + Iterator<Item = S>,
    {
        let mut p = Processor::new(indent_headings);
        self.into_iter()
            .filter_map(|line| p.process_line(line.into()))
            .collect()
    }
}

impl<S: Into<String>, I: Iterator<Item = S>> DocProcess<S> for I {}

#[cfg(test)]
mod tests {
    use super::process_docs;

    const INPUT_HIDDEN_LINE: &[&str] = &[
        "```",
        "#[visible]",
        "let visible = \"visible\";",
        "# let hidden = \"hidden\";",
        "```",
    ];

    const EXPECTED_HIDDEN_LINE: &[&str] =
        &["```rust", "#[visible]", "let visible = \"visible\";", "```"];

    #[test]
    fn hide_line_in_rust_code_block() {
        let result = process_docs(INPUT_HIDDEN_LINE, true);
        assert_eq!(result, EXPECTED_HIDDEN_LINE);
    }

    const INPUT_NOT_HIDDEN_LINE: &[&str] = &[
        "```",
        "let visible = \"visible\";",
        "# let hidden = \"hidden\";",
        "```",
        "",
        "```python",
        "# this line is visible",
        "visible = True",
        "```",
    ];

    const EXPECTED_NOT_HIDDEN_LINE: &[&str] = &[
        "```rust",
        "let visible = \"visible\";",
        "```",
        "",
        "```python",
        "# this line is visible",
        "visible = True",
        "```",
    ];

    #[test]
    fn do_not_hide_line_in_code_block() {
        let result = process_docs(INPUT_NOT_HIDDEN_LINE, true);
        assert_eq!(result, EXPECTED_NOT_HIDDEN_LINE);
    }

    const INPUT_RUST_CODE_BLOCK: &[&str] = &[
        "```",
        "let block = \"simple code block\";",
        "```",
        "",
        "```no_run",
        "let run = false;",
        "```",
        "",
        "```ignore",
        "let ignore = true;",
        "```",
        "",
        "```should_panic",
        "panic!(\"at the disco\");",
        "```",
        "",
        "```compile_fail",
        "x y z",
        "```",
        "",
        "```C",
        "int i = 0; // no rust code",
        "```",
    ];

    const EXPECTED_RUST_CODE_BLOCK: &[&str] = &[
        "```rust",
        "let block = \"simple code block\";",
        "```",
        "",
        "```rust",
        "let run = false;",
        "```",
        "",
        "```rust",
        "let ignore = true;",
        "```",
        "",
        "```rust",
        "panic!(\"at the disco\");",
        "```",
        "",
        "```rust",
        "x y z",
        "```",
        "",
        "```C",
        "int i = 0; // no rust code",
        "```",
    ];

    #[test]
    fn transform_rust_code_block() {
        let result = process_docs(INPUT_RUST_CODE_BLOCK, true);
        assert_eq!(result, EXPECTED_RUST_CODE_BLOCK);
    }

    const INPUT_RUST_CODE_BLOCK_RUST_PREFIX: &[&str] = &[
        "```rust",
        "let block = \"simple code block\";",
        "```",
        "",
        "```rust,no_run",
        "let run = false;",
        "```",
        "",
        "```rust,ignore",
        "let ignore = true;",
        "```",
        "",
        "```rust,should_panic",
        "panic!(\"at the disco\");",
        "```",
        "",
        "```rust,compile_fail",
        "x y z",
        "```",
        "",
        "```C",
        "int i = 0; // no rust code",
        "```",
    ];

    #[test]
    fn transform_rust_code_block_with_prefix() {
        let result = process_docs(INPUT_RUST_CODE_BLOCK_RUST_PREFIX, true);
        assert_eq!(result, EXPECTED_RUST_CODE_BLOCK);
    }

    const INPUT_RUST_CODE_BLOCK_ALL_ANNOTATIONS: &[&str] = &[
        "```test_harness",
        "let a = test_harness;",
        "```",
        "",
        "```standalone_crate",
        "let a = standalone;",
        "```",
        "",
        "```edition2018",
        "let a = edition;",
        "```",
        "",
        "```E0277",
        "let a = error_code;",
        "```",
        "",
        "```ignore-windows",
        "let a = ignore_target;",
        "```",
    ];

    const EXPECTED_RUST_CODE_BLOCK_ALL_ANNOTATIONS: &[&str] = &[
        "```rust",
        "let a = test_harness;",
        "```",
        "",
        "```rust",
        "let a = standalone;",
        "```",
        "",
        "```rust",
        "let a = edition;",
        "```",
        "",
        "```rust",
        "let a = error_code;",
        "```",
        "",
        "```rust",
        "let a = ignore_target;",
        "```",
    ];

    #[test]
    fn transform_rust_code_block_all_annotations() {
        let result = process_docs(INPUT_RUST_CODE_BLOCK_ALL_ANNOTATIONS, true);
        assert_eq!(result, EXPECTED_RUST_CODE_BLOCK_ALL_ANNOTATIONS);
    }

    const INPUT_RUST_CODE_BLOCK_MULTIPLE_TAGS: &[&str] = &[
        "```rust,no_run",
        "let a = 1;",
        "```",
        "",
        "```no_run,should_panic",
        "let a = 2;",
        "```",
        "",
        "```rust,edition2021,compile_fail",
        "let a = 3;",
        "```",
    ];

    const EXPECTED_RUST_CODE_BLOCK_MULTIPLE_TAGS: &[&str] = &[
        "```rust",
        "let a = 1;",
        "```",
        "",
        "```rust",
        "let a = 2;",
        "```",
        "",
        "```rust",
        "let a = 3;",
        "```",
    ];

    #[test]
    fn transform_rust_code_block_multiple_tags() {
        let result = process_docs(INPUT_RUST_CODE_BLOCK_MULTIPLE_TAGS, true);
        assert_eq!(result, EXPECTED_RUST_CODE_BLOCK_MULTIPLE_TAGS);
    }

    // rustdoc treats typo variants (hyphenated / run-together) as markers for
    // *other* languages, emitting a "did you mean" lint rather than compiling
    // them as Rust. We faithfully keep them verbatim as non-Rust blocks.
    const INPUT_TYPO_ANNOTATIONS_NOT_RUST: &[&str] = &[
        "```should-panic",
        "let a = 1;",
        "```",
        "",
        "```norun",
        "let a = 2;",
        "```",
        "",
        "```compile-fail",
        "let a = 3;",
        "```",
    ];

    #[test]
    fn typo_annotations_are_not_rust() {
        let result = process_docs(INPUT_TYPO_ANNOTATIONS_NOT_RUST, true);
        assert_eq!(result, INPUT_TYPO_ANNOTATIONS_NOT_RUST);
    }

    const INPUT_TEXT_BLOCK: &[&str] = &["```text", "this is text", "```"];

    const EXPECTED_TEXT_BLOCK: &[&str] = &["```", "this is text", "```"];

    #[test]
    fn transform_text_block() {
        let result = process_docs(INPUT_TEXT_BLOCK, true);
        assert_eq!(result, EXPECTED_TEXT_BLOCK);
    }

    const INPUT_OTHER_CODE_BLOCK_WITH_SYMBOLS: &[&str] = &[
        "```html,django",
        "{% if True %}True{% endif %}",
        "```",
        "",
        "```html+django",
        "{% if True %}True{% endif %}",
        "```",
    ];

    #[test]
    fn transform_other_code_block_with_symbols() {
        let result = process_docs(INPUT_OTHER_CODE_BLOCK_WITH_SYMBOLS, true);
        assert_eq!(result, INPUT_OTHER_CODE_BLOCK_WITH_SYMBOLS);
    }

    const INPUT_INDENT_HEADINGS: &[&str] = &[
        "# heading 1",
        "some text",
        "## heading 2",
        "some other text",
    ];

    const EXPECTED_INDENT_HEADINGS: &[&str] = &[
        "## heading 1",
        "some text",
        "### heading 2",
        "some other text",
    ];

    #[test]
    fn indent_markdown_headings() {
        let result = process_docs(INPUT_INDENT_HEADINGS, true);
        assert_eq!(result, EXPECTED_INDENT_HEADINGS);
    }

    #[test]
    fn do_not_indent_markdown_headings() {
        let result = process_docs(INPUT_INDENT_HEADINGS, false);
        assert_eq!(result, INPUT_INDENT_HEADINGS);
    }

    const INPUT_ALTERNATE_DELIMITER_4_BACKTICKS: &[&str] = &["````", "let i = 1;", "````"];

    const EXPECTED_ALTERNATE_DELIMITER_4_BACKTICKS: &[&str] = &["````rust", "let i = 1;", "````"];

    #[test]
    fn alternate_delimiter_4_backticks() {
        let result = process_docs(INPUT_ALTERNATE_DELIMITER_4_BACKTICKS, false);
        assert_eq!(result, EXPECTED_ALTERNATE_DELIMITER_4_BACKTICKS);
    }

    const INPUT_ALTERNATE_DELIMITER_4_BACKTICKS_NESTED: &[&str] = &[
        "````",
        "//! ```",
        "//! let i = 1;",
        "//! ```",
        "```python",
        "i = 1",
        "```",
        "````",
    ];

    const EXPECTED_ALTERNATE_DELIMITER_4_BACKTICKS_NESTED: &[&str] = &[
        "````rust",
        "//! ```",
        "//! let i = 1;",
        "//! ```",
        "```python",
        "i = 1",
        "```",
        "````",
    ];

    #[test]
    fn alternate_delimiter_4_backticks_nested() {
        let result = process_docs(INPUT_ALTERNATE_DELIMITER_4_BACKTICKS_NESTED, false);
        assert_eq!(result, EXPECTED_ALTERNATE_DELIMITER_4_BACKTICKS_NESTED);
    }

    const INPUT_ALTERNATE_DELIMITER_3_TILDES: &[&str] = &["~~~", "let i = 1;", "~~~"];

    const EXPECTED_ALTERNATE_DELIMITER_3_TILDES: &[&str] = &["~~~rust", "let i = 1;", "~~~"];

    #[test]
    fn alternate_delimiter_3_tildes() {
        let result = process_docs(INPUT_ALTERNATE_DELIMITER_3_TILDES, false);
        assert_eq!(result, EXPECTED_ALTERNATE_DELIMITER_3_TILDES);
    }

    const INPUT_ALTERNATE_DELIMITER_4_TILDES: &[&str] = &["~~~~", "let i = 1;", "~~~~"];

    const EXPECTED_ALTERNATE_DELIMITER_4_TILDES: &[&str] = &["~~~~rust", "let i = 1;", "~~~~"];

    #[test]
    fn alternate_delimiter_4_tildes() {
        let result = process_docs(INPUT_ALTERNATE_DELIMITER_4_TILDES, false);
        assert_eq!(result, EXPECTED_ALTERNATE_DELIMITER_4_TILDES);
    }

    const INPUT_ALTERNATE_DELIMITER_MIXED: &[&str] = &[
        "```",
        "let i = 1;",
        "```",
        "````",
        "//! ```",
        "//! let i = 1;",
        "//! ```",
        "```python",
        "i = 1",
        "```",
        "````",
        "~~~markdown",
        "```python",
        "i = 1",
        "```",
        "~~~",
    ];

    const EXPECTED_ALTERNATE_DELIMITER_MIXED: &[&str] = &[
        "```rust",
        "let i = 1;",
        "```",
        "````rust",
        "//! ```",
        "//! let i = 1;",
        "//! ```",
        "```python",
        "i = 1",
        "```",
        "````",
        "~~~markdown",
        "```python",
        "i = 1",
        "```",
        "~~~",
    ];

    #[test]
    fn alternate_delimiter_mixed() {
        let result = process_docs(INPUT_ALTERNATE_DELIMITER_MIXED, false);
        assert_eq!(result, EXPECTED_ALTERNATE_DELIMITER_MIXED);
    }
}