soroban_cli/tx/builder/
amount.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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()
    }
}