bitpanda_csv/trade/
currency.rs

1//! # Currency
2//!
3//! This module provides the currency type on bitpanda
4
5use super::{CryptoCurrency, Fiat};
6
7/// Defines the currency on Bitanda
8#[derive(Debug, Deserialize, Copy, Clone, Eq, PartialEq, Hash)]
9#[serde(rename_all = "UPPERCASE", untagged)]
10pub enum Currency {
11    /// A fiat is a kind of currency
12    Fiat(Fiat),
13    Crypto(CryptoCurrency),
14}
15
16impl Currency {
17    /// Returns whether the currency is FIAT
18    pub fn is_fiat(&self) -> bool {
19        matches!(self, Currency::Fiat(_))
20    }
21
22    /// Returns whether the currency is a crypto
23    pub fn is_crypto(&self) -> bool {
24        matches!(self, Currency::Crypto(_))
25    }
26}
27
28#[cfg(test)]
29mod test {
30
31    use super::*;
32
33    use pretty_assertions::assert_eq;
34    use std::io::Cursor;
35
36    #[test]
37    fn should_tell_whether_is_fiat() {
38        assert_eq!(Currency::Fiat(Fiat::Eur).is_fiat(), true);
39        assert_eq!(Currency::Crypto(CryptoCurrency::Best).is_fiat(), false);
40    }
41
42    #[test]
43    fn should_tell_whether_is_crypto() {
44        assert_eq!(Currency::Fiat(Fiat::Eur).is_crypto(), false);
45        assert_eq!(Currency::Crypto(CryptoCurrency::Best).is_crypto(), true);
46    }
47
48    #[test]
49    fn should_decode_currency() {
50        let csv = r#"id,currency
510,BTC
521,1INCH
532,ETH
543,USDT
554,EUR
565,USD
57"#;
58        let buffer = Cursor::new(csv);
59        let mut reader = csv::Reader::from_reader(buffer);
60        let mut fakes: Vec<Currency> = Vec::new();
61        for result in reader.deserialize::<Fake>() {
62            fakes.push(result.expect("failed to decode").currency);
63        }
64        assert_eq!(
65            fakes,
66            vec![
67                Currency::Crypto(CryptoCurrency::Btc),
68                Currency::Crypto(CryptoCurrency::OneInch),
69                Currency::Crypto(CryptoCurrency::Eth),
70                Currency::Crypto(CryptoCurrency::Usdt),
71                Currency::Fiat(Fiat::Eur),
72                Currency::Fiat(Fiat::Usd),
73            ]
74        );
75    }
76
77    #[derive(Deserialize)]
78    #[allow(dead_code)]
79    struct Fake {
80        id: u64,
81        currency: Currency,
82    }
83}