Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
62 lines (57 sloc) 2.11 KB
extern crate temperature;
use temperature::{Celsius, Fahrenheit, Kelvin, Rankine};
use std::str::FromStr;
macro_rules! temp_conv {
($fm:ident, $to:ident, $tmp:expr) => {
let res = $fm::from_str($tmp);
match res {
Ok(t) => {
println!("{}", $to::from(t)); // never an arror
},
Err(e) =>{ println!("{}", e) } //ParseFloatError || BelowAbsoluteZeroError
}
}
}
/*
macro_rules! map_sym {
($($sym:expr), +) => {
{
let mut x: Vec<&str> = Vec::new();
$(
match $sym {
"C" => x.push("Celsius"),
"F" => x.push("Fahrenheit"),
"K" => x.push("Kelvin"),
"R" => x.push("Rankine"),
_ => unreachable!()
}
)+
x
};
}
}
*/
fn main() {
let args: Vec<String> = std::env::args().collect();
let temp: &str = args[1].as_ref();
let from: &str = args[2].as_ref();
let to: &str = args[3].as_ref();
// let fmto = map_sym!(from, to);
// temp_conv![fmto[0], fmto[1], temp];
match (from, to) {
("C", "F") => { temp_conv!(Celsius, Fahrenheit, temp); },
("C", "F") => { temp_conv!(Celsius, Fahrenheit, temp); },
("C", "K") => { temp_conv!(Celsius, Kelvin, temp); },
("C", "R") => { temp_conv!(Celsius, Rankine, temp); },
("F", "C") => { temp_conv!(Fahrenheit, Celsius, temp); },
("F", "K") => { temp_conv!(Fahrenheit, Kelvin, temp); },
("F", "R") => { temp_conv!(Fahrenheit, Rankine, temp); },
("K", "C") => { temp_conv!(Kelvin, Celsius, temp); },
("K", "F") => { temp_conv!(Kelvin, Fahrenheit, temp); },
("K", "R") => { temp_conv!(Kelvin, Rankine, temp); },
("R", "C") => { temp_conv!(Rankine, Celsius, temp); },
("R", "F") => { temp_conv!(Rankine, Fahrenheit, temp); },
("R", "K") => { temp_conv!(Rankine, Kelvin, temp); },
_ => println!("Temperature unit unknown or feature not implemented. Valid units {{C|F|K|R}}\nExample: ./temperature 32 F C")
};
}