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
//! Websocket stream protocol traits.

use cxmr_currency::CurrencyPair;

use super::Error;

/// Subscription type.
#[derive(Clone)]
pub enum Subscription {
    /// Currency pairs subscription.
    Pairs(Vec<CurrencyPair>),

    /// Account data subscription.
    User(String),
}

/// WebSocket Stream parser trait.
pub trait Parser<I>: Sized {
    /// Parses received WebSocket message.
    fn parse(&mut self, msg: &str) -> Result<Option<I>, Error>;
}

/// Private stream protocol.
pub trait Protocol<I>: Sized + 'static {
    /// Private protocol parser.
    type Parser: Parser<I>;

    /// Creates protocol parser and stream address.
    fn subscription(sub: &Subscription) -> Option<(Command, Self::Parser)>;
}

/// Subscription command.
pub enum Command {
    /// Connect to given address.
    Connect(String),

    /// Send commands after connect.
    Commands(String, Vec<String>),
}

impl Command {
    /// Returns inner address.
    pub fn address(&self) -> &str {
        match self {
            Command::Connect(ref address) => address,
            Command::Commands(ref address, _) => address,
        }
    }

    /// Returns inner commands.
    pub fn commands(&self) -> Option<&Vec<String>> {
        match self {
            Command::Connect(_) => None,
            Command::Commands(_, ref cmd) => Some(cmd),
        }
    }
}