cxmr-exchanges 0.0.1

Exchanges identifiers.
Documentation
//! 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));
    }
}