Conversions RS - Comprehensive SI Unit Conversion Library ๐
A comprehensive command-line unit conversion tool and Rust library with full SI (International System of Units) support.
Installation
From crates.io (Recommended)
Install the binary directly from crates.io:
From GitHub Releases
Download the latest binary for your platform from the releases page.
From Source
Clone the repository and build from source:
# The binary will be in target/release/conversions_rs
As a Library
Add this to your Cargo.toml:
[]
= "1.2.0"
For JavaScript/TypeScript (WebAssembly)
Install from npm for use in web browsers or Node.js:
Or download WASM packages directly from the releases page:
conversions_rs-wasm-web.tar.gz- For web browsersconversions_rs-wasm-nodejs.tar.gz- For Node.js applicationsconversions_rs-wasm-bundler.tar.gz- For bundlers (webpack, rollup, etc.)
Features
โจ Multi-Platform Support: Command-line tool, Rust library, and WebAssembly module for web browsers
SI Base Units
- Length Conversions: meters, kilometers, centimeters, millimeters, feet, inches, yards, miles
- Mass Conversions: kilograms, grams, pounds, ounces, tons, stones
- Temperature Conversions: Celsius, Fahrenheit, Kelvin
- Time Conversions: seconds, minutes, hours, days, weeks, years, milliseconds, microseconds, nanoseconds
- Electric Current Conversions: amperes, milliamperes, microamperes, nanoamperes, kiloamperes
- Amount of Substance Conversions: moles, millimoles, micromoles, nanomoles, picomoles, kilomoles
- Luminous Intensity Conversions: candela, millicandela, kilocandela, hefnerkerze, international candle
SI Derived Units
- Volume Conversions: liters, milliliters, gallons (US/UK), fluid ounces (US/UK), cups, pints, quarts
- Area Conversions: square meters, square centimeters, square kilometers, square feet, square inches, acres, hectares
Usage
Command-Line Mode (Non-Interactive)
You can use the app directly from the command line for quick conversions:
# SI Base Units
# SI Derived Units
Get help:
# ... and all other conversion types
Interactive Mode
Run without arguments for the interactive menu:
The app provides an interactive menu where you can:
- Choose the conversion type (Length, Weight, Temperature, or Volume)
- Enter the value to convert
- Specify the source unit
- Specify the target unit
- Get the converted result
Example Sessions
Command-Line Mode:
Interactive Mode:
๐ Unit Conversion App
======================
Choose conversion type:
1. ๐ Length
2. โ๏ธ Weight/Mass
3. ๐ก๏ธ Temperature
4. ๐งช Volume
5. ๐ช Exit
Enter your choice (1-5): 1
๐ Length Conversion
Supported units: m, km, cm, mm, ft, in, yd, mi
Enter the value to convert: 100
From unit: ft
To unit: m
โ
100 ft = 30.480000 m
Using as a Library
You can use the conversion functions directly in your Rust code in multiple ways:
General Conversion Functions (String-based)
use *;
// Length conversion
let meters = convert_length.unwrap;
println!; // 30.48 meters
// Temperature conversion
let fahrenheit = convert_temperature.unwrap;
println!; // 32ยฐF
// Weight conversion
let pounds = convert_weight.unwrap;
println!; // 2.20462 lbs
// Volume conversion
let milliliters = convert_volume.unwrap;
println!; // 3785.41 ml
Modular API (Type-safe, organized by unit)
use *;
// Using the modular API - more organized and discoverable
let feet = to_feet; // 32.8084 feet
let inches = to_inches; // 60.0 inches
let cm = to_centimeters; // 30.48 cm
let km = to_kilometers; // 8.0467 km
// Chain conversions easily
let result = to_feet; // 1 km to feet
Legacy Functions (Backward compatibility)
use *;
// Traditional function names still work
let feet = meters_to_feet;
let kg = pounds_to_kilograms;
let fahrenheit = celsius_to_fahrenheit;
let ml = liters_to_milliliters;
WebAssembly (WASM) Usage ๐
The library can be compiled to WebAssembly for use in web browsers and JavaScript environments.
Installation Options
Option 1: Install from npm (Recommended)
Option 2: Download from GitHub Releases Download the appropriate WASM package from the releases page:
conversions_rs-wasm-web.tar.gz- For web browsersconversions_rs-wasm-nodejs.tar.gz- For Node.js applicationsconversions_rs-wasm-bundler.tar.gz- For bundlers (webpack, rollup, etc.)
Option 3: Build from source
First, install wasm-pack:
Use the provided build scripts to compile for different WASM targets:
Windows:
Unix/Linux/macOS:
This will generate WASM packages in the pkg/ directory for different targets:
pkg/web/- For direct browser usagepkg/nodejs/- For Node.js applicationspkg/bundler/- For webpack, rollup, etc.
JavaScript/TypeScript Usage
Using npm package:
import init from 'conversions_rs';
// Initialize the WASM module
await ;
Using local build:
import init from './pkg/web/conversions_rs.js';
// Initialize the WASM module
await ;
Example usage:
// Perform conversions
const lengthResult = ;
if else
// Temperature conversion
const tempResult = ;
console.log; // 25ยฐC = 77ยฐF
// Get supported units for a conversion type
const lengthUnits = ;
console.log;
// Output: ["m", "km", "cm", "mm", "ft", "in", "yd", "mi", ...]
WASM Result Type
All WASM conversion functions return a ConversionResult object:
interface ConversionResult {
success: boolean; // Whether the conversion succeeded
value: number; // The converted value (0 if failed)
error?: string; // Error message if conversion failed
}
HTML Demo
A complete HTML demo is provided in demo.html that showcases all WASM functionality.
Open it in a web browser (must be served over HTTP/HTTPS) to try the conversions interactively.
Integration Examples
React/Next.js:
import from 'react';
import init from './pkg/web/conversions_rs.js';
Node.js:
const = require;
const result = ;
console.log;
Browser Support
The WASM module supports all modern browsers with WebAssembly support:
- Chrome 57+
- Firefox 52+
- Safari 11+
- Edge 16+
For older browsers, consider using a WebAssembly polyfill.
Supported Units
Length
m,meter,meters- Meterskm,kilometer,kilometers- Kilometerscm,centimeter,centimeters- Centimetersmm,millimeter,millimeters- Millimetersft,foot,feet- Feetin,inch,inches- Inchesyd,yard,yards- Yardsmi,mile,miles- Miles
Weight/Mass
kg,kilogram,kilograms- Kilogramsg,gram,grams- Gramslb,lbs,pound,pounds- Poundsoz,ounce,ounces- Ouncest,ton,tons- Metric Tonsst,stone,stones- Stones
Temperature
C,celsius- CelsiusF,fahrenheit- FahrenheitK,kelvin- Kelvin
Volume
l,liter,liters,litre,litres- Litersml,milliliter,milliliters- Millilitersgal,gallon,gal_us- US Gallonsgal_uk,gallon_uk- UK Gallonsfl_oz,fl_oz_us,fluid_ounce- US Fluid Ouncesfl_oz_uk,fluid_ounce_uk- UK Fluid Ouncescup,cups,cup_us- US Cupspt,pint,pints,pt_us- US Pintsqt,quart,quarts,qt_us- US Quarts
Building
# Build the project
# Build for release (optimized)
# Run tests
# Check code without building
Installing for System-Wide Use
To use the app from anywhere on your system:
# Build release version
# The executable will be at: target/release/conversions_rs.exe (Windows) or target/release/conversions_rs (Unix)
# On Windows, you can add the target/release directory to your PATH
# Or copy conversions_rs.exe to a directory that's already in your PATH
# On Unix/Linux/Mac:
# sudo cp target/release/conversions_rs /usr/local/bin/
Library Usage
You can use Conversions RS as a library in your Rust projects:
use length;
Contributing
We welcome contributions! Please see CONTRIBUTING.md for guidelines on how to contribute to this project.
Development
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Run tests (
cargo test) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is dual-licensed under either:
- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)
- Apache License 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
at your option.
Changelog
See CHANGELOG.md for a detailed history of changes to this project.
Made with โค๏ธ by Raihau GRAFFE
Then you can use it directly:
Testing
The project includes comprehensive unit tests covering:
- All conversion functions
- Error handling for invalid units
- Edge cases and precision
- Same-unit conversions
Run tests with:
Project Structure
src/
โโโ main.rs # CLI application entry point
โโโ lib.rs # Library entry point and tests
โโโ conversions/
โโโ mod.rs # Module declarations
โโโ length.rs # Length conversion functions
โโโ weight.rs # Weight/mass conversion functions
โโโ temperature.rs # Temperature conversion functions
โโโ volume.rs # Volume conversion functions
License
This project is open source and available under the MIT License.
Dependencies
clap- Command-line argument parsing for non-interactive mode