openlimits_coinbase/model/websocket/
ticker.rs

1use rust_decimal::prelude::Decimal;
2use serde::Deserialize;
3use super::OrderSide;
4use super::shared::string_to_decimal;
5use super::shared::string_to_opt_decimal;
6
7/// This enum represents a ticker
8#[derive(Deserialize, Debug, Clone, PartialEq)]
9#[serde(untagged)]
10#[serde(rename_all = "camelCase")]
11pub enum Ticker {
12    Full {
13        trade_id: usize,
14        sequence: usize,
15        time: String,
16        product_id: String,
17        #[serde(with = "string_to_decimal")]
18        price: Decimal,
19        side: OrderSide,
20        #[serde(with = "string_to_decimal")]
21        last_size: Decimal,
22        #[serde(with = "string_to_opt_decimal")]
23        best_bid: Option<Decimal>,
24        #[serde(with = "string_to_opt_decimal")]
25        best_ask: Option<Decimal>,
26    },
27    Empty {
28        sequence: usize,
29        product_id: String,
30        #[serde(with = "string_to_opt_decimal")]
31        price: Option<Decimal>,
32    },
33}
34
35impl Ticker {
36    pub fn price(&self) -> Decimal {
37        match self {
38            Ticker::Full { price, .. } => *price,
39            Ticker::Empty { price, .. } => price.expect("Couldn't get price."),
40        }
41    }
42
43    pub fn time(&self) -> Option<&String> {
44        match self {
45            Ticker::Full { time, .. } => Some(time),
46            Ticker::Empty { .. } => None,
47        }
48    }
49
50    pub fn sequence(&self) -> &usize {
51        match self {
52            Ticker::Full { sequence, .. } => sequence,
53            Ticker::Empty { sequence, .. } => sequence,
54        }
55    }
56
57    pub fn bid(&self) -> Option<Decimal> {
58        match self {
59            Ticker::Full { best_bid, .. } => Some(best_bid.expect("Couldn't get best bid.")),
60            Ticker::Empty { .. } => None,
61        }
62    }
63
64    pub fn ask(&self) -> Option<Decimal> {
65        match self {
66            Ticker::Full { best_ask, .. } => Some(best_ask.expect("Couldn't get best ask.")),
67            Ticker::Empty { .. } => None,
68        }
69    }
70}