14 releases

0.3.3 Mar 17, 2020
0.3.2 Mar 7, 2020
0.3.1 Apr 28, 2019
0.2.4 Apr 15, 2019
0.1.2 Mar 29, 2018
Download history 35210/week @ 2019-12-17 18888/week @ 2019-12-24 23051/week @ 2019-12-31 38441/week @ 2020-01-07 34273/week @ 2020-01-14 38635/week @ 2020-01-21 35310/week @ 2020-01-28 30240/week @ 2020-02-04 23993/week @ 2020-02-11 23303/week @ 2020-02-18 23629/week @ 2020-02-25 25719/week @ 2020-03-03 27980/week @ 2020-03-10 31843/week @ 2020-03-17 31920/week @ 2020-03-24 31620/week @ 2020-03-31

121,766 downloads per month
Used in 276 crates (95 directly)

MIT license

9KB
57 lines

doc-comment

Write doc comments from macros.

Usage example

// Of course, we need to import the `doc_comment` macro:
#[macro_use]
extern crate doc_comment;

// If you want to test examples in your README file.
doctest!("../README.md");

// If you want to test your README file ONLY on "cargo test":
#[cfg(doctest)]
doctest!("../README.md");

// If you want to document an item:
doc_comment!(concat!("fooo", "or not foo"), pub struct Foo {});

For more information, take a look at the documentation.


lib.rs:

The point of this (small) crate is to allow you to add doc comments from macros or to test external markdown files' code blocks through rustdoc.

Including file(s) for testing

Let's assume you want to test code examples in your README.md file which looks like this:

# A crate

Here is a code example:

```rust
let x = 2;
assert!(x != 0);
```

You can use the doc_comment! macro to test it like this:

#[macro_use]
extern crate doc_comment;

// When running `cargo test`, rustdoc will check this file as well.
doc_comment!(include_str!("../README.md"));
# fn main() {}

Please note that can also use the doctest! macro to have a shorter way to test an outer file:

#[macro_use]
extern crate doc_comment;

doctest!("../README.md");
# fn main() {}

Please also note that you can use #[cfg(doctest)]:

# #[macro_use]
# extern crate doc_comment;
#[cfg(doctest)]
doctest!("../README.md");
# fn main() {}

In this case, the examples in the README.md file will only be run on cargo test. You can find more information about #[cfg(doctest)] in this blogpost.

Generic documentation

Now let's imagine you want to write documentation once for multiple types but still having examples specific to each type:

// The macro which generates types
macro_rules! gen_types {
    ($tyname:ident) => {
        /// This is a wonderful generated struct!
        ///
        /// You can use it as follow:
        ///
        /// ```
        /// let x = FirstOne {
        ///     field1: 0,
        ///     field2: 0,
        ///     field3: 0,
        ///     field4: 0,
        /// };
        ///
        /// println!("Created a new instance of FirstOne: {:?}", x);
        /// ```
        #[derive(Debug)]
        pub struct $tyname {
            pub field1: u8,
            pub field2: u16,
            pub field3: u32,
            pub field4: u64,
        }
    }
}

// Now let's actually generate types:
gen_types!(FirstOne);
gen_types!(SecondOne);
gen_types!(Another);

So now we have created three structs with different names, but they all have the exact same documentation, which is an issue for any structs not called FirstOne. That's where [doc_comment!] macro comes in handy!

Let's rewrite the gen_types! macro:

 // Of course, we need to import the `doc_comment` macro:
 #[macro_use]
 extern crate doc_comment;

 macro_rules! gen_types {
     ($tyname:ident) => {
         doc_comment! {
 concat!("This is a wonderful generated struct!

 You can use it as follow:

 ```
 let x = ", stringify!($tyname), " {
     field1: 0,
     field2: 0,
     field3: 0,
     field4: 0,
 };

 println!(\"Created a new instance of ", stringify!($tyname), ": {:?}\", x);
 ```"),
             #[derive(Debug)]
             pub struct $tyname {
                 pub field1: u8,
                 pub field2: u16,
                 pub field3: u32,
                 pub field4: u64,
             }
         }
     }
 }

 gen_types!(FirstOne);
 gen_types!(SecondOne);
 gen_types!(Another);
 # fn main() {}

Now each struct has doc which match itself!

No runtime deps