trading_types/
depth.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{DepthMap, Liq};
4
5/// Depth (order book)
6#[derive(Serialize, Deserialize, Default, Debug, Clone)]
7pub struct Depth {
8    pub asks: Vec<Liq>,
9    pub bids: Vec<Liq>,
10}
11
12impl Depth {
13    pub fn new() -> Self {
14        Self::default()
15    }
16
17    pub fn replace(&mut self, other: Self) {
18        *self = other;
19    }
20
21    pub fn into_depthmap(self) -> DepthMap {
22        let mut m = DepthMap::new();
23        m.asks = self.asks.into_iter().map(|a| (a.p, a)).collect();
24        m.bids = self.bids.into_iter().map(|b| (b.p, b)).collect();
25        m
26    }
27}
28
29impl From<(&[&[String]], &[&[String]])> for Depth {
30    fn from((aa, bb): (&[&[String]], &[&[String]])) -> Self {
31        let aa: Vec<Liq> = aa.iter().map(|&a| a.into()).collect();
32        let bb: Vec<Liq> = bb.iter().map(|&b| b.into()).collect();
33        Depth { asks: aa, bids: bb }
34    }
35}
36
37impl From<(Vec<Vec<std::string::String>>, Vec<Vec<std::string::String>>)> for Depth {
38    fn from((aa, bb): (Vec<Vec<std::string::String>>, Vec<Vec<std::string::String>>)) -> Self {
39        let aa: Vec<Liq> = aa.iter().map(|a| a.into()).collect();
40        let bb: Vec<Liq> = bb.iter().map(|b| b.into()).collect();
41        Depth { asks: aa, bids: bb }
42    }
43}
44
45impl From<(&[&[f64]], &[&[f64]])> for Depth {
46    fn from((aa, bb): (&[&[f64]], &[&[f64]])) -> Self {
47        let aa: Vec<Liq> = aa.iter().map(|&a| a.into()).collect();
48        let bb: Vec<Liq> = bb.iter().map(|&b| b.into()).collect();
49        Depth { asks: aa, bids: bb }
50    }
51}