Add support for square feet

This commit is contained in:
Juhani Krekelä 2023-05-29 03:01:56 +03:00
parent 7eb31b1b4b
commit 85c5c9d66f
4 changed files with 17 additions and 0 deletions

View File

@ -70,6 +70,11 @@ fn get_conversion(unit: NonMetric) -> Conversion {
from: inch_from * inch_from,
to: MetricQuantity { amount: inch_to * inch_to, unit: Metric::SquareMetre },
},
NonMetric::SquareFoot => Conversion {
offset: 0.0,
from: inch_from * inch_from,
to: MetricQuantity { amount: 12.0 * inch_to * 12.0 * inch_to, unit: Metric::SquareMetre },
},
}
}
@ -122,6 +127,7 @@ mod test {
fn area() {
let tests = [
Test(NonMetric::SquareInch, 0.00064516),
Test(NonMetric::SquareFoot, 0.09290304),
];
run_tests(&tests, Metric::SquareMetre);
}

View File

@ -74,6 +74,7 @@ fn unit_to_name(unit: NonMetric) -> &'static str {
NonMetric::Fahrenheit => "degrees Fahrenheit",
// Area
NonMetric::SquareInch => "square inches",
NonMetric::SquareFoot => "square feet",
}
}
@ -108,5 +109,6 @@ mod test {
assert_eq!(run("32 °F"), Ok("0 °C".to_string()));
// Area
assert_eq!(run("1 in²"), Ok("6.452 cm²".to_string()));
assert_eq!(run("1 ft²"), Ok("929 cm²".to_string()));
}
}

View File

@ -106,6 +106,10 @@ fn parse_unit(input: String) -> Result<NonMetric, ParseError> {
"inches²" => Ok(NonMetric::SquareInch),
"in²" => Ok(NonMetric::SquareInch),
"foot²" => Ok(NonMetric::SquareFoot),
"feet²" => Ok(NonMetric::SquareFoot),
"ft²" => Ok(NonMetric::SquareFoot),
_ => Err(ParseError::UnknownUnit(input)),
}
}
@ -259,6 +263,10 @@ mod test {
assert_eq!(parse_unit("inches²".to_string()), Ok(NonMetric::SquareInch));
assert_eq!(parse_unit("in²".to_string()), Ok(NonMetric::SquareInch));
assert_eq!(parse_unit("foot²".to_string()), Ok(NonMetric::SquareFoot));
assert_eq!(parse_unit("feet²".to_string()), Ok(NonMetric::SquareFoot));
assert_eq!(parse_unit("ft²".to_string()), Ok(NonMetric::SquareFoot));
// Unknown unit
assert_eq!(parse_unit("hutenosa".to_string()), Err(ParseError::UnknownUnit("hutenosa".to_string())));
}

View File

@ -21,6 +21,7 @@ pub enum NonMetric {
Fahrenheit,
// Area
SquareInch,
SquareFoot,
}
#[derive(Clone, Copy, Debug, PartialEq)]