ZFuel
A high-precision Rust library for handling ZFuel types in mutual credit accounting systems. ZFuel provides fixed-point arithmetic with configurable precision (0-6 decimal places), ensuring accurate financial calculations without floating-point rounding errors.
Features
- Variable precision support (0-6 decimal places)
- Type-safe precision handling with validation
- Mixed-precision arithmetic operations
- Full support for positive and negative values
- Precise fee calculations with configurable rates
- String parsing and formatting with full precision
- Comprehensive test coverage including fuzz testing
- No floating-point arithmetic, ensuring exact calculations
- Bounds checking on all operations
- Efficient implementation with zero-cost abstractions
Installation
Add to your Cargo.toml:
[]
= "0.4.0"
Quick Start
use ;
use Fraction;
// Create a ZFuel amount (default precision is 6)
let amount = from_str.unwrap;
// Create with specific precision
let amount_2dp = new; // 123.45
// Perform arithmetic operations
let doubled = .unwrap;
let sum = .unwrap; // Automatically uses higher precision
// Calculate fees (1%)
let fee = .unwrap;
// Format as string
println!; // "123.456789"
println!; // "246.913578"
println!; // "1.234567"
Documentation
- API Documentation
- Technical Documentation - Detailed implementation details
- CHANGELOG - Version history and changes
Development
Prerequisites
- Rust (nightly toolchain)
- cargo-fuzz
Setup
# Install nightly toolchain
# Install cargo-fuzz
Testing
The project includes comprehensive test coverage:
# Run all tests (unit tests and fuzz tests)
# Run only unit tests
# Run only fuzz tests
# Run specific fuzz tests
Fuzz Testing
The project includes several fuzz tests to ensure robustness:
fuzz_fuel: Basic ZFuel creation and validationfuzz_fraction: Fraction operations and validationfuzz_fuel_string: String parsing and validationfuzz_fuel_operations: Arithmetic operationsfuzz_fraction_operations: Fraction arithmeticfuzz_fee_calculations: Fee computationfuzz_fuel_serialization: Serialization/deserialization
Contributing
Contributions are welcome! Please see our Contributing Guidelines for details.
License
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.