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
//! Poloniex WebSocket stream.

#![feature(test, try_from)]

extern crate crypto_currency as currency;
extern crate crypto_market as market;
extern crate crypto_market_event as event;
extern crate crypto_market_stream as stream;
extern crate crypto_market_stream_ws as ws;
extern crate crypto_util as util;
#[macro_use]
extern crate error_chain;
extern crate futures;
#[macro_use]
extern crate log;
extern crate serde_json;
extern crate test;

pub mod errors;
mod parse;
mod serialize;

use market::Market;
use stream::Command;
use ws::Message;

use self::errors::Error;
use self::parse::parse_message;
use self::serialize::serialize;

/// Poloniex WebSocket protocol.
pub struct Protocol;

// Implementation of Poloniex WebSocket protocol.
impl ws::Protocol for Protocol {
    /// Protocol error.
    type Error = Error;

    /// Returns `Poloniex` WebSocket stream address.
    fn address() -> &'static str {
        "wss://api2.poloniex.com:443"
    }

    /// Returns `Poloniex` market identifier.
    fn market() -> Market {
        Market::Poloniex
    }

    /// Parses received WebSocket message.
    fn parse(msg: &str) -> Result<Option<Message>, Self::Error> {
        parse_message(msg).map_err(|e| e.into())
    }

    /// Serializes WebSocket command.
    fn serialize(cmd: Command) -> String {
        serialize(cmd)
    }
}