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
// // Doc comment // // Copyright (c) 2018 Guillaume Gomez // #![cfg_attr(feature = "no_core", feature(no_core))] #![cfg_attr(feature = "no_core", no_core)] #![cfg_attr(not(feature = "no_core"), no_std)] //! 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: //! //! ````text //! # 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: //! //! ```no_run //! #[macro_use] //! extern crate doc_comment; //! //! doctest!("../README.md"); //! # fn main() {} //! ``` //! //! Please also note that you can use `#[cfg(doctest)]`: //! //! ```no_run //! # #[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](https://blog.guillaume-gomez.fr/articles/2020-03-07+cfg%28doctest%29+is+stable+and+you+should+use+it). //! //! ## 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! /// This macro can be used to generate documentation upon a type/item (or just to test outer /// markdown file code examples). /// /// # Example /// /// ``` /// #[macro_use] /// extern crate doc_comment; /// /// // If you just want to test an outer markdown file: /// doc_comment!(include_str!("../README.md")); /// /// // If you want to document an item: /// doc_comment!("fooo", pub struct Foo {}); /// # fn main() {} /// ``` #[macro_export] macro_rules! doc_comment { ($x:expr) => { #[doc = $x] extern {} }; ($x:expr, $($tt:tt)*) => { #[doc = $x] $($tt)* }; } /// This macro provides a simpler way to test an outer markdown file. /// /// # Example /// /// ``` /// extern crate doc_comment; /// /// // The two next lines are doing exactly the same thing: /// doc_comment::doc_comment!(include_str!("../README.md")); /// doc_comment::doctest!("../README.md"); /// /// // If you want to have a name for your tests: /// doc_comment::doctest!("../README.md", another); /// # fn main() {} /// ``` #[cfg(not(feature = "old_macros"))] #[macro_export] macro_rules! doctest { ($x:expr) => { doc_comment::doc_comment!(include_str!($x)); }; ($x:expr, $y:ident) => { doc_comment::doc_comment!(include_str!($x), mod $y {}); }; } /// This macro provides a simpler way to test an outer markdown file. /// /// # Example /// /// ``` /// #[macro_use] /// extern crate doc_comment; /// /// // The two next lines are doing exactly the same thing: /// doc_comment!(include_str!("../README.md")); /// doctest!("../README.md"); /// /// // If you want to have a name for your tests: /// doctest!("../README.md", another); /// # fn main() {} /// ``` #[cfg(feature = "old_macros")] #[macro_export] macro_rules! doctest { ($x:expr) => { doc_comment!(include_str!($x)); }; ($x:expr, $y:ident) => { doc_comment!(include_str!($x), mod $y {}); }; }