|0.2.0-alpha.1||Nov 22, 2019|
|0.1.5||Nov 7, 2019|
|0.1.4||Oct 21, 2019|
#207 in Development tools
63 downloads per month
Used in 2 crates
Tired of writing and updating all the
mod statements in mod.rs?
Generate them with
dirmod scans your directory and generates the corresponding
mod statements automatically
with a simple macro call:
And that's all!
dirmodis designed for Rust 2018 Edition, so macros take simple and ambiguous names like
os, etc. It is recommended to call the macros in fully-qualified fashion like
dirmod::os!(), etc. for clarity. The old
#[macro_use] extern crate dirmod;style is not recommended.)
All modules can be set to a common visibility,
pub mod or
pub(self) mod, etc. at your favour:
You can also make all modules private, and set the visibility for the re-exported items instead:
dirmod::all!(default pub use);
It might be common to handle file modules and directory modules separately:
dirmod::all!(default file pub use; default dir pub);
This re-exports all items from file modules, and makes all directory modules public by name. (This behaviour is similar to the package system in Go)
default argument is not given,
default file priv use; default dir priv is the default
If there are individual modules among dozens that need special visibility configuration, it is also possible to write:
dirmod::all!(default pub; priv foo, bar);
Then all modules have
bar which are private.
Similarly, if all modules are publicly re-exported and
bar are only exported as modules:
dirmod::all!(default pub use; pub foo, bar);
But I use
modto implement conditional compilation!
cfg attributes for some idiomatic styles:
- A directory where each module name is the feature name (e.g.
#[cfg(feature = "foo")] mod foo;)
- A directory where each module name is the OS/OS family name (e.g.
#[cfg(target_family = "unix")] mod unix;)
This can be achieved by calling
It is likely that different OS variants of the same module expose the same API, so it might be practical to write:
If none of the modules support the current OS, you could trigger a compile error:
dirmod::os!(pub use ||);
Or with a custom error message:
dirmod::os!(pub use || "custom error message");
Note that it does not make sense to use the
because Cargo features are incremental and should not be restricted in amount.
File an issue if I missed any common styles!
No problem, you don't have to use
dirmod for every module.
dirmod::all!() has an
except argument that excludes certain modules.
Since the macro simply generates
it is perfectly fine to add more items before/after the macro call.
dirmod::all!(except corge, grault);
Instead of writing docs in mod.rs, write them in the module directly.
In addition to
dirmod constraints, there are a few advantages:
- Avoid lots of docs mixed together in a single mod.rs. Easier to navigate!
- Writing docs inside the module itself is much more relevant than references to the parent module.
To write docs for the module, use this syntax at the top of the module (before any other items):
//! Yay, I'm now describing myself! //! I finally have my own place!
Since detecting the source file requires the
Rust Nightly is required to compile this crate.
testcrate directory, which demonstrates the use of
A BNF syntax reference is available at
cargo fmt operate on the modules directly included by the entry points
by detecting direct
mod statements in the included files.
rustfmt does not expand (or even compile) macros (known issue),
modules included by
dirmod would not be formatted.
The most straightforward alternative for now is to run
shopt -s globstar enabled on a Linux shell.
The Rust compiler may fail to locate syntax error locations correctly
However, this issue has only been reproduced with the specific case
where the syntax error is related to leading
# which could be an inner attribute.