1use is_macro::Is;
2use serde::{Deserialize, Serialize};
3
4use crate::Number;
5
6#[derive(Debug, Is, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Hash)]
7pub enum Currency {
8 Dollar,
10 Cent,
12 Euro,
14 Ruble,
16 Lira,
18 Pound,
20 Yen,
22 Baht,
24 Won,
26 Kip,
28}
29
30impl Currency {
31 pub fn from_char(c: char) -> Option<Self> {
32 let cur = match c {
33 '$' => Self::Dollar,
34 '¢' => Self::Cent,
35 '€' => Self::Euro,
36 '₽' => Self::Ruble,
37 '₺' => Self::Lira,
38 '£' => Self::Pound,
39 '¥' => Self::Yen,
40 '฿' => Self::Baht,
41 '₩' => Self::Won,
42 '₭' => Self::Kip,
43 _ => return None,
44 };
45
46 Some(cur)
47 }
48
49 pub fn to_char(&self) -> char {
50 match self {
51 Self::Dollar => '$',
52 Self::Cent => '¢',
53 Self::Euro => '€',
54 Self::Ruble => '₽',
55 Self::Lira => '₺',
56 Self::Pound => '£',
57 Self::Yen => '¥',
58 Self::Baht => '฿',
59 Self::Won => '₩',
60 Self::Kip => '₭',
61 }
62 }
63
64 pub fn format_amount(&self, amount: &Number) -> String {
66 let c = self.to_char();
67
68 let amount = amount.to_string();
69
70 match self {
71 Currency::Dollar => format!("{}{amount}", c),
72 Currency::Cent => format!("{amount}{}", c),
73 Currency::Euro => format!("{}{amount}", c),
74 Currency::Ruble => format!("{amount} {}", c),
75 Currency::Lira => format!("{amount} {}", c),
76 Currency::Pound => format!("{}{amount}", c),
77 Currency::Yen => format!("{} {amount}", c),
78 Currency::Baht => format!("{amount} {}", c),
79 Currency::Won => format!("{} {amount}", c),
80 Currency::Kip => format!("{}{amount}", c),
81 }
82 }
83}