#directory-structure #module #directory #mod #automod

macro hypermod

Automatically build the module tree from the src/ dir

2 unstable releases

0.2.0 Aug 4, 2022
0.1.0 Feb 24, 2022

#73 in #mod

MIT/Apache

7KB
81 lines

Hypermod

An even lazier version of automod and supermod. Searches the src/ directory recursively for .rs files, then builds a module tree using the directory names as module names, allowing us to never write another mod statement again so long as we live. This mimics Java package namespaces, which are also mapped to the project directory structure.

Syntax

In the project's main.rs or lib.rs: hypermod::hypermod!();

Example

Assume the following project directory structure:

  • my_rust_project/
    • src/
      • db/
        • migrations/
          • 01-migration/
            • up.sql
            • down.sql
        • schema.rs
      • model/
        • bar.rs
        • foo.rs
      • baz.rs
      • main.rs

The call to the hypermod!() macro in main.rs or lib.rs expands to the following mod statements and use statements:

mod db {
    mod schema;
    pub use self::schema::*; 
}
mod model {
    mod bar;
    pub use self::bar::*; 
    mod foo;
    pub use self::foo::*; 
}
mod baz;
pub use self::baz::*;

This allows the developer to skip writing mod statements and instead use any pub item in a Rust source file through the names of the subdirectories in which it is located. For example:

use crate::model::BarStruct;
use crate::db::schema_function;

License

This software is licensed under either of the following:

Any contribution you intentionally submit for inclusion in the work, as defined in the Apache-2.0 license, shall be dual-licensed as above, without any additional terms or conditions.

Dependencies

~1.5MB
~37K SLoC