Skip to content

Support include_str! in crate-level doc attributes - #296

Closed
schneems wants to merge 2 commits into
orium:mainfrom
schneems:include-str-doc-support
Closed

schneems wants to merge 2 commits into
orium:mainfrom
schneems:include-str-doc-support

Conversation

@schneems

Copy link
Copy Markdown

Context

Using include_str! is a great way to keep docs DRY (single point of truth) and up-to-date. The same contents can be reused in multiple places, and the contents of the file can be used in assertions in unit tests as well.

One use case is my crate https://crates.io/crates/path_facts. It is used to display facts about a path on disk. To make a consistent output, in tests, I need to create a temp dir, and manipulate it. But this produces unpredictable output as the tempdir changes every time. In unit tests, I have helpers that normalize these values so the output renders consistently:

Path does not exist `/path/to/directory/a.txt/b/c/does_not_exist.txt`
                                        ^^^^^
                                        ↳ File, not a dir [✅ read, ✅ write, ❌ execute]

I want to show this in my docs as well, but the sanitization infrastructure isn't part of the public interface (and doc tests require you to import only public interfaces). So one solution is to use those contents via an include_str! in my unit tests, and then to render the contents in my docs.

I can use this approach everywhere EXCEPT for my lib.rs module docs, since I'm using cargo-rdme to keep my README DRY.

This PR

Adds support for #![doc = include_str!("<path>")] (only, no other macros) to cargo-rdme. It does this by parsing the directive and special casing doc = include_str and then re-implementing the (relatively simple) macro logic to pull in the contents of that file.

Closes #178

## Context

Using `include_str!` is a great way to keep docs DRY (single point of truth) and up-to-date. The same contents can be reused in multiple places, and the contents of the file can be used in assertions in unit tests as well.

One use case is my crate https://crates.io/crates/path_facts. It is used to display facts about a path on disk. To make a consistent output, in tests, I need to create a temp dir, and manipulate it. But this produces unpredictable output as the tempdir changes every time. In unit tests, I have helpers that normalize these values so the output renders consistently:

```
Path does not exist `/path/to/directory/a.txt/b/c/does_not_exist.txt`
                                        ^^^^^
                                        ↳ File, not a dir [✅ read, ✅ write, ❌ execute]
```

I want to show this in my docs as well, but the sanitization infrastructure isn't part of the public interface (and doc tests require you to import only public interfaces). So one solution is to use those contents via an `include_str!` in my unit tests, and then to render the contents in my docs.

I can use this approach everywhere EXCEPT for my `lib.rs` module docs, since I'm using `cargo-rdme` to keep my README DRY.

## This PR

Adds support for `#![doc = include_str!("<path>")]` (only, no other macros) to cargo-rdme. It does this by parsing the directive and special casing `doc = include_str` and then re-implementing the (relatively simple) macro logic to pull in the contents of that file.

Closes orium#178
@schneems
schneems force-pushed the include-str-doc-support branch from f5a5a4f to ecf909b Compare September 23, 2026 21:13
`include_str!` may be invoked through its fully-qualified `std` path.
Match both the bare `include_str` identifier and `std::include_str`
so the included content is not silently dropped.
@schneems
schneems marked this pull request as ready for review September 24, 2026 14:35
@schneems
schneems marked this pull request as draft September 24, 2026 14:41
@schneems

Copy link
Copy Markdown
Author

Closing in favor of #297 which supports ALL macros and is (therefore) a little less hacky.

@schneems schneems closed this Sep 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feature req: handle #![doc]

1 participant