-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When entering amounts users will want to use `_`s to better read what they are typing. Also with this approach we could add more complex parsing for example `1 XLM`.
- Loading branch information
1 parent
a11a924
commit 390c220
Showing
6 changed files
with
47 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use std::str::FromStr; | ||
|
||
#[derive(Clone, Debug, Copy)] | ||
pub struct Amount(i64); | ||
|
||
#[derive(thiserror::Error, Debug)] | ||
pub enum Error { | ||
#[error("cannot start or end with `_`: {0}")] | ||
CannotStartOrEndWithUnderscore(String), | ||
#[error(transparent)] | ||
IntParse(#[from] std::num::ParseIntError), | ||
} | ||
|
||
impl FromStr for Amount { | ||
type Err = Error; | ||
|
||
fn from_str(value: &str) -> Result<Self, Self::Err> { | ||
if value.starts_with('_') || value.ends_with('_') { | ||
return Err(Error::CannotStartOrEndWithUnderscore(value.to_string())); | ||
} | ||
Ok(Self(value.replace('_', "").parse::<i64>()?)) | ||
} | ||
} | ||
|
||
impl From<Amount> for i64 { | ||
fn from(builder: Amount) -> Self { | ||
builder.0 | ||
} | ||
} | ||
|
||
impl From<&Amount> for i64 { | ||
fn from(builder: &Amount) -> Self { | ||
(*builder).into() | ||
} | ||
} |