Compare commits
No commits in common. "254ac2018627accc1e85a82552bc1a10b364b231" and "91b236d8b3bb0bb9f8c412f689c9bc37480a4ccd" have entirely different histories.
254ac20186
...
91b236d8b3
6 changed files with 19 additions and 113 deletions
10
README.md
10
README.md
|
@ -16,7 +16,7 @@ together if the units are compatible:
|
||||||
metrify 1 acre 15 square feet
|
metrify 1 acre 15 square feet
|
||||||
|
|
||||||
You can also invoke metrify without arguments, in which case it will give you a
|
You can also invoke metrify without arguments, in which case it will give you a
|
||||||
prompt to type the expression into. Press ^D to exit.
|
prompt to type the expression into.
|
||||||
|
|
||||||
Result display
|
Result display
|
||||||
--------------
|
--------------
|
||||||
|
@ -57,7 +57,6 @@ Supported units
|
||||||
|
|
||||||
* square inch (in²)
|
* square inch (in²)
|
||||||
* square foot (ft²)
|
* square foot (ft²)
|
||||||
* square yard (yd²)
|
|
||||||
* acre (ac)
|
* acre (ac)
|
||||||
* square mile (mi²)
|
* square mile (mi²)
|
||||||
|
|
||||||
|
@ -65,7 +64,6 @@ Supported units
|
||||||
|
|
||||||
* cubic inch (in³)
|
* cubic inch (in³)
|
||||||
* cubic foot (ft³)
|
* cubic foot (ft³)
|
||||||
* cubic yard (yd³)
|
|
||||||
|
|
||||||
### Imperial fluid volume units
|
### Imperial fluid volume units
|
||||||
|
|
||||||
|
@ -92,12 +90,6 @@ metrify you do not need to explicitly tell it the unit you are converting to,
|
||||||
because due to its requirements (conversion is always from non-metric to metric)
|
because due to its requirements (conversion is always from non-metric to metric)
|
||||||
it can automatically pick the correct one.
|
it can automatically pick the correct one.
|
||||||
|
|
||||||
Why should I use units(1) instead of metrify?
|
|
||||||
---------------------------------------------
|
|
||||||
units(1) includes many more units than covered by metrify, in addition to
|
|
||||||
allowing other conversions than non-metric to metric. It can also be used for
|
|
||||||
calculations involving units, which metrify does not handle outside of addition.
|
|
||||||
|
|
||||||
Building and installation
|
Building and installation
|
||||||
-------------------------
|
-------------------------
|
||||||
You can build and test out metrify with `cargo run`. If you want to install it
|
You can build and test out metrify with `cargo run`. If you want to install it
|
||||||
|
|
|
@ -91,11 +91,6 @@ fn get_conversion(unit: NonMetric) -> Conversion {
|
||||||
from: inch_from * inch_from,
|
from: inch_from * inch_from,
|
||||||
to: MetricQuantity { amount: 12.0 * inch_to * 12.0 * inch_to, unit: Metric::SquareMetre },
|
to: MetricQuantity { amount: 12.0 * inch_to * 12.0 * inch_to, unit: Metric::SquareMetre },
|
||||||
},
|
},
|
||||||
NonMetric::SquareYard => Conversion {
|
|
||||||
offset: 0.0,
|
|
||||||
from: inch_from * inch_from,
|
|
||||||
to: MetricQuantity { amount: 3.0 * 12.0 * inch_to * 3.0 * 12.0 * inch_to, unit: Metric::SquareMetre },
|
|
||||||
},
|
|
||||||
NonMetric::Acre => Conversion {
|
NonMetric::Acre => Conversion {
|
||||||
offset: 0.0,
|
offset: 0.0,
|
||||||
from: inch_from * inch_from,
|
from: inch_from * inch_from,
|
||||||
|
@ -117,11 +112,6 @@ fn get_conversion(unit: NonMetric) -> Conversion {
|
||||||
from: inch_from * inch_from * inch_from,
|
from: inch_from * inch_from * inch_from,
|
||||||
to: MetricQuantity { amount: 12.0 * inch_to * 12.0 * inch_to * 12.0 * inch_to, unit: Metric::CubicMetre },
|
to: MetricQuantity { amount: 12.0 * inch_to * 12.0 * inch_to * 12.0 * inch_to, unit: Metric::CubicMetre },
|
||||||
},
|
},
|
||||||
NonMetric::CubicYard => Conversion {
|
|
||||||
offset: 0.0,
|
|
||||||
from: inch_from * inch_from * inch_from,
|
|
||||||
to: MetricQuantity { amount: 3.0 * 12.0 * inch_to * 3.0 * 12.0 * inch_to * 3.0 * 12.0 * inch_to, unit: Metric::CubicMetre },
|
|
||||||
},
|
|
||||||
// Fluid volume
|
// Fluid volume
|
||||||
NonMetric::ImperialFluidOunce => Conversion {
|
NonMetric::ImperialFluidOunce => Conversion {
|
||||||
offset: 0.0,
|
offset: 0.0,
|
||||||
|
@ -233,7 +223,6 @@ mod test {
|
||||||
let tests = [
|
let tests = [
|
||||||
Test(NonMetric::SquareInch, 0.00064516),
|
Test(NonMetric::SquareInch, 0.00064516),
|
||||||
Test(NonMetric::SquareFoot, 0.09290304),
|
Test(NonMetric::SquareFoot, 0.09290304),
|
||||||
Test(NonMetric::SquareYard, 0.83612736),
|
|
||||||
Test(NonMetric::Acre, 4046.8564224),
|
Test(NonMetric::Acre, 4046.8564224),
|
||||||
Test(NonMetric::SquareMile, 2589988.110336),
|
Test(NonMetric::SquareMile, 2589988.110336),
|
||||||
];
|
];
|
||||||
|
@ -245,7 +234,6 @@ mod test {
|
||||||
let tests = [
|
let tests = [
|
||||||
Test(NonMetric::CubicInch, 1.6387064e-5),
|
Test(NonMetric::CubicInch, 1.6387064e-5),
|
||||||
Test(NonMetric::CubicFoot, 0.028316846592),
|
Test(NonMetric::CubicFoot, 0.028316846592),
|
||||||
Test(NonMetric::CubicYard, 0.764554857984),
|
|
||||||
];
|
];
|
||||||
run_tests(&tests, Metric::CubicMetre);
|
run_tests(&tests, Metric::CubicMetre);
|
||||||
}
|
}
|
||||||
|
|
|
@ -84,13 +84,11 @@ fn unit_to_name(unit: NonMetric) -> &'static str {
|
||||||
// Area
|
// Area
|
||||||
NonMetric::SquareInch => "square inches",
|
NonMetric::SquareInch => "square inches",
|
||||||
NonMetric::SquareFoot => "square feet",
|
NonMetric::SquareFoot => "square feet",
|
||||||
NonMetric::SquareYard => "square yards",
|
|
||||||
NonMetric::Acre => "acres",
|
NonMetric::Acre => "acres",
|
||||||
NonMetric::SquareMile => "square miles",
|
NonMetric::SquareMile => "square miles",
|
||||||
// Volume
|
// Volume
|
||||||
NonMetric::CubicInch => "cubic inches",
|
NonMetric::CubicInch => "cubic inches",
|
||||||
NonMetric::CubicFoot => "cubic feet",
|
NonMetric::CubicFoot => "cubic feet",
|
||||||
NonMetric::CubicYard => "cubic yards",
|
|
||||||
// Fluid volume
|
// Fluid volume
|
||||||
NonMetric::ImperialFluidOunce => "imperial fluid ounces",
|
NonMetric::ImperialFluidOunce => "imperial fluid ounces",
|
||||||
NonMetric::ImperialPint => "imperial pints",
|
NonMetric::ImperialPint => "imperial pints",
|
||||||
|
@ -142,13 +140,11 @@ mod test {
|
||||||
// Area
|
// Area
|
||||||
assert_eq!(run("1 in²"), Ok("6.452 cm²".to_string()));
|
assert_eq!(run("1 in²"), Ok("6.452 cm²".to_string()));
|
||||||
assert_eq!(run("1 ft²"), Ok("929 cm²".to_string()));
|
assert_eq!(run("1 ft²"), Ok("929 cm²".to_string()));
|
||||||
assert_eq!(run("1 yd²"), Ok("8 361 cm²".to_string()));
|
|
||||||
assert_eq!(run("1 acre"), Ok("4 047 m²".to_string()));
|
assert_eq!(run("1 acre"), Ok("4 047 m²".to_string()));
|
||||||
assert_eq!(run("1 mi²"), Ok("2.59 km²".to_string()));
|
assert_eq!(run("1 mi²"), Ok("2.59 km²".to_string()));
|
||||||
// Volume
|
// Volume
|
||||||
assert_eq!(run("1 in³"), Ok("16.39 cm³".to_string()));
|
assert_eq!(run("1 in³"), Ok("16.39 cm³".to_string()));
|
||||||
assert_eq!(run("1 ft³"), Ok("28 317 cm³".to_string()));
|
assert_eq!(run("1 ft³"), Ok("28 317 cm³".to_string()));
|
||||||
assert_eq!(run("1 yd³"), Ok("764 555 cm³".to_string()));
|
|
||||||
// Fluid volume
|
// Fluid volume
|
||||||
assert_eq!(run("1 imp fl oz"), Ok("2.841 cl".to_string()));
|
assert_eq!(run("1 imp fl oz"), Ok("2.841 cl".to_string()));
|
||||||
assert_eq!(run("1 imp pt"), Ok("5.683 dl".to_string()));
|
assert_eq!(run("1 imp pt"), Ok("5.683 dl".to_string()));
|
||||||
|
|
18
src/main.rs
18
src/main.rs
|
@ -9,9 +9,9 @@ fn main() {
|
||||||
let name = args[0].clone();
|
let name = args[0].clone();
|
||||||
let args = args[1..].join(" ");
|
let args = args[1..].join(" ");
|
||||||
|
|
||||||
|
let mut input = args;
|
||||||
|
|
||||||
if args.len() == 0 {
|
if input.len() == 0 {
|
||||||
loop {
|
|
||||||
print!("> ");
|
print!("> ");
|
||||||
match io::stdout().flush() {
|
match io::stdout().flush() {
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
|
@ -20,7 +20,6 @@ fn main() {
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let mut input = String::new();
|
|
||||||
match io::stdin().read_line(&mut input) {
|
match io::stdin().read_line(&mut input) {
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
@ -28,19 +27,9 @@ fn main() {
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if input.len() == 0 {
|
|
||||||
println!(); // Add a newline if user pressed ^D
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
match run(&input) {
|
match run(&input) {
|
||||||
Ok(str) => println!("{str}"),
|
|
||||||
Err(err) => {
|
|
||||||
eprintln!("Error: {err}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
match run(&args) {
|
|
||||||
Ok(str) => println!("{str}"),
|
Ok(str) => println!("{str}"),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("{name}: Error: {err}");
|
eprintln!("{name}: Error: {err}");
|
||||||
|
@ -48,4 +37,3 @@ fn main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
56
src/parse.rs
56
src/parse.rs
|
@ -156,19 +156,6 @@ fn parse_unit(input: String) -> Result<NonMetric, ParseError> {
|
||||||
"'^2" => Ok(NonMetric::SquareFoot),
|
"'^2" => Ok(NonMetric::SquareFoot),
|
||||||
"sf" => Ok(NonMetric::SquareFoot),
|
"sf" => Ok(NonMetric::SquareFoot),
|
||||||
|
|
||||||
"square yard" => Ok(NonMetric::SquareYard),
|
|
||||||
"square yards" => Ok(NonMetric::SquareYard),
|
|
||||||
"square yd" => Ok(NonMetric::SquareYard),
|
|
||||||
"sq yard" => Ok(NonMetric::SquareYard),
|
|
||||||
"sq yards" => Ok(NonMetric::SquareYard),
|
|
||||||
"sq yd" => Ok(NonMetric::SquareYard),
|
|
||||||
"yard²" => Ok(NonMetric::SquareYard),
|
|
||||||
"yards²" => Ok(NonMetric::SquareYard),
|
|
||||||
"yd²" => Ok(NonMetric::SquareYard),
|
|
||||||
"yard^2" => Ok(NonMetric::SquareYard),
|
|
||||||
"yards^2" => Ok(NonMetric::SquareYard),
|
|
||||||
"yd^2" => Ok(NonMetric::SquareYard),
|
|
||||||
|
|
||||||
"acre" => Ok(NonMetric::Acre),
|
"acre" => Ok(NonMetric::Acre),
|
||||||
"acres" => Ok(NonMetric::Acre),
|
"acres" => Ok(NonMetric::Acre),
|
||||||
"ac" => Ok(NonMetric::Acre),
|
"ac" => Ok(NonMetric::Acre),
|
||||||
|
@ -213,19 +200,6 @@ fn parse_unit(input: String) -> Result<NonMetric, ParseError> {
|
||||||
"feet^3" => Ok(NonMetric::CubicFoot),
|
"feet^3" => Ok(NonMetric::CubicFoot),
|
||||||
"ft^3" => Ok(NonMetric::CubicFoot),
|
"ft^3" => Ok(NonMetric::CubicFoot),
|
||||||
|
|
||||||
"cubic yard" => Ok(NonMetric::CubicYard),
|
|
||||||
"cubic yards" => Ok(NonMetric::CubicYard),
|
|
||||||
"cubic yd" => Ok(NonMetric::CubicYard),
|
|
||||||
"cu yard" => Ok(NonMetric::CubicYard),
|
|
||||||
"cu yards" => Ok(NonMetric::CubicYard),
|
|
||||||
"cu yd" => Ok(NonMetric::CubicYard),
|
|
||||||
"yard³" => Ok(NonMetric::CubicYard),
|
|
||||||
"yards³" => Ok(NonMetric::CubicYard),
|
|
||||||
"yd³" => Ok(NonMetric::CubicYard),
|
|
||||||
"yard^3" => Ok(NonMetric::CubicYard),
|
|
||||||
"yards^3" => Ok(NonMetric::CubicYard),
|
|
||||||
"yd^3" => Ok(NonMetric::CubicYard),
|
|
||||||
|
|
||||||
// Fluid volume
|
// Fluid volume
|
||||||
"imperial fluid ounce" => Ok(NonMetric::ImperialFluidOunce),
|
"imperial fluid ounce" => Ok(NonMetric::ImperialFluidOunce),
|
||||||
"imperial fluid ounces" => Ok(NonMetric::ImperialFluidOunce),
|
"imperial fluid ounces" => Ok(NonMetric::ImperialFluidOunce),
|
||||||
|
@ -576,21 +550,6 @@ mod test {
|
||||||
"sf",
|
"sf",
|
||||||
]);
|
]);
|
||||||
|
|
||||||
test_units(NonMetric::SquareYard, &[
|
|
||||||
"square yard",
|
|
||||||
"square yards",
|
|
||||||
"square yd",
|
|
||||||
"sq yard",
|
|
||||||
"sq yards",
|
|
||||||
"sq yd",
|
|
||||||
"yard²",
|
|
||||||
"yards²",
|
|
||||||
"yd²",
|
|
||||||
"yard^2",
|
|
||||||
"yards^2",
|
|
||||||
"yd^2",
|
|
||||||
]);
|
|
||||||
|
|
||||||
test_units(NonMetric::Acre, &[
|
test_units(NonMetric::Acre, &[
|
||||||
"acre",
|
"acre",
|
||||||
"acres",
|
"acres",
|
||||||
|
@ -643,21 +602,6 @@ mod test {
|
||||||
"ft^3",
|
"ft^3",
|
||||||
]);
|
]);
|
||||||
|
|
||||||
test_units(NonMetric::CubicYard, &[
|
|
||||||
"cubic yard",
|
|
||||||
"cubic yards",
|
|
||||||
"cubic yd",
|
|
||||||
"cu yard",
|
|
||||||
"cu yards",
|
|
||||||
"cu yd",
|
|
||||||
"yard³",
|
|
||||||
"yards³",
|
|
||||||
"yd³",
|
|
||||||
"yard^3",
|
|
||||||
"yards^3",
|
|
||||||
"yd^3",
|
|
||||||
]);
|
|
||||||
|
|
||||||
// Fluid volume
|
// Fluid volume
|
||||||
test_units(NonMetric::ImperialFluidOunce, &[
|
test_units(NonMetric::ImperialFluidOunce, &[
|
||||||
"imperial fluid ounce",
|
"imperial fluid ounce",
|
||||||
|
|
|
@ -26,13 +26,11 @@ pub enum NonMetric {
|
||||||
// Area
|
// Area
|
||||||
SquareInch,
|
SquareInch,
|
||||||
SquareFoot,
|
SquareFoot,
|
||||||
SquareYard,
|
|
||||||
Acre,
|
Acre,
|
||||||
SquareMile,
|
SquareMile,
|
||||||
// Volume
|
// Volume
|
||||||
CubicInch,
|
CubicInch,
|
||||||
CubicFoot,
|
CubicFoot,
|
||||||
CubicYard,
|
|
||||||
// Fluid volume
|
// Fluid volume
|
||||||
ImperialFluidOunce,
|
ImperialFluidOunce,
|
||||||
ImperialPint,
|
ImperialPint,
|
||||||
|
|
Loading…
Reference in a new issue