Add support for acres

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

View File

@ -75,6 +75,11 @@ fn get_conversion(unit: NonMetric) -> Conversion {
from: inch_from * inch_from,
to: MetricQuantity { amount: 12.0 * inch_to * 12.0 * inch_to, unit: Metric::SquareMetre },
},
NonMetric::Acre => Conversion {
offset: 0.0,
from: inch_from * inch_from,
to: MetricQuantity { amount: 43_560.0 * 12.0 * inch_to * 12.0 * inch_to, unit: Metric::SquareMetre },
},
}
}
@ -128,6 +133,7 @@ mod test {
let tests = [
Test(NonMetric::SquareInch, 0.00064516),
Test(NonMetric::SquareFoot, 0.09290304),
Test(NonMetric::Acre, 4046.8564224),
];
run_tests(&tests, Metric::SquareMetre);
}

View File

@ -75,6 +75,7 @@ fn unit_to_name(unit: NonMetric) -> &'static str {
// Area
NonMetric::SquareInch => "square inches",
NonMetric::SquareFoot => "square feet",
NonMetric::Acre => "acres",
}
}
@ -110,5 +111,6 @@ mod test {
// Area
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 acre"), Ok("4 047 m²".to_string()));
}
}

View File

@ -110,6 +110,10 @@ fn parse_unit(input: String) -> Result<NonMetric, ParseError> {
"feet²" => Ok(NonMetric::SquareFoot),
"ft²" => Ok(NonMetric::SquareFoot),
"acre" => Ok(NonMetric::Acre),
"acres" => Ok(NonMetric::Acre),
"ac" => Ok(NonMetric::Acre),
_ => Err(ParseError::UnknownUnit(input)),
}
}
@ -267,6 +271,10 @@ mod test {
assert_eq!(parse_unit("feet²".to_string()), Ok(NonMetric::SquareFoot));
assert_eq!(parse_unit("ft²".to_string()), Ok(NonMetric::SquareFoot));
assert_eq!(parse_unit("acre".to_string()), Ok(NonMetric::Acre));
assert_eq!(parse_unit("acres".to_string()), Ok(NonMetric::Acre));
assert_eq!(parse_unit("ac".to_string()), Ok(NonMetric::Acre));
// Unknown unit
assert_eq!(parse_unit("hutenosa".to_string()), Err(ParseError::UnknownUnit("hutenosa".to_string())));
}

View File

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