cargo-rdme 2.0.0

Cargo command to create your `README.md` from your crate's documentation
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
use crate::transform::DocTransform;
use crate::transform::intralinks::links::{
    Link, MarkdownInlineLink, MarkdownLink, MarkdownReferenceLink, markdown_link_iterator,
    markdown_reference_link_definition_iterator,
};
use crate::transform::intralinks::rustdoc::{IntralinkResolver, create_intralink_resolver};
use crate::{Doc, PackageTarget};
use itertools::Itertools;
use std::borrow::Cow;
use std::collections::HashSet;
use std::fmt::Display;
use std::path::PathBuf;
use thiserror::Error;
use unicase::UniCase;

mod links;
mod rustdoc;

#[derive(Error, Debug)]
pub enum IntralinkError {
    #[error("failed to run rustdoc: {error}")]
    RustdocError {
        #[source]
        error: rustdoc_json::BuildError,
    },
    #[error("failed to run rustdoc:\n{stderr}")]
    BuildRustdocError { stderr: String },
    #[error("failed to read rustdoc json file: {io_error}")]
    ReadRustdocError {
        #[source]
        io_error: std::io::Error,
    },
    #[error("failed to parse rustdoc json file: {serde_error}")]
    ParseRustdocError { serde_error: serde_json::Error },
    #[error("unsupported rustdoc format version {version} (expected version {expected_version})")]
    UnsupportedRustdocFormatVersion { version: u32, expected_version: u32 },
    #[error("rust toolchain not installed: {expected}")]
    RustToolchainNotInstalled { expected: &'static str },
    #[error("failed to run rustup toolchain: {error}")]
    RustupToolchain { error: rustup_toolchain::Error },
}

#[derive(Default, Debug, PartialEq, Eq, Clone)]
pub struct IntralinksDocsRsConfig {
    pub docs_rs_base_url: Option<String>,
    pub docs_rs_version: Option<String>,
}

#[derive(Default, Debug, PartialEq, Eq, Clone)]
pub struct IntralinksConfig {
    pub docs_rs: IntralinksDocsRsConfig,
    pub strip_links: Option<bool>,
    pub all_features: Option<bool>,
    pub features: Option<Vec<String>>,
    pub no_default_features: Option<bool>,
}

pub struct DocTransformIntralinks<F> {
    package_name: String,
    package_target: PackageTarget,
    workspace_package: Option<String>,
    manifest_path: PathBuf,
    emit_warning: F,
    config: IntralinksConfig,
}

impl<F> DocTransformIntralinks<F>
where
    F: Fn(&str),
{
    pub fn new(
        package_name: impl Into<String>,
        package_target: PackageTarget,
        workspace_package: Option<String>,
        manifest_path: PathBuf,
        emit_warning: F,
        config: Option<IntralinksConfig>,
    ) -> DocTransformIntralinks<F> {
        DocTransformIntralinks {
            package_name: package_name.into(),
            package_target,
            workspace_package,
            manifest_path,
            emit_warning,
            config: config.unwrap_or_default(),
        }
    }
}

#[derive(PartialEq, Eq, Hash, Clone, Debug)]
struct ItemPath<'a> {
    segments: Cow<'a, [String]>,
}

impl<'a> ItemPath<'a> {
    fn new(segments: &'a [String]) -> ItemPath<'a> {
        assert!(!segments.is_empty(), "path item must not be empty");

        ItemPath { segments: Cow::Borrowed(segments) }
    }

    fn add(&self, segment: String) -> ItemPath<'static> {
        let mut segments = self.segments.clone().into_owned();

        segments.push(segment);

        ItemPath { segments: Cow::Owned(segments) }
    }

    fn segments(&self) -> impl Iterator<Item = &str> {
        self.segments.iter().map(String::as_str)
    }

    fn len(&self) -> usize {
        self.segments.len()
    }
}

impl Display for ItemPath<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // TODO Use standard library intersperse() one it stabilizes (https://github.com/rust-lang/rust/issues/79524).
        let iter = Itertools::intersperse(self.segments.iter().map(String::as_str), "::");

        for s in iter {
            f.write_str(s)?;
        }

        Ok(())
    }
}

fn has_intralinks(doc: &Doc) -> bool {
    let link_targets =
        markdown_link_iterator(&doc.markdown).items().filter_map(|link| match link {
            MarkdownLink::Inline { link } => Some(link.link),
            MarkdownLink::Reference { link: MarkdownReferenceLink::Shortcut { text } }
                if is_intralink_shortcut(text.as_str()) =>
            {
                Some(Link::new(text.as_str().to_owned()))
            }
            MarkdownLink::Reference { .. } => None,
        });
    let reference_links = markdown_reference_link_definition_iterator(&doc.markdown)
        .items()
        .map(|link_def| link_def.link);

    link_targets.chain(reference_links).any(|link| IntralinkResolver::is_intralink(&link))
}

impl<F> DocTransform for DocTransformIntralinks<F>
where
    F: Fn(&str),
{
    type E = IntralinkError;

    fn transform(&self, doc: &Doc) -> Result<Doc, IntralinkError> {
        // If there are no intralinks return immediately. No need to run `rustdoc` at all.
        if !has_intralinks(doc) {
            return Ok(doc.clone());
        }

        let strip_links = self.config.strip_links.unwrap_or(false);

        let intralink_resolver: IntralinkResolver<'_> = match strip_links {
            true => {
                // Create an empty resolver, since we are going to strip all intralinks.
                IntralinkResolver::new(self.package_name.as_str(), &self.config.docs_rs)
            }
            false => create_intralink_resolver(
                self.package_name.as_str(),
                &self.package_target,
                self.workspace_package.as_deref(),
                &self.manifest_path,
                &self.config,
            )?,
        };

        let doc = rewrite_links(doc, &intralink_resolver, &self.emit_warning, &self.config);

        Ok(doc)
    }
}

fn rewrite_links(
    doc: &Doc,
    intralink_resolver: &IntralinkResolver,
    emit_warning: &impl Fn(&str),
    config: &IntralinksConfig,
) -> Doc {
    let RewriteReferenceLinksResult { doc, reference_links_to_remove } =
        rewrite_reference_links_definitions(doc, intralink_resolver, emit_warning, config);

    rewrite_markdown_links(
        &doc,
        intralink_resolver,
        emit_warning,
        config,
        &reference_links_to_remove,
    )
}

enum MarkdownLinkAction {
    Link(Link),
    Preserve,
    Strip,
}

fn ensure_backticked(text: &str) -> String {
    let is_backticked = text.len() >= 2 && text.starts_with('`') && text.ends_with('`');

    match is_backticked {
        true => text.to_owned(),
        false => format!("`{text}`"),
    }
}

/// A shortcut link `[text]` looks like a Rust intralink if it is wrapped in backticks
/// (`` [`Foo`] ``), contains a path separator (`[foo::Bar]`), or is a bare identifier (`[Foo]`).
/// The three conditions overlap by design: backticked text typically wraps either an identifier
/// or a path. We check each independently to keep the logic readable.
///
/// Intentionally permissive: in non-strip mode, false positives just miss in the resolver and
/// fall through; in strip mode (where the resolver is empty), this is the only signal we have.
fn is_intralink_shortcut(text: &str) -> bool {
    let backticked = text.len() >= 2 && text.starts_with('`') && text.ends_with('`');
    let has_path_separator = text.contains("::");
    let is_bare_identifier =
        !text.is_empty() && text.chars().all(|c| c.is_alphanumeric() || c == '_');

    backticked || has_path_separator || is_bare_identifier
}

fn resolve_shortcut_intralink(
    link: &MarkdownReferenceLink,
    intralink_resolver: &IntralinkResolver,
    strip_links: bool,
) -> Option<MarkdownLinkAction> {
    let MarkdownReferenceLink::Shortcut { text } = link else {
        return None;
    };

    if strip_links {
        return is_intralink_shortcut(text.as_str()).then_some(MarkdownLinkAction::Strip);
    }

    let candidate = Link::new(text.as_str().to_owned());
    let url = intralink_resolver.resolve_link(&candidate)?;

    let url = match candidate.link_fragment() {
        Some(fragment) if !url.contains('#') => format!("{url}#{fragment}"),
        _ => url.to_owned(),
    };

    Some(MarkdownLinkAction::Link(url.into()))
}

fn markdown_link(
    link: &Link,
    intralink_resolver: &IntralinkResolver,
    emit_warning: &impl Fn(&str),
) -> MarkdownLinkAction {
    assert!(IntralinkResolver::is_intralink(link));

    match intralink_resolver.resolve_link(link) {
        None => {
            emit_warning(&format!("Could not resolve definition of `{}`.", link.symbol()));

            MarkdownLinkAction::Strip
        }
        Some(url) => {
            // For impl items the resolved URL already carries an implicit fragment (e.g.
            // `#method.foo`), so we drop any user-supplied fragment to avoid emitting a URL with
            // two `#`s.
            let url = match link.link_fragment() {
                Some(fragment) if !url.contains('#') => format!("{url}#{fragment}"),
                _ => url.to_owned(),
            };

            MarkdownLinkAction::Link(url.into())
        }
    }
}

fn rewrite_markdown_links(
    doc: &Doc,
    intralink_resolver: &IntralinkResolver,
    emit_warning: &impl Fn(&str),
    config: &IntralinksConfig,
    reference_links_to_remove: &HashSet<UniCase<String>>,
) -> Doc {
    use crate::utils::ItemOrOther;

    let strip_links = config.strip_links.unwrap_or(false);
    let mut new_doc = String::with_capacity(doc.as_string().len() + 1024);

    for item_or_other in markdown_link_iterator(&doc.markdown).complete() {
        match item_or_other {
            ItemOrOther::Item(MarkdownLink::Inline { link: inline_link }) => {
                let markdown_link: MarkdownLinkAction =
                    match IntralinkResolver::is_intralink(&inline_link.link) {
                        true => match strip_links {
                            false => {
                                markdown_link(&inline_link.link, intralink_resolver, emit_warning)
                            }
                            true => MarkdownLinkAction::Strip,
                        },
                        false => MarkdownLinkAction::Preserve,
                    };

                match markdown_link {
                    MarkdownLinkAction::Link(markdown_link) => {
                        new_doc.push_str(&inline_link.with_link(markdown_link).to_string());
                    }
                    MarkdownLinkAction::Preserve => {
                        new_doc.push_str(&inline_link.to_string());
                    }
                    MarkdownLinkAction::Strip => {
                        new_doc.push_str(&inline_link.text);
                    }
                }
            }
            ItemOrOther::Item(MarkdownLink::Reference { link }) => {
                if reference_links_to_remove.contains(link.label()) {
                    new_doc.push_str(link.text());
                } else if let Some(action) =
                    resolve_shortcut_intralink(&link, intralink_resolver, strip_links)
                {
                    // Shortcut intralinks render as code in rustdoc HTML; preserve that by
                    // ensuring the link text is backticked in the README output.
                    let backticked = ensure_backticked(link.text());

                    match action {
                        MarkdownLinkAction::Link(resolved) => {
                            let inline = MarkdownInlineLink { text: backticked, link: resolved };

                            new_doc.push_str(&inline.to_string());
                        }
                        MarkdownLinkAction::Strip => new_doc.push_str(&backticked),
                        MarkdownLinkAction::Preserve => new_doc.push_str(&link.to_string()),
                    }
                } else {
                    new_doc.push_str(&link.to_string());
                }
            }
            ItemOrOther::Other(other) => {
                new_doc.push_str(other);
            }
        }
    }

    Doc::from_str(new_doc)
}

struct RewriteReferenceLinksResult {
    doc: Doc,
    reference_links_to_remove: HashSet<UniCase<String>>,
}

fn rewrite_reference_links_definitions(
    doc: &Doc,
    intralink_resolver: &IntralinkResolver,
    emit_warning: &impl Fn(&str),
    config: &IntralinksConfig,
) -> RewriteReferenceLinksResult {
    use crate::utils::ItemOrOther;
    let mut reference_links_to_remove = HashSet::new();
    let mut new_doc = String::with_capacity(doc.as_string().len() + 1024);
    let mut skip_next_newline = false;
    let strip_links = config.strip_links.unwrap_or(false);

    let iter = markdown_reference_link_definition_iterator(&doc.markdown);

    for item_or_other in iter.complete() {
        match item_or_other {
            ItemOrOther::Item(link_ref_def) => {
                let markdown_link: MarkdownLinkAction =
                    match IntralinkResolver::is_intralink(&link_ref_def.link) {
                        true => match strip_links {
                            false => {
                                markdown_link(&link_ref_def.link, intralink_resolver, emit_warning)
                            }
                            true => MarkdownLinkAction::Strip,
                        },
                        false => MarkdownLinkAction::Preserve,
                    };

                match markdown_link {
                    MarkdownLinkAction::Link(link) => {
                        new_doc.push_str(&link_ref_def.with_link(link).to_string());
                    }
                    MarkdownLinkAction::Preserve => {
                        new_doc.push_str(&link_ref_def.to_string());
                    }
                    MarkdownLinkAction::Strip => {
                        // Do not emit anything to new_doc.
                        reference_links_to_remove.insert(link_ref_def.label);
                        skip_next_newline = true;
                    }
                }
            }
            ItemOrOther::Other(other) => {
                let other = match skip_next_newline {
                    true => {
                        skip_next_newline = false;
                        let next_index = other
                            .chars()
                            .enumerate()
                            .skip_while(|(_, c)| c.is_whitespace() && *c != '\n')
                            .skip(1)
                            .map(|(i, _)| i)
                            .next();

                        next_index.and_then(|i| other.get(i..)).unwrap_or("")
                    }
                    false => other,
                };
                new_doc.push_str(other);
            }
        }
    }

    RewriteReferenceLinksResult { doc: Doc::from_str(new_doc), reference_links_to_remove }
}

#[cfg(test)]
mod tests {
    use super::*;
    use pretty_assertions::assert_eq;

    #[test]
    fn test_ensure_backticked() {
        assert_eq!(ensure_backticked("Foo"), "`Foo`");
        assert_eq!(ensure_backticked("foo::Bar"), "`foo::Bar`");
        assert_eq!(ensure_backticked("`Foo`"), "`Foo`");
        assert_eq!(ensure_backticked("`foo::Bar`"), "`foo::Bar`");
        assert_eq!(ensure_backticked(""), "``");
        // A lone backtick is not a matched pair.
        assert_eq!(ensure_backticked("`"), "```");
        // Already-paired empty backticks stay as is.
        assert_eq!(ensure_backticked("``"), "``");
        // Inner backticks are not stripped.
        assert_eq!(ensure_backticked("a`b"), "`a`b`");
    }

    #[test]
    fn test_is_intralink_shortcut() {
        // Bare identifiers.
        assert!(is_intralink_shortcut("Foo"));
        assert!(is_intralink_shortcut("foo"));
        assert!(is_intralink_shortcut("_foo"));
        assert!(is_intralink_shortcut("Foo123"));

        // Path-separated forms.
        assert!(is_intralink_shortcut("foo::Bar"));
        assert!(is_intralink_shortcut("crate::foo::Bar"));
        assert!(is_intralink_shortcut("a b::c")); // whitespace ok if `::` is present.

        // Backticked forms.
        assert!(is_intralink_shortcut("`Foo`"));
        assert!(is_intralink_shortcut("`foo::Bar`"));
        assert!(is_intralink_shortcut("`Foo()`"));
        assert!(is_intralink_shortcut("`Foo!`"));

        // Rejected: not intralink-shaped.
        assert!(!is_intralink_shortcut("some text"));
        assert!(!is_intralink_shortcut("Foo!"));
        assert!(!is_intralink_shortcut("Foo()"));
        assert!(!is_intralink_shortcut(""));
        assert!(!is_intralink_shortcut("`Foo")); // unmatched backtick, no `::`, has backtick.
    }
}