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
use std::fmt;


/// parse symbol from string
/// from `bt_usdt_btc`
pub struct Symbol {
    pub exchange: String,
    pub currency: String,
    pub asset: String,
}

impl Symbol {
    pub fn from_str(symbol: &str) -> Option<Symbol> {
        let parts = symbol.split("_").collect::<Vec<&str>>();
        if parts.len() != 3 {
            None
        } else {
            Some(Symbol {
                exchange: parts.get(0).unwrap().to_owned().to_owned(),
                currency: parts.get(1).unwrap().to_owned().to_owned(),
                asset: parts.get(2).unwrap().to_owned().to_owned(),
            })
        }
    }
}

#[derive(Serialize)]
/// asset type:
/// spot, futures, options ...
pub enum AssetType {
    SPOT,
}

impl Default for AssetType {
    fn default() -> Self {
        AssetType::SPOT
    }
}

impl fmt::Display for AssetType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            &AssetType::SPOT => write!(f, "spot"),
        }
    }
}


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn should_parse_symbol_from_str_ok() {
        let symbol_str = "bt_usdt_btc";

        let sym = Symbol::from_str(symbol_str).unwrap();

        assert_eq!("bt", sym.exchange);
        assert_eq!("usdt", sym.currency);
        assert_eq!("btc", sym.asset);
    }
}