#mod #module #export #generator #auto #target-directory

barexp

A Rust library that automatically generates mod.rs files for your project

4 releases (stable)

new 1.1.1 Dec 13, 2024
1.0.0 Dec 13, 2024
0.1.0 Dec 13, 2024

#838 in Development tools

Download history 399/week @ 2024-12-09

399 downloads per month

MIT license

11KB
92 lines

barexp

Automatically generates mod.rs files for your Rust project's subdirectories. This crate simplifies module management by automatically creating and maintaining mod.rs files with proper module declarations and re-exports.

Features

  • 🔍 Recursively scans your project's subdirectories
  • 🚀 Automatically generates/updates mod.rs files
  • ♻️ Re-exports all modules with pub use
  • 🎯 Ignores hidden files and target directory
  • 🛡️ Preserves your project's root lib.rs or main.rs

Installation

Add this to your Cargo.toml:

[build-dependencies]
barexp = "1.1.0"

Usage

  1. Create a build.rs file in your project root:
fn main() {
    barexp::build();
}

That's it! The crate will automatically:

  • Scan your src directory recursively
  • Generate mod.rs files in subdirectories
  • Update existing mod.rs files when needed

Example Project Structure

Before:

src/
  ├── lib.rs
  ├── services/
  │   ├── crypto.rs
  │   └── user.rs
  └── models/
      ├── account.rs
      └── transaction.rs

After:

src/
  ├── lib.rs
  ├── services/
  │   ├── mod.rs          // Auto-generated
  │   ├── crypto.rs
  │   └── user.rs
  └── models/
      ├── mod.rs          // Auto-generated
      ├── account.rs
      └── transaction.rs

Generated mod.rs content example:

pub mod crypto;
pub mod user;

pub use self::{
    crypto::*,
    user::*,
};

How It Works

  1. During the build process, the crate:

    • Scans all subdirectories in your src folder
    • Identifies Rust source files (.rs)
    • Creates or updates mod.rs files
    • Adds proper module declarations and re-exports
  2. The crate preserves:

    • Your root src/lib.rs or src/main.rs
    • Hidden files and directories
    • The target directory

Configuration

Currently, the crate works with default settings. Future versions will include:

  • Custom ignore patterns
  • Export strategies configuration
  • Export attribute support
  • Logging configuration

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Notes

  • The crate operates during the build phase
  • Changes to source files trigger automatic updates
  • IDE integration works as expected since the generated files are regular Rust modules

Known Limitations

  • Assumes all modules should be public (pub mod)
  • Re-exports all items from modules (pub use)
  • No custom configuration options yet

Future Plans

  • Custom ignore patterns
  • Different export strategies
  • Export attribute support
  • Logging and error handling
  • Test coverage
  • Custom module visibility options

Dependencies

~0.1–6MB
~36K SLoC