Add support for square inches
This commit is contained in:
parent
d6441454fc
commit
7eb31b1b4b
5 changed files with 110 additions and 0 deletions
|
@ -64,6 +64,12 @@ fn get_conversion(unit: NonMetric) -> Conversion {
|
||||||
from: 9.0,
|
from: 9.0,
|
||||||
to: MetricQuantity { amount: 5.0, unit: Metric::Celcius },
|
to: MetricQuantity { amount: 5.0, unit: Metric::Celcius },
|
||||||
},
|
},
|
||||||
|
// Area
|
||||||
|
NonMetric::SquareInch => Conversion {
|
||||||
|
offset: 0.0,
|
||||||
|
from: inch_from * inch_from,
|
||||||
|
to: MetricQuantity { amount: inch_to * inch_to, unit: Metric::SquareMetre },
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,6 +118,14 @@ mod test {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn area() {
|
||||||
|
let tests = [
|
||||||
|
Test(NonMetric::SquareInch, 0.00064516),
|
||||||
|
];
|
||||||
|
run_tests(&tests, Metric::SquareMetre);
|
||||||
|
}
|
||||||
|
|
||||||
fn run_tests(tests: &[Test], unit: Metric) {
|
fn run_tests(tests: &[Test], unit: Metric) {
|
||||||
for test in tests {
|
for test in tests {
|
||||||
let from = NonMetricQuantity {
|
let from = NonMetricQuantity {
|
||||||
|
|
|
@ -84,6 +84,17 @@ fn prefixed_unit(quantity: MetricQuantity) -> PrefixedUnit {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Metric::Celcius => PrefixedUnit(1.0, "°C"),
|
Metric::Celcius => PrefixedUnit(1.0, "°C"),
|
||||||
|
Metric::SquareMetre => {
|
||||||
|
if absolute >= 1000.0 * 1000.0 {
|
||||||
|
return PrefixedUnit(1000.0 * 1000.0, "km²");
|
||||||
|
} else if absolute >= 1.0 {
|
||||||
|
return PrefixedUnit(1.0, "m²");
|
||||||
|
} else if absolute >= 0.01 * 0.01 {
|
||||||
|
return PrefixedUnit(0.01 * 0.01, "cm²");
|
||||||
|
} else {
|
||||||
|
return PrefixedUnit(0.001 * 0.001, "mm²");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -262,4 +273,72 @@ mod test {
|
||||||
unit: Metric::Celcius,
|
unit: Metric::Celcius,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn square_metres() {
|
||||||
|
assert_eq!(PrefixedUnit(0.000_001, "mm²"), prefixed_unit(MetricQuantity {
|
||||||
|
amount: 0.000_000_1,
|
||||||
|
unit: Metric::SquareMetre,
|
||||||
|
}));
|
||||||
|
assert_eq!(PrefixedUnit(0.000_001, "mm²"), prefixed_unit(MetricQuantity {
|
||||||
|
amount: 0.000_001,
|
||||||
|
unit: Metric::SquareMetre,
|
||||||
|
}));
|
||||||
|
assert_eq!(PrefixedUnit(0.000_001, "mm²"), prefixed_unit(MetricQuantity {
|
||||||
|
amount: 0.000_01,
|
||||||
|
unit: Metric::SquareMetre,
|
||||||
|
}));
|
||||||
|
assert_eq!(PrefixedUnit(0.000_1, "cm²"), prefixed_unit(MetricQuantity {
|
||||||
|
amount: 0.000_1,
|
||||||
|
unit: Metric::SquareMetre,
|
||||||
|
}));
|
||||||
|
assert_eq!(PrefixedUnit(0.000_1, "cm²"), prefixed_unit(MetricQuantity {
|
||||||
|
amount: 0.001,
|
||||||
|
unit: Metric::SquareMetre,
|
||||||
|
}));
|
||||||
|
assert_eq!(PrefixedUnit(0.000_1, "cm²"), prefixed_unit(MetricQuantity {
|
||||||
|
amount: 0.01,
|
||||||
|
unit: Metric::SquareMetre,
|
||||||
|
}));
|
||||||
|
assert_eq!(PrefixedUnit(0.000_1, "cm²"), prefixed_unit(MetricQuantity {
|
||||||
|
amount: 0.01,
|
||||||
|
unit: Metric::SquareMetre,
|
||||||
|
}));
|
||||||
|
assert_eq!(PrefixedUnit(0.000_1, "cm²"), prefixed_unit(MetricQuantity {
|
||||||
|
amount: 0.1,
|
||||||
|
unit: Metric::SquareMetre,
|
||||||
|
}));
|
||||||
|
assert_eq!(PrefixedUnit(1.0, "m²"), prefixed_unit(MetricQuantity {
|
||||||
|
amount: 1.0,
|
||||||
|
unit: Metric::SquareMetre,
|
||||||
|
}));
|
||||||
|
assert_eq!(PrefixedUnit(1.0, "m²"), prefixed_unit(MetricQuantity {
|
||||||
|
amount: 10.0,
|
||||||
|
unit: Metric::SquareMetre,
|
||||||
|
}));
|
||||||
|
assert_eq!(PrefixedUnit(1.0, "m²"), prefixed_unit(MetricQuantity {
|
||||||
|
amount: 100.0,
|
||||||
|
unit: Metric::SquareMetre,
|
||||||
|
}));
|
||||||
|
assert_eq!(PrefixedUnit(1.0, "m²"), prefixed_unit(MetricQuantity {
|
||||||
|
amount: 1_000.0,
|
||||||
|
unit: Metric::SquareMetre,
|
||||||
|
}));
|
||||||
|
assert_eq!(PrefixedUnit(1.0, "m²"), prefixed_unit(MetricQuantity {
|
||||||
|
amount: 10_000.0,
|
||||||
|
unit: Metric::SquareMetre,
|
||||||
|
}));
|
||||||
|
assert_eq!(PrefixedUnit(1.0, "m²"), prefixed_unit(MetricQuantity {
|
||||||
|
amount: 100_000.0,
|
||||||
|
unit: Metric::SquareMetre,
|
||||||
|
}));
|
||||||
|
assert_eq!(PrefixedUnit(1_000_000.0, "km²"), prefixed_unit(MetricQuantity {
|
||||||
|
amount: 1_000_000.0,
|
||||||
|
unit: Metric::SquareMetre,
|
||||||
|
}));
|
||||||
|
assert_eq!(PrefixedUnit(1_000_000.0, "km²"), prefixed_unit(MetricQuantity {
|
||||||
|
amount: 10_000_000.0,
|
||||||
|
unit: Metric::SquareMetre,
|
||||||
|
}));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -72,6 +72,8 @@ fn unit_to_name(unit: NonMetric) -> &'static str {
|
||||||
NonMetric::Stone => "stones",
|
NonMetric::Stone => "stones",
|
||||||
// Temperature
|
// Temperature
|
||||||
NonMetric::Fahrenheit => "degrees Fahrenheit",
|
NonMetric::Fahrenheit => "degrees Fahrenheit",
|
||||||
|
// Area
|
||||||
|
NonMetric::SquareInch => "square inches",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,5 +106,7 @@ mod test {
|
||||||
assert_eq!(run("-40 °F"), Ok("-40 °C".to_string()));
|
assert_eq!(run("-40 °F"), Ok("-40 °C".to_string()));
|
||||||
assert_eq!(run("0 °F"), Ok("-17.78 °C".to_string()));
|
assert_eq!(run("0 °F"), Ok("-17.78 °C".to_string()));
|
||||||
assert_eq!(run("32 °F"), Ok("0 °C".to_string()));
|
assert_eq!(run("32 °F"), Ok("0 °C".to_string()));
|
||||||
|
// Area
|
||||||
|
assert_eq!(run("1 in²"), Ok("6.452 cm²".to_string()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
10
src/parse.rs
10
src/parse.rs
|
@ -101,6 +101,11 @@ fn parse_unit(input: String) -> Result<NonMetric, ParseError> {
|
||||||
"°F" => Ok(NonMetric::Fahrenheit),
|
"°F" => Ok(NonMetric::Fahrenheit),
|
||||||
"F" => Ok(NonMetric::Fahrenheit),
|
"F" => Ok(NonMetric::Fahrenheit),
|
||||||
|
|
||||||
|
// Area
|
||||||
|
"inch²" => Ok(NonMetric::SquareInch),
|
||||||
|
"inches²" => Ok(NonMetric::SquareInch),
|
||||||
|
"in²" => Ok(NonMetric::SquareInch),
|
||||||
|
|
||||||
_ => Err(ParseError::UnknownUnit(input)),
|
_ => Err(ParseError::UnknownUnit(input)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -249,6 +254,11 @@ mod test {
|
||||||
assert_eq!(parse_unit("°F".to_string()), Ok(NonMetric::Fahrenheit));
|
assert_eq!(parse_unit("°F".to_string()), Ok(NonMetric::Fahrenheit));
|
||||||
assert_eq!(parse_unit("F".to_string()), Ok(NonMetric::Fahrenheit));
|
assert_eq!(parse_unit("F".to_string()), Ok(NonMetric::Fahrenheit));
|
||||||
|
|
||||||
|
// Area
|
||||||
|
assert_eq!(parse_unit("inch²".to_string()), Ok(NonMetric::SquareInch));
|
||||||
|
assert_eq!(parse_unit("inches²".to_string()), Ok(NonMetric::SquareInch));
|
||||||
|
assert_eq!(parse_unit("in²".to_string()), Ok(NonMetric::SquareInch));
|
||||||
|
|
||||||
// Unknown unit
|
// Unknown unit
|
||||||
assert_eq!(parse_unit("hutenosa".to_string()), Err(ParseError::UnknownUnit("hutenosa".to_string())));
|
assert_eq!(parse_unit("hutenosa".to_string()), Err(ParseError::UnknownUnit("hutenosa".to_string())));
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ pub enum Metric {
|
||||||
Metre,
|
Metre,
|
||||||
Gram,
|
Gram,
|
||||||
Celcius,
|
Celcius,
|
||||||
|
SquareMetre,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||||
|
@ -18,6 +19,8 @@ pub enum NonMetric {
|
||||||
Stone,
|
Stone,
|
||||||
// Temperature
|
// Temperature
|
||||||
Fahrenheit,
|
Fahrenheit,
|
||||||
|
// Area
|
||||||
|
SquareInch,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||||
|
|
Loading…
Reference in a new issue