cxmr_exchanges/
lib.rs

1//! Crypto-bank exchanges pritimitives.
2
3#[macro_use]
4extern crate failure;
5#[macro_use]
6extern crate serde_derive;
7extern crate hashbrown;
8extern crate serde;
9
10extern crate cxmr_balances;
11extern crate cxmr_currency;
12#[macro_use]
13extern crate err_convert_macro;
14
15pub mod filters;
16mod info;
17pub mod limits;
18mod markets;
19mod orders;
20mod status;
21mod trade;
22
23pub use self::filters::*;
24pub use self::info::*;
25pub use self::limits::*;
26pub use self::markets::*;
27pub use self::orders::*;
28pub use self::status::*;
29pub use self::trade::*;
30
31use std::str::FromStr;
32use std::string::ToString;
33
34/// Crypto market exchange error type.
35#[derive(Debug, Fail)]
36pub enum Error {
37    /// Invalid exchange error.
38    #[fail(display = "{} is not a valid exchange.", _0)]
39    InvalidExchange(String),
40    /// Invalid market error.
41    #[fail(display = "{} is not a valid market.", _0)]
42    InvalidMarket(String),
43    /// Invalid order side error.
44    #[fail(display = "{} is not a valid order side.", _0)]
45    InvalidOrderSide(String),
46    /// Invalid order side id error.
47    #[fail(display = "{} is not a valid order side id.", _0)]
48    InvalidOrderSideId(i64),
49    /// Currency module error.
50    #[fail(display = "option none error: {:?}", _0)]
51    Currency(#[cause] ::cxmr_currency::Error),
52}
53
54err_converter!(Currency, ::cxmr_currency::Error);
55
56/// Exchange identifier.
57#[derive(Serialize, Deserialize, Hash, Eq, PartialEq, PartialOrd, Ord, Copy, Clone, Debug)]
58pub enum Exchange {
59    /// Unknown exchange
60    Unknown = 0,
61    /// Poloniex.com
62    Poloniex = 1,
63    /// Bitfinex.com
64    Bitfinex = 2,
65    /// Bittrex.com
66    Bittrex = 3,
67    /// Binance.com
68    Binance = 4,
69    /// Cryptopia
70    Cryptopia = 5,
71    /// Simulation
72    Simulation = 6,
73}
74
75impl Exchange {
76    /// Short exchange identifier.
77    pub fn short(self: &Exchange) -> &str {
78        match self {
79            Exchange::Unknown => "unk",
80            Exchange::Poloniex => "pnx",
81            Exchange::Bitfinex => "bfx",
82            Exchange::Bittrex => "brx",
83            Exchange::Binance => "bnc",
84            Exchange::Cryptopia => "crt",
85            Exchange::Simulation => "sim",
86        }
87    }
88}
89
90impl ToString for Exchange {
91    fn to_string(&self) -> String {
92        match self {
93            Exchange::Unknown => "Unknown".to_owned(),
94            Exchange::Poloniex => "Poloniex".to_owned(),
95            Exchange::Bitfinex => "Bitfinex".to_owned(),
96            Exchange::Bittrex => "Bittrex".to_owned(),
97            Exchange::Binance => "Binance".to_owned(),
98            Exchange::Cryptopia => "Cryptopia".to_owned(),
99            Exchange::Simulation => "Simulation".to_owned(),
100        }
101    }
102}
103
104impl FromStr for Exchange {
105    type Err = Error;
106
107    fn from_str(s: &str) -> Result<Self, Self::Err> {
108        match s {
109            "Unknown" => Ok(Exchange::Unknown),
110            "Poloniex" => Ok(Exchange::Poloniex),
111            "Bitfinex" => Ok(Exchange::Bitfinex),
112            "Bittrex" => Ok(Exchange::Bittrex),
113            "Binance" => Ok(Exchange::Binance),
114            "Cryptopia" => Ok(Exchange::Cryptopia),
115            "Simulation" => Ok(Exchange::Simulation),
116            "unk" => Ok(Exchange::Unknown),
117            "pnx" => Ok(Exchange::Poloniex),
118            "bfx" => Ok(Exchange::Bitfinex),
119            "brx" => Ok(Exchange::Bittrex),
120            "bnc" => Ok(Exchange::Binance),
121            "crt" => Ok(Exchange::Cryptopia),
122            "sim" => Ok(Exchange::Simulation),
123            _ => Err(Error::InvalidExchange(s.to_owned())),
124        }
125    }
126}
127
128#[cfg(test)]
129mod tests {
130    use super::*;
131
132    #[test]
133    fn exchange_debug() {
134        assert_eq!("pnx", Exchange::Poloniex.short());
135        assert_eq!("Poloniex".to_owned(), format!("{:?}", Exchange::Poloniex));
136    }
137}