-
Notifications
You must be signed in to change notification settings - Fork 98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add singular and plural units to from_str impl. #212 #214
Conversation
Seeing that it's an explicit test case, I gather that e.g. |
I think that disallowing "1 meters" would require a strange edge-case in the code for little to no benefit, so I would recommend allowing it. It's up to you though. Validation rules like that perhaps make more sense to define at the application level. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! I'll review in more depth tomorrow. I feel like there should be more test cases but I'm not sure what they could be yet.
src/quantity.rs
Outdated
match unit.trim() { | ||
$($abbreviation => Ok(Self::new::<super::super::$unit>(value)),)+ | ||
$($singular => Ok(Self::new::<super::super::$unit>(value)),)+ | ||
$($plural => Ok(Self::new::<super::super::$unit>(value)),)+ | ||
_ => Err(UnknownUnit), | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current code can be simplified:
match unit.trim() {
$($abbreviation | $singular | $plural => Ok(Self::new::<super::super::$unit>(value)),)+
_ => Err(UnknownUnit),
}
We could also consider only matching singular when value.is_one()
and plural when not. This would require separate matches as written currently in order to fit the match guards. If we do end up going this route then only one macro iteration $(...)+
is needed around all three lines instead of one on each line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could also consider only matching singular when value.is_one() and plural when not.
As @bheisler said above, being strict in this sense seems to provide little benefit especially when parsing user input. I would suggest erring on the side of lenience here following the parsing adage "be lenient in what you accept, be strict in what you generate".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated it to simplify the code, as suggested.
Thanks again for the PR! |
This adds the singular and plural unit strings to the match in from_str, which means that strings containing them can be parsed.
I found that there are a number of quantities in uom which have at least one unit where the singular and plural is the same. There is also
pressure
, where pounds-per-square-inch is defined in two different ways. Because of this, the match sometimes includes multiple arms with the same string, resulting in a compiler warning. I've ignored this warning just on the generated match.I've also updated the tests to check that the expected strings can be parsed.