Crates.io | rustmerge |
lib.rs | rustmerge |
version | 0.2.0 |
source | src |
created_at | 2024-10-14 07:38:41.585235+00 |
updated_at | 2025-02-25 06:58:10.042436+00 |
description | A Cargo subcommand that merges all Rust source files in a package into a single file. |
homepage | |
repository | https://github.com/b3hr4d/rustmerge |
max_upload_size | |
id | 1408039 |
size | 62,537 |
rustmerge
is a Cargo subcommand that merges all Rust source files in a package or workspace into single files. It works with both workspace projects and single-package projects.
Its primary use case is to simplify the process of sharing Rust projects with AI tools, e.g., for training machine learning models or code analysis. By merging all source files into a single file per package, you can easily share the project with tools that require a single file as input.
.rs
files in a package into a single filecfg
attributes on modules +-------------+
| Project |
| Structure |
+------+------+
|
v
+------+------+ +------------+
| Parse Module +----> |
| Structure | | Extract |
+------+------+ | Module |
| | Content |
v | |
+------+------+ +------+-----+
| Process | |
| Modules +------------+
+------+------+
|
v
+------+------+
| Format and |
| Output |
+-------------+
rustmerge
parses your Rust project's module structure, extracts the content of each module while preserving its hierarchy, processes all modules into a single merged file, and formats the output with proper file path comments.
You can install rustmerge
using Cargo:
cargo install rustmerge
cargo rustmerge
cargo rustmerge <package_name>
cargo rustmerge --all
cargo rustmerge [<package_name>] --output <path>
If there's only one package in the workspace and you're not using --all
, you can omit the package name.
By default, the merged Rust file(s) will be created in the target
directory of your current working directory, named rustmerge/<package_name>.rs
.
Merge a single-package project:
cd my-rust-project
cargo rustmerge
Merge a specific package in a workspace:
cd my-rust-workspace
cargo rustmerge my-package
Merge all packages in a workspace:
cd my-rust-workspace
cargo rustmerge --all
Merge with a custom output path:
cargo rustmerge --output /path/to/output/merged_project.rs
Merge all packages with a custom output directory:
cargo rustmerge --all --output /path/to/output/dir
rustmerge
preserves the module structure of your project and adds helpful file path comments to indicate the original location of each file in the merged output.
Consider this directory structure:
project/
├── src/
│ ├── main.rs
│ ├── module1.rs
│ └── service/
│ ├── mod.rs
│ └── module2.rs
The merged output would look like:
// main.rs
pub mod module1 {
// module1.rs
pub fn hello() {
println!("Hello from module1");
}
pub mod submodule {
pub fn nested_function() {
println!("Nested function in module1");
}
}
}
use module1::submodule;
use service::module2;
pub mod service {
// service/mod.rs
pub mod module2 {
// service/module2.rs
pub fn hello() {
println!("Hello from module2");
}
}
}
fn main() {
module1::hello();
module2::hello();
submodule::nested_function();
}
Each file starts with a comment showing its path relative to the src
directory. This makes it easy to understand the original project structure even after merging. Notice how nested modules like service/mod.rs
and service/module2.rs
correctly show their full relative path.
rustmerge
is compatible with:
cfg
attributeslib.rs
and main.rs
based cratesIf some modules appear to be missing in the merged output:
mod module_name;
in your source codecfg
attributesIf the merged file has formatting issues:
rustfmt
is installed and available in your PATHrustfmt --edition=2021 output.rs
test
or tests
) are excluded from the merged outputThe tool will print information about the merged files, including their locations and sizes. For example:
Merged and formatted Rust program for package 'my-package' created in "/path/to/project/target/rustmerge/my-package.rs"
File size: 12345 bytes
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.