#cargo-subcommand #module #generate #sub #time #command #nested

app cargo-mod

A cargo sub command for generating modules in your project. Reducing time spent writing boilerplate.

6 releases

Uses old Rust 2015

0.1.5 Apr 28, 2017
0.1.4 Jun 13, 2016

#459 in Cargo plugins

25 downloads per month

Apache-2.0

10KB
192 lines

cargo-mod

A cargo sub command for generating modules.


Build Status Apache 2.0 License Crates.io Crates.io


Installation

You can install cargo-mod using cargo itself with the following one-liner

cargo install cargo-mod

Why

When writing Rust I grew very tired of creating modules by hand, it became especially tedious when I want to generate multiple nested modules and I would often forget to add an export to the mod.rs or to my own lib.rs etc etc, being a DevOps'er professionally whenever I see something that's manual, boring, and error-prone I can't help but want to automate it, so cargo-mod was born.

Contributing

I'm always happy to accept pull requests for any features you would like to see added, a few I personally would like to see added are:

- #5 Generators to remove boilerplate
  - I.e. I would like to generate Diesel.rs models, and nickel.rs / iron.rs routers/middleware etc.
- #4 More intelligent addition of modstrings to mod.rs/lib.rs/main.rs
- #3 Refactoring so unit testing is easier and code makes more sense.
- #2 Improve integration tests so they are more thorough (i.e. check all parts of the generation.)

If you're not sure if your feature is a good fit or not, just submit a Github issue asking for comments before you start working on it!

As always follow the Rust Code of Conduct, not only is it the nice thing to do it's one of the reasons I personally get so excited about Rust.

If something you add isn't covered by an existing integration test, please please please write one for your thing.

Please submit all pull requests to the develop branch.

Building from source

Just like any Rust project you can simply build with cargo build however I've included a make file which adds some niceties for performing certain commands. Specifically when running tests you should always use make test since it will clean up and regenerate the cargo project used for integration testing.

Usage

Create a new module or modules in the current cargo project.

Usage:
  cargo mod [<options>] [<path>]
  cargo mod -h | --help

Options:
  -h, --help        Print this message
  -p, --private     Make the generated module/s private

Details:
The path is a path seperated by / (even if on windows for now.) and will generate all folder modules
in between the final module and beginning module. The starting point being the current working directory.

Example:
If you are in the root of your project and you run

cargo mod this/is/a/module

We will generate 3 folder modules

this
is
a

and 1 file module

module.rs

With a final directory structure of:

my_crate/
 - Cargo.toml
 - src/
    - lib.rs
    - this/
      - mod.rs
      - is/
        - mod.rs
        - a/
          - mod.rs
          - module.rs

We will also automatically add the correct mod exports to the generated mod.rs files AND to lib.rs/main.rs
whichever exists with a preference for lib.rs if both exist

However if you are inside the src/ directory we will start generation from your current directory in the repo.

If you want to only generate one module you can denote whether it is a folder or file module by the addition or omission
of a trailing /

Example folder:
cargo mod new/

Example file:
cargo mod new

Dependencies

~445KB