Add support for cubic feet

This commit is contained in:
Juhani Krekelä 2023-05-29 22:40:03 +03:00
parent 5a1d6850e3
commit 782e4a18a7
4 changed files with 35 additions and 0 deletions

View File

@ -91,6 +91,11 @@ fn get_conversion(unit: NonMetric) -> Conversion {
from: inch_from * inch_from * inch_from,
to: MetricQuantity { amount: inch_to * inch_to * inch_to, unit: Metric::CubicMetre },
},
NonMetric::CubicFoot => Conversion {
offset: 0.0,
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 },
},
}
}
@ -154,6 +159,7 @@ mod test {
fn volume() {
let tests = [
Test(NonMetric::CubicInch, 1.6387064e-5),
Test(NonMetric::CubicFoot, 0.028316846592),
];
run_tests(&tests, Metric::CubicMetre);
}

View File

@ -83,6 +83,7 @@ fn unit_to_name(unit: NonMetric) -> &'static str {
NonMetric::SquareMile => "square miles",
// Volume
NonMetric::CubicInch => "cubic inches",
NonMetric::CubicFoot => "cubic feet",
}
}
@ -123,5 +124,6 @@ mod test {
assert_eq!(run("1 mi²"), Ok("2.59 km²".to_string()));
// Volume
assert_eq!(run("1 in³"), Ok("16.39 cm³".to_string()));
assert_eq!(run("1 ft³"), Ok("28 317 cm³".to_string()));
}
}

View File

@ -172,6 +172,19 @@ fn parse_unit(input: String) -> Result<NonMetric, ParseError> {
"inches^3" => Ok(NonMetric::CubicInch),
"in^3" => Ok(NonMetric::CubicInch),
"cubic foot" => Ok(NonMetric::CubicFoot),
"cubic feet" => Ok(NonMetric::CubicFoot),
"cubic ft" => Ok(NonMetric::CubicFoot),
"cu foot" => Ok(NonMetric::CubicFoot),
"cu feet" => Ok(NonMetric::CubicFoot),
"cu ft" => Ok(NonMetric::CubicFoot),
"foot³" => Ok(NonMetric::CubicFoot),
"feet³" => Ok(NonMetric::CubicFoot),
"ft³" => Ok(NonMetric::CubicFoot),
"foot^3" => Ok(NonMetric::CubicFoot),
"feet^3" => Ok(NonMetric::CubicFoot),
"ft^3" => Ok(NonMetric::CubicFoot),
_ => Err(ParseError::UnknownUnit(input)),
}
}
@ -393,6 +406,19 @@ mod test {
assert_eq!(parse_unit("inches^3".to_string()), Ok(NonMetric::CubicInch));
assert_eq!(parse_unit("in^3".to_string()), Ok(NonMetric::CubicInch));
assert_eq!(parse_unit("cubic foot".to_string()), Ok(NonMetric::CubicFoot));
assert_eq!(parse_unit("cubic feet".to_string()), Ok(NonMetric::CubicFoot));
assert_eq!(parse_unit("cubic ft".to_string()), Ok(NonMetric::CubicFoot));
assert_eq!(parse_unit("cu foot".to_string()), Ok(NonMetric::CubicFoot));
assert_eq!(parse_unit("cu feet".to_string()), Ok(NonMetric::CubicFoot));
assert_eq!(parse_unit("cu ft".to_string()), Ok(NonMetric::CubicFoot));
assert_eq!(parse_unit("foot³".to_string()), Ok(NonMetric::CubicFoot));
assert_eq!(parse_unit("feet³".to_string()), Ok(NonMetric::CubicFoot));
assert_eq!(parse_unit("ft³".to_string()), Ok(NonMetric::CubicFoot));
assert_eq!(parse_unit("foot^3".to_string()), Ok(NonMetric::CubicFoot));
assert_eq!(parse_unit("feet^3".to_string()), Ok(NonMetric::CubicFoot));
assert_eq!(parse_unit("ft^3".to_string()), Ok(NonMetric::CubicFoot));
// Unknown unit
assert_eq!(parse_unit("hutenosa".to_string()), Err(ParseError::UnknownUnit("hutenosa".to_string())));
}

View File

@ -27,6 +27,7 @@ pub enum NonMetric {
SquareMile,
// Volume
CubicInch,
CubicFoot,
}
#[derive(Clone, Copy, Debug, PartialEq)]