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
//! Add a cute dependency declaration snippet in your crate documentation.
//!
//! # Adding to `Cargo.toml`
//!
#![doc = dep_doc!()]
//!
//! # Goal
//!
//! When writing Rust libraries, it is quite common to add a code snippet
//! which shows to the end-user how to add the crate to the `Cargo.toml`. The
//! problem is that there's no way to ensure that the crate name and version
//! are correct before releasing a new version.
//!
//! This crate aims to automate the TOML snippet generation by providing
//! macro-assisted solution which expand in the correct crate name and version.
//! It does so by reading environment variables that are set by cargo itself.
//!
//! # Usage
//!
//! To add the TOML snippet, insert the following line between two documentation
//! lines:
//!
//! ```rust
//! //! Some doc...
//! #![doc = dep_doc::dep_doc!()]
//! //! Some other doc
//! ```
//!
//! If invoked in `dep_doc`, this will generates the following documentation:
//!
//! > Some doc...
//! > ```TOML
//! > [dependencies]
#![doc = concat!("> ", package_import!())]
//! > ```
//! > Some other doc
//!
//! # Customization
//!
//! Some crates may document specific features, git repository, branches,
//! commit hash, and so on. This can be addressed by passing code in the
//! [`dep_doc`] invocation:
//!
//! ```rust
//! //! Some doc...
//! #![doc = dep_doc::dep_doc!(git = "https://github.com/scrabsha/dep-doc")]
//! //! Some other doc
//! ```
//!
//! If invoked in `dep_doc`, this will generate the following documentation:
//!
//! > Some doc...
//! > ```TOML
//! > [dependencies]
#![doc = concat!("> ", package_import!(git = "https://github.com/scrabsha/dep-doc"))]
//! > ```
//! > Some other doc
//!
//! # My library is better suited as a development dependency
//!
//! That's fine! [`dev_dep_doc`] generates the appropriate documentation. It
//! replaces the `[dependencies]` section by a `[dev-dependencies]` one.
//!
//! ```rust
//! //! Some doc...
//! #![doc = dep_doc::dev_dep_doc!(features = ["proc_macro", "no_std"])]
//! //! Some other doc
//! ```
//!
//! If invoked in `dep_doc`, this will generate the following documentation:
//!
//! > Some doc...
//! > ```TOML
//! > [dev-dependencies]
#![doc = concat!("> ", package_import!(features = ["proc_macro", "no_std"]))]
//! > ```
//! > Some other doc

#[doc(hidden)]
pub use core;

/// Generates a `Cargo.toml` code snippet showing how to add the current crate
/// as a dependency.
///
/// This crate expands to the correct docstring. As such, the `#![doc = ...]`
/// macro must be used.
///
/// See the [crate-level documentation][crate] for more.
///
/// # Example
///
/// The simplest invocation is:
///
/// ```rust
/// #![doc = dep_doc::dep_doc!()]
/// ```
///
/// Specific feature, git repository, path can be passed in the macro invocation:
///
/// ```rust
/// #![doc = dep_doc::dep_doc!(git = "https://github.com/scrabsha/dep-doc")]
/// ```
#[macro_export]
macro_rules! dep_doc {
    ( $( $tt:tt )* ) => {
        concat!(
            "```TOML\n[dependencies]\n",
            $crate::package_import!(),
            "\n```",
        )
    };
}

/// Generates a `Cargo.toml` code snippet showing how to add the current crate
/// as a dev-dependency.
///
/// # Example
///
/// The simplest invocation is:
///
/// ```rust
/// #![doc = dep_doc::dev_dep_doc!()]
/// ```
///
/// Specific feature, git repository, path can be passed in the macro invocation:
///
/// ```rust
/// #![doc = dep_doc::dev_dep_doc!(git = "https://github.com/scrabsha/dep-doc")]
/// ```
#[macro_export]
macro_rules! dev_dep_doc {
    ( $( $tt:tt )* ) => {
        concat!(
            "```TOML\n[dev-dependencies]\n",
            $crate::package_import!(),
            "\n```",
        )
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! package_import {
    (@inner [$name:expr, $version:expr $(,)? ]) => {
        concat!(
            $name,
            " = \"",
            $version,
            "\"",
        )
    };

    (@inner [$name:expr, $version:expr $(,)? ], [ $( $rest:tt )* ] ) => {
        concat!(
            $name,
            " = { version = \"",
            $version,
            "\", ",
            stringify!( $( $rest)* ),
            " }",
        )
    };

    () => {
        $crate::package_import!(@inner [
            $crate::core::env!("CARGO_PKG_NAME"),
            $crate::core::env!("CARGO_PKG_VERSION"),
        ])
    };

    ( $( $tt:tt )+ ) => {
        $crate::package_import!(@inner [
                $crate::core::env!("CARGO_PKG_NAME"),
                $crate::core::env!("CARGO_PKG_VERSION"),
            ],
            [ $( $tt )* ]
        )
    };
}

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

    mod package_import {
        use super::*;

        #[test]
        fn no_additional_tokens() {
            let left = package_import!(@inner ["tokio", "1.13.0"]);
            let right = "tokio = \"1.13.0\"";

            assert_eq!(left, right)
        }

        #[test]
        fn with_git_path() {
            let left = package_import!(@inner ["tokio", "1.13.0"], [git = "https://github.com/tokio-rs/tokio"]);
            let right =
                "tokio = { version = \"1.13.0\", git = \"https://github.com/tokio-rs/tokio\" }";

            assert_eq!(left, right);
        }

        #[test]
        fn with_feature() {
            let left = package_import!(@inner ["tokio", "1.13.0"], [features = ["macros"]]);
            let right = "tokio = { version = \"1.13.0\", features = [\"macros\"] }";

            assert_eq!(left, right);
        }
    }
}