soroban_cli/tx/builder/
amount.rs

1use std::str::FromStr;
2
3#[derive(Clone, Debug, Copy)]
4pub struct Amount(i64);
5
6#[derive(thiserror::Error, Debug)]
7pub enum Error {
8    #[error("cannot start or end with `_`: {0}")]
9    CannotStartOrEndWithUnderscore(String),
10    #[error(transparent)]
11    IntParse(#[from] std::num::ParseIntError),
12}
13
14impl FromStr for Amount {
15    type Err = Error;
16
17    fn from_str(value: &str) -> Result<Self, Self::Err> {
18        if value.starts_with('_') || value.ends_with('_') {
19            return Err(Error::CannotStartOrEndWithUnderscore(value.to_string()));
20        }
21        Ok(Self(value.replace('_', "").parse::<i64>()?))
22    }
23}
24
25impl From<Amount> for i64 {
26    fn from(builder: Amount) -> Self {
27        builder.0
28    }
29}
30
31impl From<&Amount> for i64 {
32    fn from(builder: &Amount) -> Self {
33        (*builder).into()
34    }
35}