#![allow(dead_code)]
mod conversions;
use clap::{Parser, Subcommand};
use conversions::*;
use std::io::{self, Write};
#[derive(Parser)]
#[command(name = "conversions_rs")]
#[command(about = "A comprehensive unit conversion tool")]
#[command(version = "1.2.0")]
struct Args {
#[command(subcommand)]
command: Option<Commands>,
}
#[derive(Subcommand)]
enum Commands {
Length {
value: f64,
from: String,
to: String,
},
Weight {
value: f64,
from: String,
to: String,
},
Temperature {
value: f64,
from: String,
to: String,
},
Volume {
value: f64,
from: String,
to: String,
},
Time {
value: f64,
from: String,
to: String,
},
Current {
value: f64,
from: String,
to: String,
},
Amount {
value: f64,
from: String,
to: String,
},
Luminosity {
value: f64,
from: String,
to: String,
},
Area {
value: f64,
from: String,
to: String,
},
}
fn main() {
let args = Args::parse();
match args.command {
Some(command) => handle_cli_command(command),
None => run_interactive_mode(),
}
}
fn handle_cli_command(command: Commands) {
match command {
Commands::Length { value, from, to } => match convert_length(value, &from, &to) {
Ok(result) => println!("{} {} = {:.6} {}", value, from, result, to),
Err(error) => {
eprintln!("❌ Error: {}", error);
std::process::exit(1);
}
},
Commands::Weight { value, from, to } => match convert_weight(value, &from, &to) {
Ok(result) => println!("{} {} = {:.6} {}", value, from, result, to),
Err(error) => {
eprintln!("❌ Error: {}", error);
std::process::exit(1);
}
},
Commands::Temperature { value, from, to } => match convert_temperature(value, &from, &to) {
Ok(result) => println!(
"{}°{} = {:.2}°{}",
value,
from.to_uppercase(),
result,
to.to_uppercase()
),
Err(error) => {
eprintln!("❌ Error: {}", error);
std::process::exit(1);
}
},
Commands::Volume { value, from, to } => match convert_volume(value, &from, &to) {
Ok(result) => println!("{} {} = {:.6} {}", value, from, result, to),
Err(error) => {
eprintln!("❌ Error: {}", error);
std::process::exit(1);
}
},
Commands::Time { value, from, to } => match convert_time(value, &from, &to) {
Ok(result) => println!("{} {} = {:.6} {}", value, from, result, to),
Err(error) => {
eprintln!("❌ Error: {}", error);
std::process::exit(1);
}
},
Commands::Current { value, from, to } => match convert_current(value, &from, &to) {
Ok(result) => println!("{} {} = {:.6} {}", value, from, result, to),
Err(error) => {
eprintln!("❌ Error: {}", error);
std::process::exit(1);
}
},
Commands::Amount { value, from, to } => match convert_amount(value, &from, &to) {
Ok(result) => println!("{} {} = {:.6} {}", value, from, result, to),
Err(error) => {
eprintln!("❌ Error: {}", error);
std::process::exit(1);
}
},
Commands::Luminosity { value, from, to } => {
match convert_luminous_intensity(value, &from, &to) {
Ok(result) => println!("{} {} = {:.6} {}", value, from, result, to),
Err(error) => {
eprintln!("❌ Error: {}", error);
std::process::exit(1);
}
}
}
Commands::Area { value, from, to } => match convert_area(value, &from, &to) {
Ok(result) => println!("{} {} = {:.6} {}", value, from, result, to),
Err(error) => {
eprintln!("❌ Error: {}", error);
std::process::exit(1);
}
},
}
}
fn run_interactive_mode() {
println!("🔄 Unit Conversion App");
println!("======================");
loop {
display_menu();
print!("\nEnter your choice (1-9): ");
io::stdout().flush().unwrap();
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
match input.trim() {
"1" => handle_length_conversion(),
"2" => handle_weight_conversion(),
"3" => handle_temperature_conversion(),
"4" => handle_volume_conversion(),
"5" => handle_time_conversion(),
"6" => handle_current_conversion(),
"7" => handle_amount_conversion(),
"8" => handle_luminosity_conversion(),
"9" => handle_area_conversion(),
"0" => {
println!("Thanks for using the Unit Conversion App! 👋");
break;
}
_ => println!("❌ Invalid choice. Please select 0-9."),
}
println!("\n{}", "-".repeat(50));
}
}
fn display_menu() {
println!("\nChoose conversion type:");
println!("1. 📏 Length");
println!("2. ⚖️ Weight/Mass");
println!("3. 🌡️ Temperature");
println!("4. 🧪 Volume");
println!("5. ⏱️ Time");
println!("6. ⚡ Electric Current");
println!("7. 🧬 Amount of Substance");
println!("8. 💡 Luminous Intensity");
println!("9. 📐 Area");
println!("0. 🚪 Exit");
}
fn get_input(prompt: &str) -> String {
print!("{}", prompt);
io::stdout().flush().unwrap();
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
input.trim().to_string()
}
fn get_number(prompt: &str) -> f64 {
loop {
let input = get_input(prompt);
match input.parse::<f64>() {
Ok(num) => return num,
Err(_) => println!("❌ Please enter a valid number."),
}
}
}
fn handle_length_conversion() {
println!("\n📏 Length Conversion");
println!("Supported units: m, km, cm, mm, ft, in, yd, mi");
let value = get_number("Enter the value to convert: ");
let from_unit = get_input("From unit: ");
let to_unit = get_input("To unit: ");
match convert_length(value, &from_unit, &to_unit) {
Ok(result) => {
println!("✅ {} {} = {:.6} {}", value, from_unit, result, to_unit);
}
Err(error) => {
println!("❌ Error: {}", error);
}
}
}
fn handle_weight_conversion() {
println!("\n⚖️ Weight/Mass Conversion");
println!("Supported units: kg, g, lb, oz, t, st");
let value = get_number("Enter the value to convert: ");
let from_unit = get_input("From unit: ");
let to_unit = get_input("To unit: ");
match convert_weight(value, &from_unit, &to_unit) {
Ok(result) => {
println!("✅ {} {} = {:.6} {}", value, from_unit, result, to_unit);
}
Err(error) => {
println!("❌ Error: {}", error);
}
}
}
fn handle_temperature_conversion() {
println!("\n🌡️ Temperature Conversion");
println!("Supported units: C (Celsius), F (Fahrenheit), K (Kelvin)");
let value = get_number("Enter the temperature to convert: ");
let from_unit = get_input("From unit: ");
let to_unit = get_input("To unit: ");
match convert_temperature(value, &from_unit, &to_unit) {
Ok(result) => {
println!(
"✅ {}°{} = {:.2}°{}",
value,
from_unit.to_uppercase(),
result,
to_unit.to_uppercase()
);
}
Err(error) => {
println!("❌ Error: {}", error);
}
}
}
fn handle_volume_conversion() {
println!("\n🧪 Volume Conversion");
println!("Supported units: l, ml, gal, gal_uk, fl_oz, fl_oz_uk, cup, pt, qt");
let value = get_number("Enter the value to convert: ");
let from_unit = get_input("From unit: ");
let to_unit = get_input("To unit: ");
match convert_volume(value, &from_unit, &to_unit) {
Ok(result) => {
println!("✅ {} {} = {:.6} {}", value, from_unit, result, to_unit);
}
Err(error) => {
println!("❌ Error: {}", error);
}
}
}
fn handle_time_conversion() {
println!("\n⏱️ Time Conversion");
println!("Supported units: s, min, h, day, week, year, ms, μs, ns");
let value = get_number("Enter the value to convert: ");
let from_unit = get_input("From unit: ");
let to_unit = get_input("To unit: ");
match convert_time(value, &from_unit, &to_unit) {
Ok(result) => {
println!("✅ {} {} = {:.6} {}", value, from_unit, result, to_unit);
}
Err(error) => {
println!("❌ Error: {}", error);
}
}
}
fn handle_current_conversion() {
println!("\n⚡ Electric Current Conversion");
println!("Supported units: A, mA, μA, nA, kA");
let value = get_number("Enter the value to convert: ");
let from_unit = get_input("From unit: ");
let to_unit = get_input("To unit: ");
match convert_current(value, &from_unit, &to_unit) {
Ok(result) => {
println!("✅ {} {} = {:.6} {}", value, from_unit, result, to_unit);
}
Err(error) => {
println!("❌ Error: {}", error);
}
}
}
fn handle_amount_conversion() {
println!("\n🧬 Amount of Substance Conversion");
println!("Supported units: mol, mmol, μmol, nmol, pmol, kmol");
let value = get_number("Enter the value to convert: ");
let from_unit = get_input("From unit: ");
let to_unit = get_input("To unit: ");
match convert_amount(value, &from_unit, &to_unit) {
Ok(result) => {
println!("✅ {} {} = {:.6} {}", value, from_unit, result, to_unit);
}
Err(error) => {
println!("❌ Error: {}", error);
}
}
}
fn handle_luminosity_conversion() {
println!("\n💡 Luminous Intensity Conversion");
println!("Supported units: cd, mcd, kcd, hk, ic, dc");
let value = get_number("Enter the value to convert: ");
let from_unit = get_input("From unit: ");
let to_unit = get_input("To unit: ");
match convert_luminous_intensity(value, &from_unit, &to_unit) {
Ok(result) => {
println!("✅ {} {} = {:.6} {}", value, from_unit, result, to_unit);
}
Err(error) => {
println!("❌ Error: {}", error);
}
}
}
fn handle_area_conversion() {
println!("\n📐 Area Conversion");
println!("Supported units: m², cm², km², ft², in², ac, ha, mi²");
let value = get_number("Enter the value to convert: ");
let from_unit = get_input("From unit: ");
let to_unit = get_input("To unit: ");
match convert_area(value, &from_unit, &to_unit) {
Ok(result) => {
println!("✅ {} {} = {:.6} {}", value, from_unit, result, to_unit);
}
Err(error) => {
println!("❌ Error: {}", error);
}
}
}