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
use rust_decimal::prelude::Decimal;
use serde::Deserialize;
use super::OrderSide;
use super::shared::string_to_decimal;
use super::shared::string_to_opt_decimal;

/// This enum represents a ticker
#[derive(Deserialize, Debug, Clone)]
#[serde(untagged)]
#[serde(rename_all = "camelCase")]
pub enum Ticker {
    Full {
        trade_id: usize,
        sequence: usize,
        time: String,
        product_id: String,
        #[serde(with = "string_to_decimal")]
        price: Decimal,
        side: OrderSide,
        #[serde(with = "string_to_decimal")]
        last_size: Decimal,
        #[serde(with = "string_to_opt_decimal")]
        best_bid: Option<Decimal>,
        #[serde(with = "string_to_opt_decimal")]
        best_ask: Option<Decimal>,
    },
    Empty {
        sequence: usize,
        product_id: String,
        #[serde(with = "string_to_opt_decimal")]
        price: Option<Decimal>,
    },
}

impl Ticker {
    pub fn price(&self) -> Decimal {
        match self {
            Ticker::Full { price, .. } => *price,
            Ticker::Empty { price, .. } => price.expect("Couldn't get price."),
        }
    }

    pub fn time(&self) -> Option<&String> {
        match self {
            Ticker::Full { time, .. } => Some(time),
            Ticker::Empty { .. } => None,
        }
    }

    pub fn sequence(&self) -> &usize {
        match self {
            Ticker::Full { sequence, .. } => sequence,
            Ticker::Empty { sequence, .. } => sequence,
        }
    }

    pub fn bid(&self) -> Option<Decimal> {
        match self {
            Ticker::Full { best_bid, .. } => Some(best_bid.expect("Couldn't get best bid.")),
            Ticker::Empty { .. } => None,
        }
    }

    pub fn ask(&self) -> Option<Decimal> {
        match self {
            Ticker::Full { best_ask, .. } => Some(best_ask.expect("Couldn't get best ask.")),
            Ticker::Empty { .. } => None,
        }
    }
}