Compare commits
4 Commits
782e4a18a7
...
bec9e494b5
Author | SHA1 | Date |
---|---|---|
Juhani Krekelä | bec9e494b5 | |
Juhani Krekelä | f9975903aa | |
Juhani Krekelä | 8d50c9643a | |
Juhani Krekelä | 9ef0c9655d |
|
@ -20,6 +20,9 @@ fn get_conversion(unit: NonMetric) -> Conversion {
|
|||
let pound_from = 100_000.0;
|
||||
let pound_to = 45359237.0;
|
||||
|
||||
let imperial_gallon_from = 100_000.0;
|
||||
let imperial_gallon_to = 454609.0;
|
||||
|
||||
match unit {
|
||||
// Length
|
||||
NonMetric::Inch => Conversion {
|
||||
|
@ -96,6 +99,27 @@ fn get_conversion(unit: NonMetric) -> Conversion {
|
|||
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 },
|
||||
},
|
||||
// Fluid volume
|
||||
NonMetric::ImperialGallon => Conversion {
|
||||
offset: 0.0,
|
||||
from: imperial_gallon_from,
|
||||
to: MetricQuantity { amount: imperial_gallon_to, unit: Metric::Litre },
|
||||
},
|
||||
NonMetric::ImperialQuart => Conversion {
|
||||
offset: 0.0,
|
||||
from: 4.0 * imperial_gallon_from,
|
||||
to: MetricQuantity { amount: imperial_gallon_to, unit: Metric::Litre },
|
||||
},
|
||||
NonMetric::ImperialPint => Conversion {
|
||||
offset: 0.0,
|
||||
from: 2.0 * 4.0 * imperial_gallon_from,
|
||||
to: MetricQuantity { amount: imperial_gallon_to, unit: Metric::Litre },
|
||||
},
|
||||
NonMetric::ImperialFluidOunce => Conversion {
|
||||
offset: 0.0,
|
||||
from: 20.0 * 2.0 * 4.0 * imperial_gallon_from,
|
||||
to: MetricQuantity { amount: imperial_gallon_to, unit: Metric::Litre },
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -164,6 +188,17 @@ mod test {
|
|||
run_tests(&tests, Metric::CubicMetre);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fluid_volume() {
|
||||
let tests = [
|
||||
Test(NonMetric::ImperialGallon, 4.54609),
|
||||
Test(NonMetric::ImperialQuart, 1.1365225),
|
||||
Test(NonMetric::ImperialPint, 0.56826125),
|
||||
Test(NonMetric::ImperialFluidOunce, 0.0284130625),
|
||||
];
|
||||
run_tests(&tests, Metric::Litre);
|
||||
}
|
||||
|
||||
fn run_tests(tests: &[Test], unit: Metric) {
|
||||
for test in tests {
|
||||
let from = NonMetricQuantity {
|
||||
|
|
|
@ -106,6 +106,17 @@ fn prefixed_unit(quantity: MetricQuantity) -> PrefixedUnit {
|
|||
return PrefixedUnit(0.001 * 0.001 * 0.001, "mm³");
|
||||
}
|
||||
}
|
||||
Metric::Litre => {
|
||||
if absolute >= 1.0 {
|
||||
return PrefixedUnit(1.0, "l");
|
||||
} else if absolute >= 0.1 {
|
||||
return PrefixedUnit(0.1, "dl");
|
||||
} else if absolute >= 0.01 {
|
||||
return PrefixedUnit(0.01, "cl");
|
||||
} else {
|
||||
return PrefixedUnit(0.001, "ml");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -392,4 +403,32 @@ mod test {
|
|||
unit: Metric::CubicMetre,
|
||||
}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn litre() {
|
||||
assert_eq!(PrefixedUnit(0.001, "ml"), prefixed_unit(MetricQuantity {
|
||||
amount: 0.000_1,
|
||||
unit: Metric::Litre,
|
||||
}));
|
||||
assert_eq!(PrefixedUnit(0.001, "ml"), prefixed_unit(MetricQuantity {
|
||||
amount: 0.001,
|
||||
unit: Metric::Litre,
|
||||
}));
|
||||
assert_eq!(PrefixedUnit(0.01, "cl"), prefixed_unit(MetricQuantity {
|
||||
amount: 0.01,
|
||||
unit: Metric::Litre,
|
||||
}));
|
||||
assert_eq!(PrefixedUnit(0.1, "dl"), prefixed_unit(MetricQuantity {
|
||||
amount: 0.1,
|
||||
unit: Metric::Litre,
|
||||
}));
|
||||
assert_eq!(PrefixedUnit(1.0, "l"), prefixed_unit(MetricQuantity {
|
||||
amount: 1.0,
|
||||
unit: Metric::Litre,
|
||||
}));
|
||||
assert_eq!(PrefixedUnit(1.0, "l"), prefixed_unit(MetricQuantity {
|
||||
amount: 10.0,
|
||||
unit: Metric::Litre,
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
|
10
src/lib.rs
10
src/lib.rs
|
@ -84,6 +84,11 @@ fn unit_to_name(unit: NonMetric) -> &'static str {
|
|||
// Volume
|
||||
NonMetric::CubicInch => "cubic inches",
|
||||
NonMetric::CubicFoot => "cubic feet",
|
||||
// Fluid volume
|
||||
NonMetric::ImperialGallon => "imperial gallons",
|
||||
NonMetric::ImperialQuart => "imperial quarts",
|
||||
NonMetric::ImperialPint => "imperial pints",
|
||||
NonMetric::ImperialFluidOunce => "imperial fluid ounces",
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -125,5 +130,10 @@ mod test {
|
|||
// Volume
|
||||
assert_eq!(run("1 in³"), Ok("16.39 cm³".to_string()));
|
||||
assert_eq!(run("1 ft³"), Ok("28 317 cm³".to_string()));
|
||||
// Fluid volume
|
||||
assert_eq!(run("1 imp gal"), Ok("4.546 l".to_string()));
|
||||
assert_eq!(run("1 imp qt"), Ok("1.137 l".to_string()));
|
||||
assert_eq!(run("1 imp pt"), Ok("5.683 dl".to_string()));
|
||||
assert_eq!(run("1 imp fl oz"), Ok("2.841 cl".to_string()));
|
||||
}
|
||||
}
|
||||
|
|
38
src/parse.rs
38
src/parse.rs
|
@ -185,6 +185,25 @@ fn parse_unit(input: String) -> Result<NonMetric, ParseError> {
|
|||
"feet^3" => Ok(NonMetric::CubicFoot),
|
||||
"ft^3" => Ok(NonMetric::CubicFoot),
|
||||
|
||||
// Fluid volume
|
||||
"imperial gallon" => Ok(NonMetric::ImperialGallon),
|
||||
"imperial gallons" => Ok(NonMetric::ImperialGallon),
|
||||
"imp gal" => Ok(NonMetric::ImperialGallon),
|
||||
|
||||
"imperial quart" => Ok(NonMetric::ImperialQuart),
|
||||
"imperial quarts" => Ok(NonMetric::ImperialQuart),
|
||||
"imp qt" => Ok(NonMetric::ImperialQuart),
|
||||
|
||||
"imperial pint" => Ok(NonMetric::ImperialPint),
|
||||
"imperial pints" => Ok(NonMetric::ImperialPint),
|
||||
"imp pt" => Ok(NonMetric::ImperialPint),
|
||||
|
||||
"imperial fluid ounce" => Ok(NonMetric::ImperialFluidOunce),
|
||||
"imperial fluid ounces" => Ok(NonMetric::ImperialFluidOunce),
|
||||
"imp fl oz" => Ok(NonMetric::ImperialFluidOunce),
|
||||
"imp fl. oz." => Ok(NonMetric::ImperialFluidOunce),
|
||||
"imp oz. fl." => Ok(NonMetric::ImperialFluidOunce),
|
||||
|
||||
_ => Err(ParseError::UnknownUnit(input)),
|
||||
}
|
||||
}
|
||||
|
@ -419,6 +438,25 @@ mod test {
|
|||
assert_eq!(parse_unit("feet^3".to_string()), Ok(NonMetric::CubicFoot));
|
||||
assert_eq!(parse_unit("ft^3".to_string()), Ok(NonMetric::CubicFoot));
|
||||
|
||||
// Fluid volume
|
||||
assert_eq!(parse_unit("imperial gallon".to_string()), Ok(NonMetric::ImperialGallon));
|
||||
assert_eq!(parse_unit("imperial gallons".to_string()), Ok(NonMetric::ImperialGallon));
|
||||
assert_eq!(parse_unit("imp gal".to_string()), Ok(NonMetric::ImperialGallon));
|
||||
|
||||
assert_eq!(parse_unit("imperial quart".to_string()), Ok(NonMetric::ImperialQuart));
|
||||
assert_eq!(parse_unit("imperial quarts".to_string()), Ok(NonMetric::ImperialQuart));
|
||||
assert_eq!(parse_unit("imp qt".to_string()), Ok(NonMetric::ImperialQuart));
|
||||
|
||||
assert_eq!(parse_unit("imperial pint".to_string()), Ok(NonMetric::ImperialPint));
|
||||
assert_eq!(parse_unit("imperial pints".to_string()), Ok(NonMetric::ImperialPint));
|
||||
assert_eq!(parse_unit("imp pt".to_string()), Ok(NonMetric::ImperialPint));
|
||||
|
||||
assert_eq!(parse_unit("imperial fluid ounce".to_string()), Ok(NonMetric::ImperialFluidOunce));
|
||||
assert_eq!(parse_unit("imperial fluid ounces".to_string()), Ok(NonMetric::ImperialFluidOunce));
|
||||
assert_eq!(parse_unit("imp fl oz".to_string()), Ok(NonMetric::ImperialFluidOunce));
|
||||
assert_eq!(parse_unit("imp fl. oz.".to_string()), Ok(NonMetric::ImperialFluidOunce));
|
||||
assert_eq!(parse_unit("imp oz. fl.".to_string()), Ok(NonMetric::ImperialFluidOunce));
|
||||
|
||||
// Unknown unit
|
||||
assert_eq!(parse_unit("hutenosa".to_string()), Err(ParseError::UnknownUnit("hutenosa".to_string())));
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ pub enum Metric {
|
|||
Celsius,
|
||||
SquareMetre,
|
||||
CubicMetre,
|
||||
Litre,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
|
@ -28,6 +29,11 @@ pub enum NonMetric {
|
|||
// Volume
|
||||
CubicInch,
|
||||
CubicFoot,
|
||||
// Fluid volume
|
||||
ImperialGallon,
|
||||
ImperialQuart,
|
||||
ImperialPint,
|
||||
ImperialFluidOunce,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
|
|
Loading…
Reference in New Issue