File: enum_select_raw.rs

package info (click to toggle)
rust-inquire 0.9.3%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,956 kB
  • sloc: makefile: 2; sh: 1
file content (57 lines) | stat: -rw-r--r-- 1,114 bytes parent folder | download | duplicates (2)
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
use std::fmt::{Display, Formatter};

use inquire::{error::InquireResult, Select};

fn main() -> InquireResult<()> {
    let ans: Currency = Select::new("Currency:", Currency::VARIANTS.to_vec()).prompt()?;

    match ans {
        Currency::BRL | Currency::USD | Currency::CAD | Currency::EUR | Currency::GBP => {
            bank_transfer();
        }
        Currency::BTC | Currency::LTC => crypto_transfer(),
    }

    Ok(())
}

fn bank_transfer() {
    // ask for bank account
    // transfer funds
}

fn crypto_transfer() {
    // ask for wallet address
    // transfer funds
}

#[derive(Debug, Copy, Clone)]
#[allow(clippy::upper_case_acronyms)]
enum Currency {
    BRL,
    USD,
    CAD,
    EUR,
    GBP,
    BTC,
    LTC,
}

impl Currency {
    // could be generated by macro
    const VARIANTS: &'static [Currency] = &[
        Self::BRL,
        Self::USD,
        Self::CAD,
        Self::EUR,
        Self::GBP,
        Self::BTC,
        Self::LTC,
    ];
}

impl Display for Currency {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
        write!(f, "{self:?}")
    }
}