use crate::Value;
use indexmap::IndexMap;
pub const BOOK_PLAIN: &str = "OrderBook";
pub const BOOK_INDEXED: &str = "IndexedOrderBook";
pub const BOOK_COUNTED: &str = "CountedOrderBook";
pub const SIDE_PLAIN: &str = "OrderBookSide";
pub const SIDE_INDEXED: &str = "IndexedOrderBookSide";
pub const SIDE_COUNTED: &str = "CountedOrderBookSide";
const MAX_DEPTH_SENTINEL: i64 = i64::MAX / 2;
fn iso8601_millis(ts: i64) -> Option<String> {
if ts < 0 { return None; }
chrono::DateTime::<chrono::Utc>::from_timestamp_millis(ts)
.map(|t| t.to_rfc3339_opts(chrono::SecondsFormat::Millis, true))
}
fn new_side(kind: &str, is_bid: bool, depth: i64, deltas: &Value) -> Value {
let mut side = crate::value::make_side_marker(kind, is_bid, depth);
if let Value::Arr(rows) = deltas {
for row in rows.iter() {
if let Value::Arr(_) = row {
side.store_array(row.clone());
}
}
}
side
}
fn build_book(kind: &str, snapshot: Value, depth: Value) -> Value {
let max_depth = match &depth {
Value::Int(n) if *n > 0 => *n,
_ => MAX_DEPTH_SENTINEL,
};
let (side_kind, _) = match kind {
BOOK_INDEXED => (SIDE_INDEXED, true),
BOOK_COUNTED => (SIDE_COUNTED, true),
_ => (SIDE_PLAIN, true),
};
let bids_deltas = crate::get_value(&snapshot, &Value::Str("bids".to_string()));
let asks_deltas = crate::get_value(&snapshot, &Value::Str("asks".to_string()));
let bids = new_side(side_kind, true, max_depth, &bids_deltas);
let asks = new_side(side_kind, false, max_depth, &asks_deltas);
let timestamp = match crate::get_value(&snapshot, &Value::Str("timestamp".to_string())) {
Value::Int(n) => Some(n),
Value::Float(f) => Some(f as i64),
_ => None,
};
let datetime = timestamp.and_then(iso8601_millis);
let nonce = match crate::get_value(&snapshot, &Value::Str("nonce".to_string())) {
Value::Int(n) => Value::Int(n),
Value::Float(f) => Value::Int(f as i64),
_ => Value::Null,
};
let symbol = crate::get_value(&snapshot, &Value::Str("symbol".to_string()));
let book_id = crate::value::alloc_book_id();
crate::value::book_meta_set(book_id, "timestamp", timestamp.map(Value::Int).unwrap_or(Value::Null));
crate::value::book_meta_set(book_id, "datetime", datetime.map(Value::Str).unwrap_or(Value::Null));
crate::value::book_meta_set(book_id, "nonce", nonce);
crate::value::book_meta_set(book_id, "symbol", symbol);
let mut m = IndexMap::new();
m.insert("__bookKind".to_string(), Value::Str(kind.to_string()));
m.insert("__book_id".to_string(), Value::Int(book_id));
m.insert("_depth".to_string(), Value::Int(max_depth));
m.insert("bids".to_string(), bids);
m.insert("asks".to_string(), asks);
Value::Map(m)
}
pub struct Bids;
impl Bids {
pub fn new(deltas: Value, depth: Value) -> Value {
let max_depth = match &depth {
Value::Int(n) if *n > 0 => *n,
_ => MAX_DEPTH_SENTINEL,
};
new_side(SIDE_PLAIN, true, max_depth, &deltas)
}
}
pub struct Asks;
impl Asks {
pub fn new(deltas: Value, depth: Value) -> Value {
let max_depth = match &depth {
Value::Int(n) if *n > 0 => *n,
_ => MAX_DEPTH_SENTINEL,
};
new_side(SIDE_PLAIN, false, max_depth, &deltas)
}
}
pub struct OrderBook;
impl OrderBook {
pub fn new(snapshot: Value, depth: Value) -> Value {
build_book(BOOK_PLAIN, snapshot, depth)
}
}
pub struct IndexedOrderBook;
impl IndexedOrderBook {
pub fn new(snapshot: Value, depth: Value) -> Value {
build_book(BOOK_INDEXED, snapshot, depth)
}
}
pub struct CountedOrderBook;
impl CountedOrderBook {
pub fn new(snapshot: Value, depth: Value) -> Value {
build_book(BOOK_COUNTED, snapshot, depth)
}
}