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
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use crate::cli::StringOrStdin;
use crate::units::{Cents, MicroNano, Nano, UnboundedRai};
use clap::Clap;
use std::str::FromStr;

macro_rules! rai {
    ($dst:expr) => {
        UnboundedRai::from_str(&$dst.resolve()?)?
    };
}

macro_rules! nano {
    ($dst:expr) => {
        Nano::from_str(&$dst.resolve()?)?
    };
}

macro_rules! cents {
    ($dst:expr) => {
        Cents::from_str(&$dst.resolve()?)?
    };
}

macro_rules! micro {
    ($dst:expr) => {
        MicroNano::from_str(&$dst.resolve()?)?
    };
}

#[derive(Clap)]
pub(crate) struct UnitOpts {
    #[clap(subcommand)]
    command: SrcUnit,
}

impl UnitOpts {
    pub fn handle(&self) -> anyhow::Result<()> {
        // This is a bit unwieldy because of the "from to amount" structure.
        // Not sure how to improve.
        let src = match &self.command {
            SrcUnit::Rai(src) => match &src.dst {
                DstCommand::Rai(dst) => rai!(dst).to_unbounded_rai().to_string(),
                DstCommand::Nano(dst) => rai!(dst).to_nano().to_string(),
                DstCommand::Cents(dst) => rai!(dst).to_cents().to_string(),
                DstCommand::Micro(dst) => rai!(dst).to_micro_nano().to_string(),
            }
            .to_string(),
            SrcUnit::Nano(src) => match &src.dst {
                DstCommand::Rai(dst) => nano!(dst).to_unbounded_rai().to_string(),
                DstCommand::Nano(dst) => nano!(dst).to_nano().to_string(),
                DstCommand::Cents(dst) => nano!(dst).to_cents().to_string(),
                DstCommand::Micro(dst) => nano!(dst).to_micro_nano().to_string(),
            }
            .to_string(),
            SrcUnit::Cents(src) => match &src.dst {
                DstCommand::Rai(dst) => cents!(dst).to_unbounded_rai().to_string(),
                DstCommand::Nano(dst) => cents!(dst).to_nano().to_string(),
                DstCommand::Cents(dst) => cents!(dst).to_cents().to_string(),
                DstCommand::Micro(dst) => cents!(dst).to_micro_nano().to_string(),
            }
            .to_string(),
            SrcUnit::Micro(src) => match &src.dst {
                DstCommand::Rai(dst) => micro!(dst).to_unbounded_rai().to_string(),
                DstCommand::Nano(dst) => micro!(dst).to_nano().to_string(),
                DstCommand::Cents(dst) => micro!(dst).to_cents().to_string(),
                DstCommand::Micro(dst) => micro!(dst).to_micro_nano().to_string(),
            },
        };
        println!("{}", src);
        Ok(())
    }
}

#[derive(Clap)]
enum SrcUnit {
    /// From a rai amount.
    Rai(Dst),

    /// From a Nano amount
    Nano(Dst),

    /// From a Cents amount
    Cents(Dst),

    /// From a MicroNano amount
    Micro(Dst),
}

#[derive(Clap)]
struct Dst {
    #[clap(subcommand)]
    dst: DstCommand,
}

#[derive(Clap)]
enum DstCommand {
    /// Convert to a rai amount.
    Rai(Opts),

    /// Convert to a Nano amount.
    Nano(Opts),

    /// Convert to a Cent amount
    Cents(Opts),

    /// Convert to a MicroNano amount
    Micro(Opts),
}

#[derive(Clap)]
struct Opts {
    amount: StringOrStdin<String>,
}

impl Opts {
    fn resolve(&self) -> anyhow::Result<String> {
        self.amount.to_owned().resolve()
    }
}