1 unstable release
| 0.1.0 | Jul 27, 2025 |
|---|
#11 in #assignment
46KB
969 lines
shafu-rs - Rust Solidity Formatter
A fast Solidity code formatter written in Rust that applies the "shafu" style formatting rules.
Features
- Variable declarations: Aligns type declarations with proper spacing
- Function declarations: Converts single-line functions with >2 parameters to multiline format
- Import statements: Aligns import statements with proper spacing
- Require statements: Aligns require conditions and error messages in tabular format
- Constructor declarations: Formats constructors with aligned parameters
- Variable assignments: Aligns assignment operators and handles storage/memory modifiers
- Struct assignments: Aligns struct field assignments with colons
- Double space before braces: Adds double space before opening braces
- uint256 → uint conversion: Converts uint256 to shorter uint syntax
Installation
Prerequisites
- Rust (1.70 or later)
- forge (Foundry)
Build from source
# Clone and build
git clone <repository>
cd shafu-rs
cargo build --release
# The binary will be available at target/release/shafu
Install globally
cargo install --path .
Usage
Command Line Interface
# Format a file and print to stdout
shafu path/to/your/file.sol
# Format a file and write changes back
shafu path/to/your/file.sol --write
Examples
Before formatting:
contract Example {
uint256 public balance;
address public owner;
function transfer(address to, uint256 amount, bytes memory data) external {
require(balance >= amount, "Insufficient balance");
require(to != address(0), "Invalid address");
}
}
After formatting:
contract Example {
uint public balance;
address public owner;
function transfer(
address to,
uint amount,
bytes memory data
)
external
{
require(balance >= amount, "Insufficient balance");
require(to != address(0), "Invalid address");
}
}
Development
Project Structure
shafu-rs/
├── src/
│ ├── main.rs # CLI interface
│ ├── lib.rs # Main library entry point
│ └── formatter.rs # Core formatting logic
├── Cargo.toml
└── README.md
Testing
# Run tests
cargo test
# Test against example files
cargo run -- ../shafu-formatter/tests/0_before/00_simple_storage_vars.sol
Building
# Debug build
cargo build
# Release build
cargo build --release
# Run directly
cargo run -- <file.sol> [--write]
Differences from Python Version
The Rust version maintains functional parity with the Python version while offering:
- Performance: Significantly faster execution
- Memory safety: No runtime panics from memory errors
- Type safety: Compile-time guarantees about data handling
- Single binary: No external dependencies at runtime (except forge)
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Submit a pull request
License
[License information here]
Dependencies
~5–11MB
~216K SLoC