-
Notifications
You must be signed in to change notification settings - Fork 344
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
Construct Coin from String #1684
Conversation
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.
Good stuss. Two smaller things and we need a CHANGELOG entry
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.
Nice. Just the CHANGELOG.md entry
packages/std/src/coin.rs
Outdated
assert_eq!( | ||
Coin::from_str("ucosm").unwrap_err().to_string(), | ||
"Invalid amount: cannot parse integer from empty string" | ||
); | ||
assert_eq!( | ||
Coin::from_str("-123ucosm").unwrap_err().to_string(), | ||
"Invalid amount: cannot parse integer from empty string" | ||
); |
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.
Interesting test cases here.
Can we have a dedicated check for cases where the number does not start with a digit? Otherwise the error message is more misleasing than helping. The basic idea would be
- Ensure we start reading digits (not yet implemented)
- Once we get a non-digit, mark as separation point to denom (already implemented)
This would better cover cases like those:
Coin::from_str("ucosm") // no amount
Coin::from_str("") // empty input
Coin::from_str("-1ucosm") // negative amount
Coin::from_str(" 1ucosm") // unsupported whitespace
Coin::from_str("�1ucosm") // other broken data
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.
So, basically, there would be another error variant, something like "MissingAmount" that would throw in all those cases?
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 was thinking about using InvalidAmount for the new check, but given the error payload type and the separate check time, it is probably better to have MissingAmount
or AmountNotFound
separately.
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 added a check for this now. Only the empty one still throws MissingDenom
, because that way the check becomes much more elegant (and I think both errors make sense here)
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.
👏
closes #1575