Add support for square miles
This commit is contained in:
parent
7a7959c172
commit
055ab32a99
|
@ -80,6 +80,11 @@ fn get_conversion(unit: NonMetric) -> Conversion {
|
||||||
from: inch_from * inch_from,
|
from: inch_from * inch_from,
|
||||||
to: MetricQuantity { amount: 43_560.0 * 12.0 * inch_to * 12.0 * inch_to, unit: Metric::SquareMetre },
|
to: MetricQuantity { amount: 43_560.0 * 12.0 * inch_to * 12.0 * inch_to, unit: Metric::SquareMetre },
|
||||||
},
|
},
|
||||||
|
NonMetric::SquareMile => Conversion {
|
||||||
|
offset: 0.0,
|
||||||
|
from: inch_from * inch_from,
|
||||||
|
to: MetricQuantity { amount: 1760.0 * 3.0 * 12.0 * inch_to * 1760.0 * 3.0 * 12.0 * inch_to, unit: Metric::SquareMetre },
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -134,6 +139,7 @@ mod test {
|
||||||
Test(NonMetric::SquareInch, 0.00064516),
|
Test(NonMetric::SquareInch, 0.00064516),
|
||||||
Test(NonMetric::SquareFoot, 0.09290304),
|
Test(NonMetric::SquareFoot, 0.09290304),
|
||||||
Test(NonMetric::Acre, 4046.8564224),
|
Test(NonMetric::Acre, 4046.8564224),
|
||||||
|
Test(NonMetric::SquareMile, 2589988.110336),
|
||||||
];
|
];
|
||||||
run_tests(&tests, Metric::SquareMetre);
|
run_tests(&tests, Metric::SquareMetre);
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,6 +76,7 @@ fn unit_to_name(unit: NonMetric) -> &'static str {
|
||||||
NonMetric::SquareInch => "square inches",
|
NonMetric::SquareInch => "square inches",
|
||||||
NonMetric::SquareFoot => "square feet",
|
NonMetric::SquareFoot => "square feet",
|
||||||
NonMetric::Acre => "acres",
|
NonMetric::Acre => "acres",
|
||||||
|
NonMetric::SquareMile => "square miles",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,5 +113,6 @@ mod test {
|
||||||
assert_eq!(run("1 in²"), Ok("6.452 cm²".to_string()));
|
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 ft²"), Ok("929 cm²".to_string()));
|
||||||
assert_eq!(run("1 acre"), Ok("4 047 m²".to_string()));
|
assert_eq!(run("1 acre"), Ok("4 047 m²".to_string()));
|
||||||
|
assert_eq!(run("1 mi²"), Ok("2.59 km²".to_string()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -114,6 +114,10 @@ fn parse_unit(input: String) -> Result<NonMetric, ParseError> {
|
||||||
"acres" => Ok(NonMetric::Acre),
|
"acres" => Ok(NonMetric::Acre),
|
||||||
"ac" => Ok(NonMetric::Acre),
|
"ac" => Ok(NonMetric::Acre),
|
||||||
|
|
||||||
|
"mile²" => Ok(NonMetric::SquareMile),
|
||||||
|
"miles²" => Ok(NonMetric::SquareMile),
|
||||||
|
"mi²" => Ok(NonMetric::SquareMile),
|
||||||
|
|
||||||
_ => Err(ParseError::UnknownUnit(input)),
|
_ => Err(ParseError::UnknownUnit(input)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -275,6 +279,10 @@ mod test {
|
||||||
assert_eq!(parse_unit("acres".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));
|
assert_eq!(parse_unit("ac".to_string()), Ok(NonMetric::Acre));
|
||||||
|
|
||||||
|
assert_eq!(parse_unit("mile²".to_string()), Ok(NonMetric::SquareMile));
|
||||||
|
assert_eq!(parse_unit("miles²".to_string()), Ok(NonMetric::SquareMile));
|
||||||
|
assert_eq!(parse_unit("mi²".to_string()), Ok(NonMetric::SquareMile));
|
||||||
|
|
||||||
// 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())));
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,7 @@ pub enum NonMetric {
|
||||||
SquareInch,
|
SquareInch,
|
||||||
SquareFoot,
|
SquareFoot,
|
||||||
Acre,
|
Acre,
|
||||||
|
SquareMile,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||||
|
|
Loading…
Reference in New Issue