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
use std::fmt;
use std::cell::Cell;
use crate::order_book::OrderBook;
use crate::tick::{TickUnit, Tick};
thread_local! {
static DISPLAY_LIMIT: Cell<usize> = Cell::new(5);
static DISPLAY_PRICE_TICK: Cell<Option<Tick>> = Cell::new(None);
static DISPLAY_SIZE_TICK: Cell<Option<Tick>> = Cell::new(None);
}
pub fn set_limit(limit: usize) {
DISPLAY_LIMIT.with(|dl| dl.set(limit));
}
pub fn set_price_tick(maybe_tick: Option<Tick>) {
DISPLAY_PRICE_TICK.with(|dt| dt.set(maybe_tick));
}
pub fn set_size_tick(maybe_tick: Option<Tick>) {
DISPLAY_SIZE_TICK.with(|dt| dt.set(maybe_tick));
}
pub fn displayable_price(ticked: TickUnit) -> String {
match DISPLAY_PRICE_TICK.with(|dt| dt.get()) {
Some(tick) => tick.unticked(ticked).unwrap(),
None => format!("{}", ticked),
}
}
pub fn displayable_size(ticked: TickUnit) -> String {
match DISPLAY_SIZE_TICK.with(|dt| dt.get()) {
Some(tick) => tick.unticked(ticked).unwrap(),
None => format!("{}", ticked),
}
}
impl fmt::Display for OrderBook {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let display_limit = DISPLAY_LIMIT.with(|dl| dl.get());
writeln!(f, "## ASK")?;
let ask: Vec<_> = self.ask()
.take(display_limit)
.collect();
for (&price, &size) in ask.iter().rev() {
writeln!(f, "{}:\t{}", displayable_price(price), displayable_size(size))?;
}
write!(f, "\n\n")?;
for (&price, &size) in self.bid()
.take(display_limit)
{
writeln!(f, "{}:\t{}", displayable_price(price), displayable_size(size))?;
}
writeln!(f, "## BID")?;
Ok(())
}
}