| Crates.io | splitrs |
| lib.rs | splitrs |
| version | 0.2.4 |
| created_at | 2025-10-02 11:29:57.644162+00 |
| updated_at | 2025-12-27 05:20:15.80967+00 |
| description | AST-based Rust refactoring tool with trait separation, config files, and intelligent module generation |
| homepage | https://github.com/cool-japan/splitrs |
| repository | https://github.com/cool-japan/splitrs |
| max_upload_size | |
| id | 1864252 |
| size | 454,851 |
A production-ready Rust refactoring tool that intelligently splits large files into maintainable modules
SplitRS uses AST-based analysis to automatically refactor large Rust source files (>1000 lines) into well-organized, compilable modules. It handles complex generics, async functions, Arc/Mutex patterns, and automatically generates correct imports and visibility modifiers.
syn for accurate Rust parsinguse statements with proper pathspub(super), pub(crate), or pub.splitrs.toml support for project-specific settingscargo install splitrs
Or build from source:
git clone https://github.com/cool-japan/splitrs
cd splitrs
cargo build --release
# Split a large file into modules
splitrs --input src/large_file.rs --output src/large_file/
# Preview what will be created (no files written)
splitrs --input src/large_file.rs --output src/large_file/ --dry-run
# Interactive mode with confirmation
splitrs --input src/large_file.rs --output src/large_file/ --interactive
splitrs \
--input src/large_file.rs \
--output src/large_file/ \
--split-impl-blocks \
--max-impl-lines 200
Create a .splitrs.toml in your project root:
[splitrs]
max_lines = 1000
max_impl_lines = 500
split_impl_blocks = true
[naming]
type_module_suffix = "_type"
impl_module_suffix = "_impl"
[output]
preserve_comments = true
format_output = true
Then simply run:
splitrs --input src/large_file.rs --output src/large_file/
SplitRS automatically detects and separates trait implementations:
Input: user.rs
pub struct User {
pub name: String,
pub age: u32,
}
impl User {
pub fn new(name: String, age: u32) -> Self { /* ... */ }
}
impl Debug for User { /* ... */ }
impl Display for User { /* ... */ }
impl Clone for User { /* ... */ }
impl Default for User { /* ... */ }
Command:
splitrs --input user.rs --output user/ --dry-run
Output:
user/
โโโ types.rs # struct User definition + inherent impl
โโโ user_traits.rs # All trait implementations (Debug, Display, Clone, Default)
โโโ mod.rs # Module organization
Generated user_traits.rs:
//! # User - Trait Implementations
//!
//! This module contains trait implementations for `User`.
//!
//! ## Implemented Traits
//!
//! - `Debug`
//! - `Display`
//! - `Clone`
//! - `Default`
//!
//! ๐ค Generated with [SplitRS](https://github.com/cool-japan/splitrs)
use super::types::User;
impl Debug for User { /* ... */ }
impl Display for User { /* ... */ }
impl Clone for User { /* ... */ }
impl Default for User { /* ... */ }
Input: connection_pool.rs (1660 lines)
pub struct ConnectionPool<T> {
connections: Arc<Mutex<Vec<T>>>,
config: PoolConfig,
// ... 50 fields
}
impl<T: Clone + Send + Sync> ConnectionPool<T> {
pub fn new(config: PoolConfig) -> Self { ... }
pub async fn acquire(&self) -> Result<T> { ... }
pub async fn release(&self, conn: T) -> Result<()> { ... }
// ... 80 methods
}
Command:
splitrs --input connection_pool.rs --output connection_pool/ --split-impl-blocks
Output: 25 well-organized modules
connection_pool/
โโโ mod.rs # Module organization & re-exports
โโโ connectionpool_type.rs # Type definition with proper visibility
โโโ connectionpool_new_group.rs # Constructor methods
โโโ connectionpool_acquire_group.rs # Connection acquisition
โโโ connectionpool_release_group.rs # Connection release
โโโ ... (20 more focused modules)
Get detailed information before refactoring:
splitrs --input examples/trait_impl_example.rs --output /tmp/preview -n
Output:
============================================================
DRY RUN - Preview Mode
============================================================
๐ Statistics:
Original file: 82 lines
Total modules to create: 4
๐ Module Structure:
๐ product_traits.rs (2 trait impls)
๐ user_traits.rs (4 trait impls)
๐ types.rs (2 types)
๐ functions.rs (1 items)
๐พ Files that would be created:
๐ /tmp/preview/
๐ product_traits.rs
๐ user_traits.rs
๐ types.rs
๐ functions.rs
๐ mod.rs
============================================================
โ Preview complete - no files were created
============================================================
SplitRS correctly handles complex Rust patterns:
// Input
pub struct Cache<K, V>
where
K: Hash + Eq + Clone,
V: Clone + Send + Sync + 'static,
{
data: Arc<RwLock<HashMap<K, V>>>,
eviction: EvictionPolicy,
}
// Output (auto-generated)
// cache_type.rs
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
pub struct Cache<K, V>
where
K: Hash + Eq + Clone,
V: Clone + Send + Sync + 'static,
{
pub(super) data: Arc<RwLock<HashMap<K, V>>>,
pub(super) eviction: EvictionPolicy,
}
// cache_insert_group.rs
use super::cache_type::Cache;
use std::collections::HashMap;
impl<K, V> Cache<K, V>
where
K: Hash + Eq + Clone,
V: Clone + Send + Sync + 'static,
{
pub async fn insert(&mut self, key: K, value: V) -> Result<()> {
// ... implementation
}
}
| Option | Short | Description | Default |
|---|---|---|---|
--input <FILE> |
-i |
Input Rust source file (required) | - |
--output <DIR> |
-o |
Output directory for modules (required) | - |
--max-lines <N> |
-m |
Maximum lines per module | 1000 |
--split-impl-blocks |
Split large impl blocks into method groups | false | |
--max-impl-lines <N> |
Maximum lines per impl block before splitting | 500 | |
--dry-run |
-n |
Preview without creating files | false |
--interactive |
-I |
Prompt for confirmation before creating files | false |
--config <FILE> |
-c |
Path to configuration file | .splitrs.toml |
When using a .splitrs.toml file, you can configure:
[splitrs] section:
max_lines - Maximum lines per modulemax_impl_lines - Maximum lines per impl blocksplit_impl_blocks - Enable impl block splitting[naming] section:
type_module_suffix - Suffix for type modules (default: "_type")impl_module_suffix - Suffix for impl modules (default: "_impl")use_snake_case - Use snake_case for module names (default: true)[output] section:
module_doc_template - Template for module documentationpreserve_comments - Preserve original comments (default: true)format_output - Format with prettyplease (default: true)Command-line arguments always override configuration file settings.
SplitRS uses a multi-stage analysis pipeline:
synprettypleaseInline - Keep impl blocks with type definition:
typename_module.rs
โโโ struct TypeName { ... }
โโโ impl TypeName { ... }
Submodule - Split type and impl blocks (recommended for large files):
typename_type.rs # Type definition
typename_new_group.rs # Constructor methods
typename_getters.rs # Getter methods
mod.rs # Module organization
Wrapper - Wrap in parent module:
typename/
โโโ type.rs
โโโ methods.rs
โโโ mod.rs
Tested on real-world codebases:
| File Size | Lines | Time | Modules Generated |
|---|---|---|---|
| Small | 500-1000 | <100ms | 3-5 |
| Medium | 1000-1500 | <500ms | 5-12 |
| Large | 1500-2000 | <1s | 10-25 |
| Very Large | 2000+ | <2s | 25-40 |
SplitRS includes comprehensive tests:
# Run all tests
cargo test
# Test on example files
cargo run -- --input examples/large_struct.rs --output /tmp/test_output
Full API documentation is available at docs.rs/splitrs.
Generate documentation locally:
# Generate and open documentation
cargo doc --no-deps --open
# Generate documentation for all features
cargo doc --all-features --no-deps
The codebase is organized into these main modules:
main.rs - CLI interface, file analysis, and module generationconfig.rs - Configuration file parsing and management (.splitrs.toml)method_analyzer.rs - Method dependency analysis and groupingimport_analyzer.rs - Type usage tracking and import generationscope_analyzer.rs - Module scope analysis and visibility inferencedependency_analyzer.rs - Circular dependency detection and graph visualizationCore Types:
FileAnalyzer - Main analyzer for processing Rust filesTypeInfo - Information about a Rust type and its implementationsModule - Represents a generated moduleConfig - Configuration loaded from .splitrs.tomlAnalysis Types:
ImplBlockAnalyzer - Analyzes impl blocks for splittingMethodGroup - Groups related methods togetherImportAnalyzer - Tracks type usage and generates importsDependencyGraph - Detects circular dependenciesโ Perfect for:
โ ๏ธ Consider Carefully:
โ Not Recommended:
#[cfg])# .github/workflows/refactor.yml
name: Auto-refactor
on:
workflow_dispatch:
inputs:
file:
description: 'File to refactor'
required: true
jobs:
refactor:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
- run: cargo install splitrs
- run: |
splitrs --input ${{ github.event.inputs.file }} \
--output $(dirname ${{ github.event.inputs.file }})/refactored \
--split-impl-blocks
- uses: peter-evans/create-pull-request@v5
with:
title: "Refactor: Split ${{ github.event.inputs.file }}"
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
git clone https://github.com/cool-japan/splitrs
cd splitrs
cargo build
cargo test
v0.2.1 Highlights:
v0.2.0 Features:
.splitrs.toml)Current status: 90% production-ready
Next features (v0.3.0):
Future enhancements (v0.4.0+):
Licensed under either of:
at your option.
Made with โค๏ธ by the OxiRS team | Star โญ us on GitHub!