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
//! Simpler module declaration, brought to you by [@NikolaiVazquez]!
//!
//! This library enables you to declare modules in ways the current syntax
//! doesn't allow.
//!
//! # Examples
//!
//! To declare multiple public modules, simply place `pub` before a module list:
//!
//! ```rust,ignore
//! mods::mods! {
//!     pub puppy, kitty;
//! }
//! ```
//!
//! This works for all visibility modifiers:
//!
//! ```rust,ignore
//! mods::mods! {
//!     pub a, b;        // Visible anywhere, even outside the module
//!     pub(crate) c, d; // Visible anywhere within the crate
//!     pub(super) e, f; // Visible to the parent module
//!     g, h;            // Visible to the current module
//! }
//! ```
//!
//! Without the `mods!` macro, the same code is much less succinct. This is what
//! the macro expands out to:
//!
//! ```rust,ignore
//! pub mod a;
//! pub mod b;
//! pub(crate) mod c;
//! pub(crate) mod d;
//! pub(super) mod e;
//! pub(super) mod f;
//! mod g;
//! mod h;
//! ```
//!
//! # Installation
//!
//! This crate is available [on crates.io][crate] and can be used by adding the
//! following to your project's [`Cargo.toml`]:
//!
//! ```toml
//! [dependencies]
//! mods = "1.0.0"
//! ```
//!
//! ## Minimum Supported Rust Version (MSRV)
//!
//! This library requires Rust 1.9.0 as the minimum version and will work with
//! all subsequent versions.
//!
//! This is because  previous versions can't have `mod x;` declarations within
//! submodules [When testing this](https://github.com/nvzqz/mods/runs/508242550)
//! we get an error complaining that the module file is not inside the directory
//! "src".
//!
//! ## Rust 2015
//!
//! If you're not using [Rust 2018], add this to your crate root (`main.rs` or
//! `lib.rs`):
//!
//! ```rust,ignore
//! #[macro_use]
//! extern crate mods;
//! ```
//!
//! You can then use the macro directly from anywhere:
//!
//! ```rust,ignore
//! mods! {
//!     pub puppy, kitty;
//! }
//! ```
//!
//! # Wishful Thinking
//!
//! It would be wonderful if we could instead have:
//!
//! ```rust,ignore
//! pub mod puppy, kitty;
//! ```
//!
//! Or a syntax that matches `use` imports:
//!
//! ```rust,ignore
//! pub mod {puppy, kitty};
//! ```
//!
//! # Changes
//!
//! See [`CHANGELOG.md`] for an exhaustive list of what has changed from one
//! version to another.
//!
//! # License
//!
//! This project is released under either:
//!
//! - [MIT License](https://github.com/nvzqz/mods/blob/master/LICENSE-MIT)
//! - [Apache License (Version 2.0)](https://github.com/nvzqz/mods/blob/master/LICENSE-APACHE)
//!
//! # Donate
//!
//! This project is made freely available (as in free beer), but unfortunately
//! not all beer is free! So, if you would like to buy me a beer (or coffee or
//! *more*), then consider supporting my work that's benefited your project
//! and thousands of others.
//!
//! <a href="https://www.patreon.com/nvzqz">
//!     <img src="https://c5.patreon.com/external/logo/become_a_patron_button.png" alt="Become a Patron!" height="35">
//! </a>
//! <a href="https://www.paypal.me/nvzqz">
//!     <img src="https://buymecoffee.intm.org/img/button-paypal-white.png" alt="Buy me a coffee" height="35">
//! </a>
//!
//! [@NikolaiVazquez]: https://twitter.com/NikolaiVazquez
//! [`Cargo.toml`]:    https://doc.rust-lang.org/cargo/reference/manifest.html
//! [`CHANGELOG.md`]:  https://github.com/nvzqz/mods/blob/master/CHANGELOG.md
//! [crate]:           https://crates.io/crates/mods
//! [Rust 2018]:       https://blog.rust-lang.org/2018/12/06/Rust-1.31-and-rust-2018.html#rust-2018

#![no_std]
#![warn(missing_docs)]

/// Declares modules simply.
///
/// # Examples
///
/// To declare multiple public modules, simply place `pub` before a module list:
///
/// ```rust,ignore
/// mods::mods! {
///     pub puppy, kitty;
/// }
/// ```
///
/// This works for all visibility modifiers:
///
/// ```rust,ignore
/// mods::mods! {
///     pub a, b;        // Visible anywhere, even outside the module
///     pub(crate) c, d; // Visible anywhere within the crate
///     pub(super) e, f; // Visible to the parent module
///     g, h;            // Visible to the current module
/// }
/// ```
///
/// Without the `mods!` macro, the same code is much less succinct. This is what
/// the macro expands out to:
///
/// ```rust,ignore
/// pub mod a;
/// pub mod b;
/// pub(crate) mod c;
/// pub(crate) mod d;
/// pub(super) mod e;
/// pub(super) mod f;
/// mod g;
/// mod h;
/// ```
///
/// # Wishful Thinking
///
/// It would be wonderful if we could instead have:
///
/// ```rust,ignore
/// pub mod puppy, kitty;
/// ```
///
/// Or a syntax that matches `use` imports:
///
/// ```rust,ignore
/// pub mod {puppy, kitty};
/// ```
#[macro_export]
macro_rules! mods {
    () => {};

    // Trailing comma
    (pub $($module:ident,)+; $($rest:tt)*) => {
        $(pub mod $module;)+
        mods! { $($rest)* }
    };
    (pub($vis:ident) $($module:ident,)+; $($rest:tt)*) => {
        $(pub($vis) mod $module;)+
        mods! { $($rest)* }
    };
    (pub(in $vis:path) $($module:ident,)+; $($rest:tt)*) => {
        $(pub(in $vis) mod $module;)+
        mods! { $($rest)* }
    };
    ($($module:ident,)+; $($rest:tt)*) => {
        $(mod $module;)+
        mods! { $($rest)* }
    };

    // No trailing comma
    (pub $($module:ident),+; $($rest:tt)*) => {
        $(pub mod $module;)+
        mods! { $($rest)* }
    };
    (pub($vis:ident) $($module:ident),+; $($rest:tt)*) => {
        $(pub($vis) mod $module;)+
        mods! { $($rest)* }
    };
    (pub(in $vis:path) $($module:ident),+; $($rest:tt)*) => {
        $(pub(in $vis) mod $module;)+
        mods! { $($rest)* }
    };
    ($($module:ident),+; $($rest:tt)*) => {
        $(mod $module;)+
        mods! { $($rest)* }
    };
}

#[cfg(test)]
mod tests;