#hiding #line #block #doctest #rustdoc #documentation #dedicated

macro doctest-file

Procedural macro that sources doctests from dedicated files into Rustdoc documentation with support for hiding lines

1 stable release

1.0.0 May 17, 2024

#963 in Procedural macros

Download history 8710/week @ 2024-07-22 9231/week @ 2024-07-29 11586/week @ 2024-08-05 11637/week @ 2024-08-12 12452/week @ 2024-08-19 13406/week @ 2024-08-26 16350/week @ 2024-09-02 14332/week @ 2024-09-09 15153/week @ 2024-09-16 19143/week @ 2024-09-23 20987/week @ 2024-09-30 24193/week @ 2024-10-07 28086/week @ 2024-10-14 28703/week @ 2024-10-21 24916/week @ 2024-10-28 22592/week @ 2024-11-04

105,868 downloads per month
Used in 39 crates (via interprocess)

0BSD license

11KB
232 lines

Procedural macro that sources doctests from dedicated files into Rustdoc documentation with support for hiding lines.

Example usage:

/// ```
#[doc = doctest_file::include_doctest!("examples/foo.rs")]
/// ```

Unlike include_str!(), include_doctest!() allows you to hide certain lines, just as you can with in-band doctests using the # syntax. Instead of prefixing every line which is to be hidden with #, though, you can instead suffix them with //. An empty end-of-line comment does not make an .rs file malformed, so you can receive rich editor support while writing doctests and put them in the examples/ directory. Note that whitespace is ignored during detection of this suffix.

Under the hood, // suffixes are simply translated to # prefixes, which are then interpreted by Rustdoc.

Blocks of lines can also be hidden – this helps work around Rustfmt's inflexible formatting choices. //{ starts a hidden block, which only includes the line it is on if there is nothing on the line other than that marker (aside from whitespace). //} ends a hidden block whilst unconditionally hiding the line it's on.

Another feature that comes in handy when storing doctests in separate files is auto-dedent: the macro will reduce indentation of visible lines such that the least indented visible line appears completely unindented. This means that you don't need to bend the indentation rules that are enforced by Rustfmt to get your doctest looking right in Rustdoc.

Additionally, blank lines at the end of the file are removed, also helping interoperability with Rustfmt.

This crate has zero dependencies: no proc_macro2, no syn, no quote. It is also incredibly simple, at just over 200 lines of code.

License

doctest-file is licensed under the 0-clause BSD license, meaning that it is impossible to violate the licensing terms. This is functionally equivalent to the public domain, but is also legal in countries such as Germany.

No runtime deps