Skip to content

Commit

Permalink
Merge pull request #214 from bheisler/master
Browse files Browse the repository at this point in the history
Add singular and plural units to from_str impl. #212
  • Loading branch information
iliekturtles authored Oct 22, 2020
2 parents 9cacb9b + 916e2ad commit 6f5fbbc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/quantity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,11 +508,12 @@ macro_rules! quantity {
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut parts = s.splitn(2, ' ');
let value = parts.next().unwrap();
let abbr = parts.next().ok_or(NoSeparator)?;
let unit = parts.next().ok_or(NoSeparator)?;
let value = value.parse::<V>().map_err(|_| ValueParseError)?;

match abbr.trim() {
$($abbreviation => Ok(Self::new::<super::super::$unit>(value)),)+
#[allow(unreachable_patterns)]
match unit.trim() {
$($abbreviation | $singular | $plural => Ok(Self::new::<super::super::$unit>(value)),)+
_ => Err(UnknownUnit),
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/tests/quantity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ storage_types! {
let m2 = k::Mass::new::<kilogram>(V::from_f64(1.0E-3).unwrap());

Test::assert_eq(&"1 m".parse::<k::Length>().unwrap(), &l1);
Test::assert_eq(&"1 meter".parse::<k::Length>().unwrap(), &l1);
Test::assert_eq(&"1 meters".parse::<k::Length>().unwrap(), &l1);
Test::assert_eq(&"1.0 km".parse::<k::Length>().unwrap(), &l2);
Test::assert_eq(&"1000 kg".parse::<k::Mass>().unwrap(), &m1);
Test::assert_eq(&"1.0E-3 kg".parse::<k::Mass>().unwrap(), &m2);
Expand Down

0 comments on commit 6f5fbbc

Please sign in to comment.