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
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//! Crypto-bank exchanges pritimitives.

#[macro_use]
extern crate failure;
#[macro_use]
extern crate serde_derive;
extern crate hashbrown;
extern crate serde;

extern crate cxmr_balances;
extern crate cxmr_currency;
#[macro_use]
extern crate err_convert_macro;

pub mod filters;
mod info;
pub mod limits;
mod markets;
mod orders;
mod status;
mod trade;

pub use self::filters::*;
pub use self::info::*;
pub use self::limits::*;
pub use self::markets::*;
pub use self::orders::*;
pub use self::status::*;
pub use self::trade::*;

use std::str::FromStr;
use std::string::ToString;

/// Crypto market exchange error type.
#[derive(Debug, Fail)]
pub enum Error {
    /// Invalid exchange error.
    #[fail(display = "{} is not a valid exchange.", _0)]
    InvalidExchange(String),
    /// Invalid market error.
    #[fail(display = "{} is not a valid market.", _0)]
    InvalidMarket(String),
    /// Invalid order side error.
    #[fail(display = "{} is not a valid order side.", _0)]
    InvalidOrderSide(String),
    /// Invalid order side id error.
    #[fail(display = "{} is not a valid order side id.", _0)]
    InvalidOrderSideId(i64),
    /// Currency module error.
    #[fail(display = "option none error: {:?}", _0)]
    Currency(#[cause] ::cxmr_currency::Error),
}

err_converter!(Currency, ::cxmr_currency::Error);

/// Exchange identifier.
#[derive(Serialize, Deserialize, Hash, Eq, PartialEq, PartialOrd, Ord, Copy, Clone, Debug)]
pub enum Exchange {
    /// Unknown exchange
    Unknown = 0,
    /// Poloniex.com
    Poloniex = 1,
    /// Bitfinex.com
    Bitfinex = 2,
    /// Bittrex.com
    Bittrex = 3,
    /// Binance.com
    Binance = 4,
    /// Cryptopia
    Cryptopia = 5,
    /// Simulation
    Simulation = 6,
}

impl Exchange {
    /// Short exchange identifier.
    pub fn short(self: &Exchange) -> &str {
        match self {
            Exchange::Unknown => "unk",
            Exchange::Poloniex => "pnx",
            Exchange::Bitfinex => "bfx",
            Exchange::Bittrex => "brx",
            Exchange::Binance => "bnc",
            Exchange::Cryptopia => "crt",
            Exchange::Simulation => "sim",
        }
    }
}

impl ToString for Exchange {
    fn to_string(&self) -> String {
        match self {
            Exchange::Unknown => "Unknown".to_owned(),
            Exchange::Poloniex => "Poloniex".to_owned(),
            Exchange::Bitfinex => "Bitfinex".to_owned(),
            Exchange::Bittrex => "Bittrex".to_owned(),
            Exchange::Binance => "Binance".to_owned(),
            Exchange::Cryptopia => "Cryptopia".to_owned(),
            Exchange::Simulation => "Simulation".to_owned(),
        }
    }
}

impl FromStr for Exchange {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "Unknown" => Ok(Exchange::Unknown),
            "Poloniex" => Ok(Exchange::Poloniex),
            "Bitfinex" => Ok(Exchange::Bitfinex),
            "Bittrex" => Ok(Exchange::Bittrex),
            "Binance" => Ok(Exchange::Binance),
            "Cryptopia" => Ok(Exchange::Cryptopia),
            "Simulation" => Ok(Exchange::Simulation),
            "unk" => Ok(Exchange::Unknown),
            "pnx" => Ok(Exchange::Poloniex),
            "bfx" => Ok(Exchange::Bitfinex),
            "brx" => Ok(Exchange::Bittrex),
            "bnc" => Ok(Exchange::Binance),
            "crt" => Ok(Exchange::Cryptopia),
            "sim" => Ok(Exchange::Simulation),
            _ => Err(Error::InvalidExchange(s.to_owned())),
        }
    }
}

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

    #[test]
    fn exchange_debug() {
        assert_eq!("pnx", Exchange::Poloniex.short());
        assert_eq!("Poloniex".to_owned(), format!("{:?}", Exchange::Poloniex));
    }
}