Skip to content
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

Merged
merged 6 commits into from
May 22, 2023
Merged

Construct Coin from String #1684

merged 6 commits into from
May 22, 2023

Conversation

chipshort
Copy link
Collaborator

closes #1575

@chipshort chipshort requested a review from webmaster128 May 4, 2023 13:42
Copy link
Member

@webmaster128 webmaster128 left a 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

packages/std/src/coin.rs Show resolved Hide resolved
packages/std/src/coin.rs Outdated Show resolved Hide resolved
Copy link
Member

@webmaster128 webmaster128 left a 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

Comment on lines 207 to 214
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"
);
Copy link
Member

@webmaster128 webmaster128 May 6, 2023

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

Copy link
Collaborator Author

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?

Copy link
Member

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.

Copy link
Collaborator Author

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)

Copy link
Member

@webmaster128 webmaster128 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏

@webmaster128 webmaster128 merged commit 45c6d98 into main May 22, 2023
@webmaster128 webmaster128 deleted the chipshort/issue1575 branch May 22, 2023 07:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Coin constructor that does the opposite of Coin::to_string
2 participants