pub use indexmap::IndexMap as HashMap;
use std::sync::Arc;
#[cfg(feature = "transpiled-base")]
use crate::exchange_generated::ExchangeBase;
#[derive(Debug, Clone, PartialEq)]
pub enum Value {
Null,
Bool(bool),
Int(i64),
Float(f64),
Str(String),
Dict(Arc<HashMap<String, Value>>),
Arr(Arc<Vec<Value>>),
}
impl Default for Value {
fn default() -> Self {
Value::Null
}
}
impl Value {
pub fn reduce(&mut self) {
let m = match self { Value::Dict(m) if m.contains_key("__precise") => Arc::make_mut(m), _ => return };
let mut decimals: i64 = match m.get("decimals") {
Some(Value::Int(n)) => *n,
_ => return,
};
let mut integer: String = match m.get("integer") {
Some(Value::Str(s)) => s.clone(),
_ => return,
};
if decimals < 0 {
let (sign, digits) = if let Some(rest) = integer.strip_prefix('-') {
("-", rest.to_string())
} else { ("", integer.clone()) };
let padded = format!("{digits}{:0<width$}", "", width = (-decimals) as usize);
integer = format!("{sign}{padded}");
decimals = 0;
}
while decimals > 0 && integer.ends_with('0') && integer.len() > 1 {
integer.pop();
decimals -= 1;
}
m.insert("integer".to_string(), Value::Str(integer));
m.insert("decimals".to_string(), Value::Int(decimals));
}
pub fn append(&mut self, item: Value) {
match self {
Value::Arr(a) => Arc::make_mut(a).push(item),
Value::Dict(m) if m.contains_key("__cacheKind") => {
cache_append(self, item);
}
_ => {}
}
}
pub fn clear(&mut self) {
match self {
Value::Arr(a) => Arc::make_mut(a).clear(),
Value::Dict(m) if m.contains_key("__cacheKind") => {
if let Some(id) = cache_id_of_m(m) {
with_cache_cell(id, |c| {
if let Some(Value::Arr(data)) = c.get_mut("_data") { Arc::make_mut(data).clear(); }
cache_reset_bookkeeping(c);
});
} else {
let m = Arc::make_mut(m);
if let Some(Value::Arr(data)) = m.get_mut("_data") {
Arc::make_mut(data).clear();
}
cache_reset_bookkeeping(m);
}
}
_ => {}
}
}
pub fn get_limit(&mut self, symbol: Value, limit: Value) -> Value {
if matches!(self, Value::Dict(d) if d.contains_key("__cacheKind")) {
cache_get_limit(self, symbol, limit)
} else {
limit
}
}
#[allow(non_snake_case)]
pub fn List(items: Vec<Value>) -> Value {
Value::Arr(Arc::new(items))
}
#[allow(non_snake_case)]
pub fn Map(m: HashMap<String, Value>) -> Value {
Value::Dict(Arc::new(m))
}
#[allow(non_snake_case)]
pub fn Array(a: Vec<Value>) -> Value {
Value::Arr(Arc::new(a))
}
pub fn str(s: impl Into<String>) -> Self {
Value::Str(s.into())
}
pub fn int(n: i64) -> Self {
Value::Int(n)
}
pub fn float(f: f64) -> Self {
Value::Float(f)
}
pub fn bool(b: bool) -> Self {
Value::Bool(b)
}
pub fn map(m: HashMap<String, Value>) -> Self {
Value::Map(m)
}
pub fn array(a: Vec<Value>) -> Self {
Value::Array(a)
}
pub fn is_null(&self) -> bool { matches!(self, Value::Null) }
pub fn is_str(&self) -> bool { matches!(self, Value::Str(_)) }
pub fn is_int(&self) -> bool { matches!(self, Value::Int(_)) }
pub fn is_float(&self) -> bool { matches!(self, Value::Float(_)) }
pub fn is_number(&self) -> bool { matches!(self, Value::Int(_) | Value::Float(_)) }
pub fn is_bool(&self) -> bool { matches!(self, Value::Bool(_)) }
pub fn is_map(&self) -> bool { matches!(self, Value::Dict(_)) }
pub fn is_array(&self) -> bool { matches!(self, Value::Arr(_)) }
pub fn is_truthy(&self) -> bool {
match self {
Value::Null => false,
Value::Bool(b) => *b,
Value::Int(n) => *n != 0,
Value::Float(f) => *f != 0.0,
Value::Str(s) => !s.is_empty(),
Value::Dict(m) => !m.is_empty(),
Value::Arr(a) => !a.is_empty(),
}
}
pub fn as_str(&self) -> Option<&str> { if let Value::Str(s) = self { Some(s) } else { None } }
pub fn as_i64(&self) -> Option<i64> { if let Value::Int(n) = self { Some(*n) } else { None } }
pub fn as_f64(&self) -> Option<f64> {
match self {
Value::Float(f) => Some(*f),
Value::Int(n) => Some(*n as f64),
_ => None,
}
}
pub fn as_bool(&self) -> Option<bool> { if let Value::Bool(b) = self { Some(*b) } else { None } }
pub fn as_map(&self) -> Option<&HashMap<String, Value>> {
if let Value::Dict(m) = self { Some(&**m) } else { None }
}
pub fn as_array(&self) -> Option<&Vec<Value>> {
if let Value::Arr(a) = self { Some(&**a) } else { None }
}
pub fn len(&self) -> usize {
match self {
Value::Arr(a) => a.len(),
Value::Dict(m) => m.len(),
Value::Str(s) => s.chars().count(),
_ => 0,
}
}
pub fn is_empty(&self) -> bool { self.len() == 0 }
pub fn describe(&self) -> Value { Value::Null }
pub fn reject(&self, args: &[Value]) -> Value {
crate::pro::ws_client::value_reject(self, args)
}
pub fn resolve(&self, args: &[Value]) -> Value {
crate::pro::ws_client::value_resolve(self, args)
}
pub fn future(&self, args: &[Value]) -> Value {
self.open_flight(args.get(0).cloned().unwrap_or(Value::Null))
}
pub fn reusable_future(&self, msg_hash: Value) -> Value {
self.open_flight(msg_hash)
}
fn open_flight(&self, msg_hash: Value) -> Value {
let hash = match &msg_hash {
Value::Str(h) => h.clone(),
_ => return msg_hash,
};
match crate::pro::ws_client::url_of(self) {
Some(url) => {
let led = crate::pro::ws_client::begin_flight(&url, &hash);
crate::pro::ws_client::flight_handle(&url, &hash, led)
}
None => msg_hash,
}
}
pub fn hashmap(&self) -> Value {
if let Value::Dict(d) = self {
if let Some(id) = cache_id_of(self) {
let hm = with_cache_cell(id, |m| m.get("hashmap").cloned().unwrap_or(Value::Null));
return tag_cache_hashmap_buckets(hm, id);
}
if let Some(v) = d.get("hashmap") { return v.clone(); }
}
Value::Null
}
pub fn send(&self, args: &[Value]) -> Value {
crate::pro::ws_client::value_send(self, args)
}
pub fn decode_proto_msg(&self, _args: &[Value]) -> Value { Value::Null }
pub fn on_pong(&self, _args: &[Value]) -> Value {
crate::pro::ws_client::value_on_pong(self)
}
pub fn call(&self, _args: &[Value]) -> Value { Value::Null }
pub fn is_dictionary(&self, value: Value) -> Value {
Value::Bool(matches!(value, Value::Dict(_)))
}
#[cfg(feature = "transpiled-base")]
fn snapshot_as_exchange(&self) -> crate::exchange::Exchange {
let mut ex = crate::exchange::Exchange::new(None);
ex.options = crate::runtime::get_value(self, &Value::Str("options".to_string()));
ex.currencies = crate::runtime::get_value(self, &Value::Str("currencies".to_string()));
ex
}
pub fn network_code_to_id(&self, code: Value, args: &[Value]) -> Value {
#[cfg(feature = "transpiled-base")]
{
let currency = args.first().cloned().unwrap_or(Value::Null);
return crate::exchange::BaseCore::new(self.snapshot_as_exchange()).network_code_to_id(code, &[currency]);
}
#[cfg(not(feature = "transpiled-base"))]
{
let _ = args;
code
}
}
pub fn network_id_to_code(&self, args: &[Value]) -> Value {
#[cfg(feature = "transpiled-base")]
{
return crate::exchange::BaseCore::new(self.snapshot_as_exchange()).network_id_to_code(args);
}
#[cfg(not(feature = "transpiled-base"))]
{
let _ = args;
Value::Null
}
}
pub fn extend(&self, a: Value, args: &[Value]) -> Value {
#[cfg(feature = "transpiled-base")]
{
return self.snapshot_as_exchange().extend(a, args);
}
#[cfg(not(feature = "transpiled-base"))]
{
let _ = args;
a
}
}
pub fn set_markets(&mut self, markets: Value) -> Value {
#[cfg(feature = "transpiled-base")]
{
if let Value::Dict(m) = &*self {
if let Some(Value::Str(id)) = m.get("__live_id") {
if let Some(cb) = live_set_markets_get() {
return cb(&id.clone(), markets);
}
}
}
let mut ex = crate::exchange::BaseCore::new(self.snapshot_as_exchange());
let result = ex.set_markets(markets, &[]);
crate::runtime::add_element_to_object(self, &Value::Str("markets".to_string()), ex.markets.clone());
crate::runtime::add_element_to_object(self, &Value::Str("markets_by_id".to_string()), ex.markets_by_id.clone());
crate::runtime::add_element_to_object(self, &Value::Str("symbols".to_string()), ex.symbols.clone());
return result;
}
#[cfg(not(feature = "transpiled-base"))]
{
let _ = markets;
Value::Null
}
}
pub fn store_array(&mut self, delta: Value) {
if side_kind(self).is_some() { side_store_array(self, delta); }
}
pub fn append_to_array(&self, _v: Value) {}
pub fn to_json(&self) -> serde_json::Value {
match self {
Value::Null => serde_json::Value::Null,
Value::Bool(b) => serde_json::Value::Bool(*b),
Value::Int(n) => serde_json::json!(*n),
Value::Float(f) => serde_json::json!(*f),
Value::Str(s) => serde_json::Value::String(s.clone()),
Value::Arr(a) => serde_json::Value::Array(a.iter().map(Value::to_json).collect()),
Value::Dict(m) => {
if m.contains_key("__side_id") {
let entries = side_entries_view(m);
return serde_json::Value::Array(entries.iter().map(Value::to_json).collect());
}
let is_book = m.contains_key("__book_id") || m.contains_key("__bookKind");
let mut obj = serde_json::Map::new();
for (k, v) in m.iter() {
if is_book && matches!(k.as_str(), "__bookKind" | "__book_id" | "_depth") {
continue;
}
obj.insert(k.clone(), v.to_json());
}
if let Some(Value::Int(id)) = m.get("__book_id") {
for (k, v) in book_meta_snapshot(*id) { obj.insert(k, v.to_json()); }
}
if m.contains_key("__cacheKind") {
let data = cache_data_snapshot(m);
obj.insert("_data".to_string(),
serde_json::Value::Array(data.iter().map(Value::to_json).collect()));
}
serde_json::Value::Object(obj)
}
}
}
pub fn from_json(v: &serde_json::Value) -> Self {
match v {
serde_json::Value::Null => Value::Null,
serde_json::Value::Bool(b) => Value::Bool(*b),
serde_json::Value::Number(n) => {
if let Some(i) = n.as_i64() {
Value::Int(i)
} else if n.is_u64() {
Value::Str(n.to_string())
} else if let Some(f) = n.as_f64() {
Value::Float(f)
} else {
Value::Null
}
}
serde_json::Value::String(s) => Value::Str(s.clone()),
serde_json::Value::Array(a) => Value::Array(a.iter().map(Value::from_json).collect()),
serde_json::Value::Object(m) => Value::Map(
m.iter().map(|(k, v)| (k.clone(), Value::from_json(v))).collect()
),
}
}
}
impl std::fmt::Display for Value {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Value::Null => write!(f, "null"),
Value::Bool(b) => write!(f, "{b}"),
Value::Int(n) => write!(f, "{n}"),
Value::Float(fl) => write!(f, "{fl}"),
Value::Str(s) => write!(f, "{s}"),
Value::Arr(a) => write!(f, "[{} items]", a.len()),
Value::Dict(m) => write!(f, "{{{} keys}}", m.len()),
}
}
}
impl std::ops::Not for Value {
type Output = Value;
fn not(self) -> Value { Value::Bool(!self.is_truthy()) }
}
impl std::ops::Not for &Value {
type Output = Value;
fn not(self) -> Value { Value::Bool(!self.is_truthy()) }
}
impl std::ops::BitAnd for Value {
type Output = Value;
fn bitand(self, rhs: Value) -> Value {
Value::Bool(self.is_truthy() && rhs.is_truthy())
}
}
impl std::ops::BitOr for Value {
type Output = Value;
fn bitor(self, rhs: Value) -> Value {
Value::Bool(self.is_truthy() || rhs.is_truthy())
}
}
impl From<&str> for Value { fn from(s: &str) -> Self { Value::Str(s.to_owned()) } }
impl From<String> for Value { fn from(s: String) -> Self { Value::Str(s) } }
impl From<i64> for Value { fn from(n: i64) -> Self { Value::Int(n) } }
impl From<f64> for Value { fn from(f: f64) -> Self { Value::Float(f) } }
impl From<bool> for Value { fn from(b: bool) -> Self { Value::Bool(b) } }
impl From<crate::ExchangeError> for Value {
fn from(e: crate::ExchangeError) -> Self {
Value::Str(format!("[{}] {}", e.kind, e.message))
}
}
impl From<HashMap<String, Value>> for Value {
fn from(m: HashMap<String, Value>) -> Self { Value::Map(m) }
}
impl From<Vec<Value>> for Value {
fn from(a: Vec<Value>) -> Self { Value::Array(a) }
}
impl From<serde_json::Value> for Value {
fn from(v: serde_json::Value) -> Self { Value::from_json(&v) }
}
type LiveLookupFn = fn(id: &str, key: &str) -> Option<Value>;
static LIVE_LOOKUP_PTR: std::sync::atomic::AtomicUsize =
std::sync::atomic::AtomicUsize::new(0);
pub fn set_live_lookup(f: LiveLookupFn) {
LIVE_LOOKUP_PTR.store(f as usize, std::sync::atomic::Ordering::Release);
}
fn live_lookup_get() -> Option<LiveLookupFn> {
let n = LIVE_LOOKUP_PTR.load(std::sync::atomic::Ordering::Acquire);
if n == 0 { None } else {
Some(unsafe { std::mem::transmute::<usize, LiveLookupFn>(n) })
}
}
type LiveSetMarketsFn = fn(id: &str, markets: Value) -> Value;
static LIVE_SET_MARKETS_PTR: std::sync::atomic::AtomicUsize =
std::sync::atomic::AtomicUsize::new(0);
pub fn set_live_set_markets(f: LiveSetMarketsFn) {
LIVE_SET_MARKETS_PTR.store(f as usize, std::sync::atomic::Ordering::Release);
}
fn live_set_markets_get() -> Option<LiveSetMarketsFn> {
let n = LIVE_SET_MARKETS_PTR.load(std::sync::atomic::Ordering::Acquire);
if n == 0 { None } else {
Some(unsafe { std::mem::transmute::<usize, LiveSetMarketsFn>(n) })
}
}
#[inline]
pub fn get_value_k(obj: &Value, key: &str) -> Value {
if let Value::Dict(m) = obj {
if is_book_meta_key(key) {
if let Some(id) = book_id_of(m) { return book_meta_get(id, key); }
}
if key == "cache" {
if let Some(id) = book_id_of(m) { return book_cache_handle(id); }
}
if let Some(v) = m.get(key) {
if !matches!(v, Value::Null) {
return v.clone();
}
}
if let Some(id_val) = m.get("__live_id") {
if let (Value::Str(id), Some(cb)) = (id_val, live_lookup_get()) {
if let Some(v) = cb(id, key) { return v; }
}
}
}
Value::Null
}
pub fn get_value(obj: &Value, key: &Value) -> Value {
if let Value::Dict(m) = obj {
let idx = match key {
Value::Int(i) if *i >= 0 => Some(*i as usize),
Value::Str(s) => s.parse::<usize>().ok(),
_ => None,
};
if let Some(i) = idx {
if let Some(id) = book_cache_id_of(m) {
return book_cache_at(id, i);
}
if m.contains_key("__cacheKind") {
return cache_at(m, i);
}
if m.contains_key("__sideKind") {
return side_entry_at(m, i);
}
}
}
match (obj, key) {
(Value::Dict(m), Value::Str(k)) => {
if is_book_meta_key(k) {
if let Some(id) = book_id_of(m) { return book_meta_get(id, k); }
}
if k == "cache" {
if let Some(id) = book_id_of(m) { return book_cache_handle(id); }
}
if k == "hashmap" && m.contains_key("__cacheKind") {
if let Some(id) = cache_id_of_m(m) {
return with_cache_cell(id, |c| c.get("hashmap").cloned().unwrap_or(Value::Null));
}
return m.get("hashmap").cloned().unwrap_or(Value::Null);
}
if (k == "subscriptions" || k == "futures")
&& m.contains_key("subscriptions") && m.contains_key("futures")
{
if let Some(Value::Str(url)) = m.get("url") {
return crate::pro::ws_client::client_field_live(url, k);
}
}
let snapshot_val = m.get(k);
if let Some(v) = snapshot_val {
if !matches!(v, Value::Null) {
return v.clone();
}
}
if let Some(id_val) = m.get("__live_id") {
if let (Value::Str(id), Some(cb)) = (id_val, live_lookup_get()) {
if let Some(v) = cb(id, k) { return v; }
}
}
Value::Null
}
(Value::Arr(a), Value::Int(i)) => a.get(*i as usize).cloned().unwrap_or(Value::Null),
(Value::Arr(a), Value::Str(k)) => {
if let Ok(i) = k.parse::<usize>() {
a.get(i).cloned().unwrap_or(Value::Null)
} else {
Value::Null
}
}
(Value::Str(s), Value::Int(i)) => {
if *i < 0 {
return Value::Null;
}
match s.chars().nth(*i as usize) {
Some(c) => Value::Str(c.to_string()),
None => Value::Null,
}
}
_ => Value::Null,
}
}
pub fn set_value(obj: &mut Value, key: &Value, val: Value) {
match (obj, key) {
(Value::Dict(m), Value::Str(k)) => {
if try_book_meta_write(m, k, &val) { return; }
try_ws_subs_write(m, k, &val);
try_ws_sub_field_write(m, k, &val);
Arc::make_mut(m).insert(k.clone(), val);
}
(Value::Arr(a), Value::Int(i)) => {
let idx = *i as usize;
let a = Arc::make_mut(a);
if idx < a.len() { a[idx] = val; }
}
_ => {}
}
}
pub fn safe_string(obj: &Value, key: &str, default: Option<&str>) -> Option<String> {
let v = get_value(obj, &Value::Str(key.to_owned()));
match v {
Value::Str(s) => Some(s),
Value::Int(n) => Some(n.to_string()),
Value::Float(f)=> Some(f.to_string()),
Value::Null => default.map(str::to_owned),
_ => default.map(str::to_owned),
}
}
pub fn safe_number(obj: &Value, key: &str, default: Option<f64>) -> Option<f64> {
let v = get_value(obj, &Value::Str(key.to_owned()));
match v {
Value::Float(f) => Some(f),
Value::Int(n) => Some(n as f64),
Value::Str(s) => s.parse::<f64>().ok().or(default),
_ => default,
}
}
pub fn safe_integer(obj: &Value, key: &str, default: Option<i64>) -> Option<i64> {
let v = get_value(obj, &Value::Str(key.to_owned()));
match v {
Value::Int(n) => Some(n),
Value::Float(f) => Some(f as i64),
Value::Str(s) => s.parse::<i64>().ok().or(default),
_ => default,
}
}
pub fn safe_bool(obj: &Value, key: &str, default: Option<bool>) -> Option<bool> {
let v = get_value(obj, &Value::Str(key.to_owned()));
match v {
Value::Bool(b) => Some(b),
Value::Int(n) => Some(n != 0),
Value::Str(s) => match s.to_lowercase().as_str() {
"true" | "1" | "yes" => Some(true),
"false"| "0" | "no" => Some(false),
_ => default,
},
_ => default,
}
}
pub fn deep_extend(dst: Value, src: Value) -> Value {
match (dst, src) {
(Value::Dict(mut d), Value::Dict(s)) => {
let dm = Arc::make_mut(&mut d);
let sm = Arc::try_unwrap(s).unwrap_or_else(|arc| (*arc).clone());
for (k, v) in sm {
let entry = dm.shift_remove(&k).unwrap_or(Value::Null);
dm.insert(k, deep_extend(entry, v));
}
Value::Dict(d)
}
(_, src) => src,
}
}
fn cache_str_field(item: &Value, key: &str) -> Option<String> {
match item {
Value::Dict(d) => match d.get(key) {
Some(Value::Str(s)) => Some(s.clone()),
Some(Value::Int(n)) => Some(n.to_string()),
Some(Value::Float(f)) => Some(f.to_string()),
Some(Value::Bool(b)) => Some(b.to_string()),
_ => None,
},
_ => None,
}
}
fn tag_cache_hashmap_buckets(hm: Value, id: i64) -> Value {
if let Value::Dict(d) = &hm {
let mut out: HashMap<String, Value> = HashMap::new();
for (sym, bucket) in d.iter() {
if let Value::Dict(bd) = bucket {
let mut nb: HashMap<String, Value> = (**bd).clone();
nb.insert(
"__cache_backref".to_string(),
Value::Arr(std::sync::Arc::new(vec![Value::Int(id), Value::Str(sym.clone())])),
);
out.insert(sym.clone(), Value::Dict(std::sync::Arc::new(nb)));
} else {
out.insert(sym.clone(), bucket.clone());
}
}
return Value::Dict(std::sync::Arc::new(out));
}
hm
}
pub(crate) fn try_cache_hashmap_write(bucket: &HashMap<String, Value>, key: &str, item: &Value) -> bool {
let (id, sym) = match bucket.get("__cache_backref") {
Some(Value::Arr(a)) => match (a.first(), a.get(1)) {
(Some(Value::Int(id)), Some(Value::Str(s))) => (*id, s.clone()),
_ => return false,
},
_ => return false,
};
with_cache_cell(id, |m| {
{
let hm = cache_dict_field_mut(m, "hashmap");
let b = hm.entry(sym.clone()).or_insert_with(|| Value::Map(HashMap::new()));
if let Value::Dict(bd) = b {
Arc::make_mut(bd).insert(key.to_string(), item.clone());
}
}
let data = cache_data_mut(m);
for x in data.iter_mut() {
if cache_str_field(x, "symbol").as_deref() == Some(&sym)
&& (cache_str_field(x, "id").as_deref() == Some(key)
|| cache_str_field(x, "side").as_deref() == Some(key))
{
*x = item.clone();
break;
}
}
});
true
}
fn cache_kind(v: &Value) -> Option<String> {
match v {
Value::Dict(d) => match d.get("__cacheKind") {
Some(Value::Str(s)) => Some(s.clone()),
_ => None,
},
_ => None,
}
}
fn cache_max_size(v: &Value) -> Option<usize> {
match v {
Value::Dict(d) => match d.get("maxSize") {
Some(Value::Int(n)) if *n > 0 => Some(*n as usize),
_ => None,
},
_ => None,
}
}
fn cache_data_mut(m: &mut HashMap<String, Value>) -> &mut Vec<Value> {
let entry = m.entry("_data".to_string()).or_insert_with(|| Value::Array(Vec::new()));
if let Value::Arr(a) = entry {
return Arc::make_mut(a);
}
*entry = Value::Array(Vec::new());
if let Value::Arr(a) = entry {
return Arc::make_mut(a);
}
unreachable!()
}
fn cache_dict_field_mut<'a>(m: &'a mut HashMap<String, Value>, key: &str) -> &'a mut HashMap<String, Value> {
let entry = m.entry(key.to_string()).or_insert_with(|| Value::Map(HashMap::new()));
if let Value::Dict(d) = entry {
return Arc::make_mut(d);
}
*entry = Value::Map(HashMap::new());
if let Value::Dict(d) = entry {
return Arc::make_mut(d);
}
unreachable!()
}
fn cache_int_field(m: &HashMap<String, Value>, key: &str) -> i64 {
match m.get(key) { Some(Value::Int(n)) => *n, _ => 0 }
}
fn cache_set_int(m: &mut HashMap<String, Value>, key: &str, value: i64) {
m.insert(key.to_string(), Value::Int(value));
}
fn cache_bool_field(m: &HashMap<String, Value>, key: &str) -> bool {
matches!(m.get(key), Some(Value::Bool(true)))
}
fn cache_set_bool(m: &mut HashMap<String, Value>, key: &str, value: bool) {
m.insert(key.to_string(), Value::Bool(value));
}
fn cache_reset_bookkeeping(state: &mut HashMap<String, Value>) {
for k in ["hashmap", "_newUpdatesBySymbol", "_seenUpdatesBySymbol", "_seenUpdatesAll",
"_clearUpdatesBySymbol", "_sizeTracker"] {
if state.contains_key(k) {
state.insert(k.to_string(), Value::Map(HashMap::new()));
}
}
if state.contains_key("_allNewUpdates") { cache_set_int(state, "_allNewUpdates", 0); }
if state.contains_key("_newUpdates") { cache_set_int(state, "_newUpdates", 0); }
if state.contains_key("_clearAllUpdates") { cache_set_bool(state, "_clearAllUpdates", false); }
if state.contains_key("_clearUpdates") { cache_set_bool(state, "_clearUpdates", false); }
}
fn cache_apply_clear_all(m: &mut HashMap<String, Value>) {
if cache_bool_field(m, "_clearAllUpdates") {
cache_set_bool(m, "_clearAllUpdates", false);
cache_set_int(m, "_allNewUpdates", 0);
cache_dict_field_mut(m, "_seenUpdatesAll").clear();
}
}
fn cache_take_symbol_clear(m: &mut HashMap<String, Value>, key1: &str) -> bool {
let cs = cache_dict_field_mut(m, "_clearUpdatesBySymbol");
if matches!(cs.get(key1), Some(Value::Bool(true))) {
cs.insert(key1.to_string(), Value::Bool(false));
true
} else {
false
}
}
fn cache_seen_add(m: &mut HashMap<String, Value>, field: &str, key1: &str, key2: &str) -> i64 {
let outer = cache_dict_field_mut(m, field);
let set = outer.entry(key1.to_string()).or_insert_with(|| Value::Map(HashMap::new()));
if let Value::Dict(sd) = set {
let sd = Arc::make_mut(sd);
sd.insert(key2.to_string(), Value::Bool(true));
sd.len() as i64
} else {
0
}
}
fn cache_seen_size(m: &mut HashMap<String, Value>, field: &str, key1: &str) -> i64 {
match cache_dict_field_mut(m, field).get(key1) {
Some(Value::Dict(sd)) => sd.len() as i64,
_ => 0,
}
}
fn cache_seen_clear(m: &mut HashMap<String, Value>, field: &str, key1: &str) {
if let Some(Value::Dict(sd)) = cache_dict_field_mut(m, field).get_mut(key1) {
Arc::make_mut(sd).clear();
}
}
fn cache_seen_remove(m: &mut HashMap<String, Value>, field: &str, key1: &str, key2: &str) -> bool {
let outer = cache_dict_field_mut(m, field);
let (removed, empty) = if let Some(Value::Dict(sd)) = outer.get_mut(key1) {
let sd = Arc::make_mut(sd);
let removed = sd.shift_remove(key2).is_some();
(removed, sd.is_empty())
} else {
(false, false)
};
if empty { outer.shift_remove(key1); }
removed
}
static CACHE_STORE: once_cell::sync::Lazy<std::sync::Mutex<std::collections::HashMap<i64, HashMap<String, Value>>>> =
once_cell::sync::Lazy::new(|| std::sync::Mutex::new(std::collections::HashMap::new()));
static NEXT_CACHE_ID: std::sync::atomic::AtomicI64 = std::sync::atomic::AtomicI64::new(1);
pub(crate) fn alloc_cache_id(initial: HashMap<String, Value>) -> i64 {
let id = NEXT_CACHE_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
CACHE_STORE.lock().unwrap().insert(id, initial);
id
}
fn cache_id_of(v: &Value) -> Option<i64> {
match v {
Value::Dict(d) => match d.get("__cache_id") { Some(Value::Int(n)) => Some(*n), _ => None },
_ => None,
}
}
fn cache_id_of_m(m: &HashMap<String, Value>) -> Option<i64> {
match m.get("__cache_id") { Some(Value::Int(n)) => Some(*n), _ => None }
}
fn with_cache_cell<R>(id: i64, f: impl FnOnce(&mut HashMap<String, Value>) -> R) -> R {
let mut g = CACHE_STORE.lock().unwrap();
let cell = g.entry(id).or_insert_with(HashMap::new);
f(cell)
}
fn cache_data_snapshot(m: &HashMap<String, Value>) -> Vec<Value> {
if let Some(id) = cache_id_of_m(m) {
return with_cache_cell(id, |c| match c.get("_data") {
Some(Value::Arr(a)) => (**a).clone(),
_ => Vec::new(),
});
}
match m.get("_data") { Some(Value::Arr(a)) => (**a).clone(), _ => Vec::new() }
}
fn cache_len_of(m: &HashMap<String, Value>) -> usize {
if let Some(id) = cache_id_of_m(m) {
return with_cache_cell(id, |c| match c.get("_data") { Some(Value::Arr(a)) => a.len(), _ => 0 });
}
match m.get("_data") { Some(Value::Arr(a)) => a.len(), _ => 0 }
}
fn cache_at(m: &HashMap<String, Value>, i: usize) -> Value {
if let Some(id) = cache_id_of_m(m) {
return with_cache_cell(id, |c| match c.get("_data") {
Some(Value::Arr(a)) => a.get(i).cloned().unwrap_or(Value::Null),
_ => Value::Null,
});
}
match m.get("_data") { Some(Value::Arr(a)) => a.get(i).cloned().unwrap_or(Value::Null), _ => Value::Null }
}
pub(crate) fn cache_len_of_marker(m: &HashMap<String, Value>) -> Option<usize> {
if m.contains_key("__cacheKind") { Some(cache_len_of(m)) } else { None }
}
pub(crate) fn cache_append(target: &mut Value, item: Value) {
let kind = cache_kind(target).unwrap_or_default();
let cap = cache_max_size(target);
if let Some(id) = cache_id_of(target) {
with_cache_cell(id, |m| cache_append_inner(m, &kind, cap, item));
} else if let Value::Dict(m_arc) = target {
let m = Arc::make_mut(m_arc);
cache_append_inner(m, &kind, cap, item);
}
}
fn cache_append_inner(m: &mut HashMap<String, Value>, kind: &str, cap: Option<usize>, item: Value) {
{
match kind {
"ArrayCache" => {
let data = cache_data_mut(m);
if let Some(cap) = cap { if data.len() == cap { data.remove(0); } }
data.push(item.clone());
cache_apply_clear_all(m);
let sym = cache_str_field(&item, "symbol").unwrap_or_default();
if cache_take_symbol_clear(m, &sym) {
cache_dict_field_mut(m, "_newUpdatesBySymbol").insert(sym.clone(), Value::Int(0));
}
{
let nubs = cache_dict_field_mut(m, "_newUpdatesBySymbol");
let cur = match nubs.get(&sym) { Some(Value::Int(n)) => *n, _ => 0 };
nubs.insert(sym, Value::Int(cur + 1));
}
let all = cache_int_field(m, "_allNewUpdates");
cache_set_int(m, "_allNewUpdates", all + 1);
}
"ArrayCacheByTimestamp" => {
let ts_key = match item {
Value::Arr(ref a) => match a.first() {
Some(Value::Int(n)) => n.to_string(),
Some(Value::Float(f)) => f.to_string(),
Some(Value::Str(s)) => s.clone(),
_ => String::new(),
},
_ => String::new(),
};
let existing_idx = {
let hm = cache_dict_field_mut(m, "hashmap");
match hm.get(&ts_key) { Some(Value::Int(n)) => Some(*n as usize), _ => None }
};
if let Some(idx) = existing_idx {
let data = cache_data_mut(m);
if idx < data.len() && data[idx] != item { data[idx] = item.clone(); }
} else {
let evicting = match cap { Some(c) => {
let data_len = cache_data_mut(m).len();
data_len == c
}, None => false };
if evicting {
let removed = {
let data = cache_data_mut(m);
if data.is_empty() { Value::Null } else { data.remove(0) }
};
let removed_key = match &removed {
Value::Arr(a) => match a.first() {
Some(Value::Int(n)) => n.to_string(),
Some(Value::Float(f)) => f.to_string(),
Some(Value::Str(s)) => s.clone(),
_ => String::new(),
},
_ => String::new(),
};
let hm = cache_dict_field_mut(m, "hashmap");
hm.shift_remove(&removed_key);
for v in hm.values_mut() {
if let Value::Int(n) = v { *n = n.saturating_sub(1); }
}
}
let new_idx = {
let data = cache_data_mut(m);
let i = data.len();
data.push(item);
i
};
let hm = cache_dict_field_mut(m, "hashmap");
hm.insert(ts_key.clone(), Value::Int(new_idx as i64));
}
if cache_bool_field(m, "_clearUpdates") {
cache_set_bool(m, "_clearUpdates", false);
let st = cache_dict_field_mut(m, "_sizeTracker");
st.clear();
}
{
let st = cache_dict_field_mut(m, "_sizeTracker");
st.insert(ts_key, Value::Bool(true));
}
let st_len = match m.get("_sizeTracker") {
Some(Value::Dict(d)) => d.len() as i64,
_ => 0,
};
cache_set_int(m, "_newUpdates", st_len);
}
"ArrayCacheBySymbolById" | "ArrayCacheBySymbolBySide" | "ArrayCacheByOutcomeById" => {
let key1_name = if kind == "ArrayCacheByOutcomeById" { "outcome" } else { "symbol" };
let symbol = cache_str_field(&item, key1_name).unwrap_or_default();
let key2_name = if kind == "ArrayCacheBySymbolBySide" { "side" } else { "id" };
let key2 = cache_str_field(&item, key2_name).unwrap_or_default();
let was_duplicate = {
let hm = cache_dict_field_mut(m, "hashmap");
let bucket = hm.entry(symbol.clone())
.or_insert_with(|| Value::Map(HashMap::new()));
if let Value::Dict(bd) = bucket {
Arc::make_mut(bd);
}
if let Some(Value::Dict(bd)) = hm.get(&symbol) {
bd.contains_key(&key2)
} else { false }
};
let item_to_store = if was_duplicate {
let merged = {
let hm = cache_dict_field_mut(m, "hashmap");
let bucket = hm.get(&symbol).cloned();
let existing = match bucket {
Some(Value::Dict(bd)) => bd.get(&key2).cloned(),
_ => None,
};
match (existing, item.clone()) {
(Some(Value::Dict(old)), Value::Dict(new_)) => {
let mut merged = (*old).clone();
for (k, v) in new_.iter() { merged.insert(k.clone(), v.clone()); }
Value::Dict(Arc::new(merged))
}
(_, new_) => new_,
}
};
{
let hm = cache_dict_field_mut(m, "hashmap");
let bucket = hm.entry(symbol.clone())
.or_insert_with(|| Value::Map(HashMap::new()));
if let Value::Dict(bd) = bucket {
Arc::make_mut(bd).insert(key2.clone(), merged.clone());
}
}
{
let data = cache_data_mut(m);
if let Some(pos) = data.iter().position(|x| {
cache_str_field(x, key2_name).as_deref() == Some(&key2)
&& cache_str_field(x, key1_name).as_deref() == Some(&symbol)
}) { data.remove(pos); }
}
merged
} else {
let hm = cache_dict_field_mut(m, "hashmap");
let bucket = hm.entry(symbol.clone())
.or_insert_with(|| Value::Map(HashMap::new()));
if let Value::Dict(bd) = bucket {
Arc::make_mut(bd).insert(key2.clone(), item.clone());
}
item.clone()
};
let evicting = match cap { Some(c) => {
cache_data_mut(m).len() == c
}, None => false };
if evicting {
let removed = {
let data = cache_data_mut(m);
if data.is_empty() { Value::Null } else { data.remove(0) }
};
let r_sym = cache_str_field(&removed, key1_name).unwrap_or_default();
let r_key2 = cache_str_field(&removed, key2_name).unwrap_or_default();
{
let hm = cache_dict_field_mut(m, "hashmap");
let now_empty = if let Some(Value::Dict(bd)) = hm.get_mut(&r_sym) {
let bd = Arc::make_mut(bd);
bd.shift_remove(&r_key2);
bd.is_empty()
} else { false };
if now_empty { hm.shift_remove(&r_sym); }
}
if cache_seen_remove(m, "_seenUpdatesBySymbol", &r_sym, &r_key2) {
let nubs = cache_dict_field_mut(m, "_newUpdatesBySymbol");
if let Some(Value::Int(n)) = nubs.get(&r_sym).cloned() {
nubs.insert(r_sym.clone(), Value::Int(n - 1));
}
}
if cache_seen_remove(m, "_seenUpdatesAll", &r_sym, &r_key2) {
let all = cache_int_field(m, "_allNewUpdates");
cache_set_int(m, "_allNewUpdates", all - 1);
}
}
cache_data_mut(m).push(item_to_store);
cache_apply_clear_all(m);
if cache_take_symbol_clear(m, &symbol) {
cache_seen_clear(m, "_seenUpdatesBySymbol", &symbol);
}
let sz = cache_seen_add(m, "_seenUpdatesBySymbol", &symbol, &key2);
cache_dict_field_mut(m, "_newUpdatesBySymbol").insert(symbol.clone(), Value::Int(sz));
let before = cache_seen_size(m, "_seenUpdatesAll", &symbol);
let after = cache_seen_add(m, "_seenUpdatesAll", &symbol, &key2);
let all = cache_int_field(m, "_allNewUpdates");
cache_set_int(m, "_allNewUpdates", all + (after - before));
}
_ => {}
}
}
}
pub(crate) fn cache_get_limit(target: &mut Value, symbol: Value, limit: Value) -> Value {
let kind = cache_kind(target).unwrap_or_default();
if let Some(id) = cache_id_of(target) {
return with_cache_cell(id, |m| cache_get_limit_inner(m, &kind, symbol, limit));
}
if let Value::Dict(m_arc) = target {
let m = Arc::make_mut(m_arc);
cache_get_limit_inner(m, &kind, symbol, limit)
} else {
limit
}
}
fn cache_get_limit_inner(m: &mut HashMap<String, Value>, kind: &str, symbol: Value, limit: Value) -> Value {
{
if kind == "ArrayCacheByTimestamp" {
let v = cache_int_field(m, "_newUpdates");
cache_set_bool(m, "_clearUpdates", true);
return match limit {
Value::Int(l) => Value::Int(v.min(l)),
_ => Value::Int(v),
};
}
let new_updates_value: Option<i64> = match &symbol {
Value::Null => {
cache_set_bool(m, "_clearAllUpdates", true);
Some(cache_int_field(m, "_allNewUpdates"))
}
Value::Str(sym) => {
let v = match cache_dict_field_mut(m, "_newUpdatesBySymbol").get(sym) {
Some(Value::Int(n)) => Some(*n),
_ => None,
};
cache_dict_field_mut(m, "_clearUpdatesBySymbol")
.insert(sym.clone(), Value::Bool(true));
v
}
_ => None,
};
match (new_updates_value, &limit) {
(None, _) => limit.clone(),
(Some(v), Value::Int(l)) => Value::Int(v.min(*l)),
(Some(v), _) => Value::Int(v),
}
}
}
fn as_f64(v: &Value) -> f64 {
match v {
Value::Float(f) => *f,
Value::Int(n) => *n as f64,
Value::Str(s) => s.parse().unwrap_or(0.0),
_ => 0.0,
}
}
fn book_kind(v: &Value) -> Option<String> {
match v { Value::Dict(d) => match d.get("__bookKind") {
Some(Value::Str(s)) => Some(s.clone()), _ => None,
}, _ => None }
}
static BOOK_META: once_cell::sync::Lazy<std::sync::Mutex<std::collections::HashMap<i64, HashMap<String, Value>>>> =
once_cell::sync::Lazy::new(|| std::sync::Mutex::new(std::collections::HashMap::new()));
static NEXT_BOOK_ID: std::sync::atomic::AtomicI64 = std::sync::atomic::AtomicI64::new(1);
fn is_book_meta_key(k: &str) -> bool {
matches!(k, "timestamp" | "datetime" | "nonce" | "symbol" | "checksum")
}
pub(crate) fn alloc_book_id() -> i64 {
let id = NEXT_BOOK_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
BOOK_META.lock().unwrap().insert(id, HashMap::new());
id
}
fn book_id_of(m: &HashMap<String, Value>) -> Option<i64> {
match m.get("__book_id") { Some(Value::Int(n)) => Some(*n), _ => None }
}
fn book_meta_get(id: i64, k: &str) -> Value {
BOOK_META.lock().unwrap().get(&id).and_then(|m| m.get(k).cloned()).unwrap_or(Value::Null)
}
pub(crate) fn book_meta_set(id: i64, k: &str, v: Value) {
let mut g = BOOK_META.lock().unwrap();
g.entry(id).or_default().insert(k.to_string(), v);
}
fn book_meta_snapshot(id: i64) -> Vec<(String, Value)> {
BOOK_META.lock().unwrap().get(&id)
.map(|m| m.iter().map(|(k, v)| (k.clone(), v.clone())).collect())
.unwrap_or_default()
}
static BOOK_CACHE: once_cell::sync::Lazy<std::sync::Mutex<std::collections::HashMap<i64, Vec<Value>>>> =
once_cell::sync::Lazy::new(|| std::sync::Mutex::new(std::collections::HashMap::new()));
fn book_cache_handle(id: i64) -> Value {
let mut h = HashMap::new();
h.insert("__book_cache_id".to_string(), Value::Int(id));
Value::Dict(Arc::new(h))
}
fn book_cache_id_of(m: &HashMap<String, Value>) -> Option<i64> {
match m.get("__book_cache_id") { Some(Value::Int(n)) => Some(*n), _ => None }
}
pub(crate) fn book_cache_push(id: i64, v: Value) {
BOOK_CACHE.lock().unwrap().entry(id).or_default().push(v);
}
fn book_cache_len(id: i64) -> usize {
BOOK_CACHE.lock().unwrap().get(&id).map(|c| c.len()).unwrap_or(0)
}
pub(crate) fn book_cache_len_of(m: &HashMap<String, Value>) -> Option<usize> {
book_cache_id_of(m).map(book_cache_len)
}
pub(crate) fn try_ws_subs_write(m: &HashMap<String, Value>, key: &str, val: &Value) -> bool {
if key == "__ws_subs_url" { return false; }
if let Some(Value::Str(url)) = m.get("__ws_subs_url") {
crate::pro::ws_client::value_subs_insert(url, key, val.clone());
return true;
}
false
}
pub(crate) fn try_ws_sub_field_write(m: &HashMap<String, Value>, key: &str, val: &Value) -> bool {
if key == "__ws_sub_ref" { return false; }
if let Some(Value::Str(subref)) = m.get("__ws_sub_ref") {
crate::pro::ws_client::value_sub_field_write(subref, key, val.clone());
return true;
}
false
}
pub(crate) fn try_ws_subs_remove(m: &HashMap<String, Value>, key: &str) -> bool {
if let Some(Value::Str(url)) = m.get("__ws_subs_url") {
crate::pro::ws_client::value_subs_remove(url, key);
return true;
}
false
}
pub(crate) fn try_book_cache_push(obj: &Value, v: &Value) -> bool {
if let Value::Dict(m) = obj {
if let Some(id) = book_cache_id_of(m) { book_cache_push(id, v.clone()); return true; }
}
false
}
fn book_cache_at(id: i64, i: usize) -> Value {
BOOK_CACHE.lock().unwrap().get(&id).and_then(|c| c.get(i).cloned()).unwrap_or(Value::Null)
}
pub(crate) fn book_cache_set(id: i64, items: Vec<Value>) {
BOOK_CACHE.lock().unwrap().insert(id, items);
}
pub fn cache_entries_as_value(v: &Value) -> Option<Value> {
if let Value::Dict(m) = v {
if let Some(id) = book_cache_id_of(m) {
let items = BOOK_CACHE.lock().unwrap().get(&id).cloned().unwrap_or_default();
return Some(Value::Array(items));
}
if m.contains_key("__cacheKind") {
return Some(Value::Array(cache_data_snapshot(m)));
}
}
None
}
pub(crate) fn try_book_meta_write(m: &HashMap<String, Value>, k: &str, v: &Value) -> bool {
if is_book_meta_key(k) {
if let Some(id) = book_id_of(m) { book_meta_set(id, k, v.clone()); return true; }
}
if k == "cache" {
if let Some(id) = book_id_of(m) {
let items = match v { Value::Arr(a) => a.as_ref().clone(), _ => Vec::new() };
book_cache_set(id, items);
return true;
}
}
false
}
pub fn book_fields_as_value(v: &Value) -> Option<Value> {
match v {
Value::Dict(d) if d.contains_key("__book_id") => {
let mut out: HashMap<String, Value> = (**d).clone();
out.shift_remove("__book_id");
if let Some(Value::Int(id)) = d.get("__book_id") {
if let Some(meta) = BOOK_META.lock().unwrap().get(id) {
for (k, val) in meta.iter() { out.insert(k.clone(), val.clone()); }
}
}
Some(Value::Dict(Arc::new(out)))
}
_ => None,
}
}
struct Level {
price: f64, entry: Value, }
struct SideCell {
entries: Vec<Level>,
hashmap: HashMap<String, Value>,
}
static SIDE_STORE: once_cell::sync::Lazy<std::sync::Mutex<std::collections::HashMap<i64, SideCell>>> =
once_cell::sync::Lazy::new(|| std::sync::Mutex::new(std::collections::HashMap::new()));
static NEXT_SIDE_ID: std::sync::atomic::AtomicI64 = std::sync::atomic::AtomicI64::new(1);
fn alloc_side_id() -> i64 {
let id = NEXT_SIDE_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
SIDE_STORE.lock().unwrap().insert(id, SideCell { entries: Vec::new(), hashmap: HashMap::new() });
id
}
fn clear_side_cell(id: i64) {
let mut g = SIDE_STORE.lock().unwrap();
let cell = g.entry(id).or_insert_with(|| SideCell { entries: Vec::new(), hashmap: HashMap::new() });
cell.entries.clear();
cell.hashmap.clear();
}
fn side_id_of(m: &HashMap<String, Value>) -> Option<i64> {
match m.get("__side_id") { Some(Value::Int(n)) => Some(*n), _ => None }
}
fn side_entries_view(m: &HashMap<String, Value>) -> Vec<Value> {
match side_id_of(m) {
Some(id) => SIDE_STORE.lock().unwrap().get(&id)
.map(|c| c.entries.iter().map(|l| l.entry.clone()).collect()).unwrap_or_default(),
None => match m.get("_entries") { Some(Value::Arr(a)) => a.as_ref().clone(), _ => Vec::new() },
}
}
fn side_entry_at(m: &HashMap<String, Value>, i: usize) -> Value {
match side_id_of(m) {
Some(id) => SIDE_STORE.lock().unwrap().get(&id)
.and_then(|c| c.entries.get(i).map(|l| l.entry.clone())).unwrap_or(Value::Null),
None => match m.get("_entries") {
Some(Value::Arr(a)) => a.get(i).cloned().unwrap_or(Value::Null), _ => Value::Null },
}
}
pub(crate) fn side_entries_len(m: &HashMap<String, Value>) -> usize {
match side_id_of(m) {
Some(id) => SIDE_STORE.lock().unwrap().get(&id).map(|c| c.entries.len()).unwrap_or(0),
None => match m.get("_entries") { Some(Value::Arr(a)) => a.len(), _ => 0 },
}
}
fn with_side_cell<R>(m: &HashMap<String, Value>, f: impl FnOnce(&mut SideCell) -> R) -> Option<R> {
let id = side_id_of(m)?;
let mut g = SIDE_STORE.lock().unwrap();
let cell = g.entry(id).or_insert_with(|| SideCell { entries: Vec::new(), hashmap: HashMap::new() });
Some(f(cell))
}
pub fn side_entries_as_value(v: &Value) -> Option<Value> {
match v {
Value::Dict(d) if d.contains_key("__sideKind") => Some(Value::List(side_entries_view(d))),
_ => None,
}
}
fn row_price_amount(row: &Value) -> Option<[f64; 2]> {
let r = match row { Value::Arr(a) => a, _ => return None };
let num = |cell: Option<&Value>| -> f64 {
match cell {
Some(Value::Float(f)) => *f,
Some(Value::Int(n)) => *n as f64,
Some(Value::Str(s)) => s.parse().unwrap_or(f64::NAN),
_ => f64::NAN,
}
};
let p = num(r.first());
let a = num(r.get(1));
if p.is_nan() || a.is_nan() { None } else { Some([p, a]) }
}
pub fn side_price_amounts(v: &Value) -> Option<Vec<[f64; 2]>> {
let d = match v {
Value::Dict(d) if d.contains_key("__sideKind") => d,
_ => return None,
};
let out = match side_id_of(d) {
Some(id) => match SIDE_STORE.lock().unwrap().get(&id) {
Some(cell) => cell.entries.iter().filter_map(|l| row_price_amount(&l.entry)).collect(),
None => Vec::new(),
},
None => match d.get("_entries") {
Some(Value::Arr(a)) => a.iter().filter_map(row_price_amount).collect(),
_ => Vec::new(),
},
};
Some(out)
}
pub(crate) fn make_side_marker(side_kind: &str, is_bid: bool, depth: i64) -> Value {
let sid = alloc_side_id();
let mut m = HashMap::new();
m.insert("__sideKind".to_string(), Value::Str(side_kind.to_string()));
m.insert("_isBid".to_string(), Value::Bool(is_bid));
m.insert("_depth".to_string(), Value::Int(depth));
m.insert("__side_id".to_string(), Value::Int(sid));
Value::Dict(Arc::new(m))
}
fn side_kind(v: &Value) -> Option<String> {
match v { Value::Dict(d) => match d.get("__sideKind") {
Some(Value::Str(s)) => Some(s.clone()), _ => None,
}, _ => None }
}
fn side_is_bid(m: &HashMap<String, Value>) -> bool {
matches!(m.get("_isBid"), Some(Value::Bool(true)))
}
fn side_depth(m: &HashMap<String, Value>) -> usize {
match m.get("_depth") {
Some(Value::Int(n)) if *n > 0 => *n as usize,
_ => usize::MAX / 2,
}
}
fn level_price(entries: &[Level]) -> impl Fn(usize) -> f64 + '_ {
move |i: usize| entries[i].price
}
fn bisect_left_by(len: usize, target: f64, is_bid: bool, get_price: impl Fn(usize) -> f64) -> usize {
let mut lo = 0usize;
let mut hi = len;
while lo < hi {
let mid = (lo + hi) / 2;
let p = get_price(mid);
let ip = if is_bid { -p } else { p };
if ip < target { lo = mid + 1; } else { hi = mid; }
}
lo
}
fn delta_field<T>(d: &Value, idx: usize, parse: impl Fn(&Value) -> T, default: T) -> T {
match d {
Value::Arr(a) => match a.get(idx) { Some(v) => parse(v), None => default },
_ => default,
}
}
fn entry_id(entries: &[Level], i: usize) -> String {
if let Value::Arr(a) = &entries[i].entry {
if let Some(v) = a.get(2) {
return match v {
Value::Str(s) => s.clone(),
Value::Int(n) => n.to_string(),
_ => String::new(),
};
}
}
String::new()
}
fn side_store_array(side: &Value, delta: Value) {
let kind = side_kind(side).unwrap_or_default();
let m = match side { Value::Dict(d) => d.as_ref(), _ => return };
let is_bid = side_is_bid(m);
with_side_cell(m, |cell| {
match kind.as_str() {
"OrderBookSide" => {
let price = delta_field(&delta, 0, as_f64, 0.0);
let size = delta_field(&delta, 1, as_f64, 0.0);
let target = if is_bid { -price } else { price };
let entries = &mut cell.entries;
let idx = bisect_left_by(entries.len(), target, is_bid, level_price(entries));
let matches_here = idx < entries.len()
&& (if is_bid { -entries[idx].price } else { entries[idx].price }) == target;
if size != 0.0 {
if matches_here {
entries[idx] = Level { price, entry: delta };
} else {
entries.insert(idx, Level { price, entry: delta });
}
} else if matches_here {
entries.remove(idx);
}
}
"CountedOrderBookSide" => {
let price = delta_field(&delta, 0, as_f64, 0.0);
let size = delta_field(&delta, 1, as_f64, 0.0);
let count = delta_field(&delta, 2, as_f64, 0.0);
let has_count = matches!(&delta,
Value::Arr(a) if matches!(a.get(2), Some(Value::Int(_)) | Some(Value::Float(_))));
let store_it = if has_count { size != 0.0 && count != 0.0 } else { size != 0.0 };
let target = if is_bid { -price } else { price };
let entries = &mut cell.entries;
let idx = bisect_left_by(entries.len(), target, is_bid, level_price(entries));
let matches_here = idx < entries.len()
&& (if is_bid { -entries[idx].price } else { entries[idx].price }) == target;
if store_it {
if matches_here {
entries[idx] = Level { price, entry: delta };
} else {
entries.insert(idx, Level { price, entry: delta });
}
} else if matches_here {
entries.remove(idx);
}
}
"IndexedOrderBookSide" => {
let price_opt = match &delta {
Value::Arr(a) => a.first().and_then(|v| match v {
Value::Float(f) => Some(*f),
Value::Int(n) => Some(*n as f64),
_ => None,
}),
_ => None,
};
let size = delta_field(&delta, 1, as_f64, 0.0);
let id = match &delta {
Value::Arr(a) => match a.get(2) {
Some(Value::Str(s)) => s.clone(),
Some(Value::Int(n)) => n.to_string(),
_ => String::new(),
},
_ => String::new(),
};
let mut index_price = price_opt.map(|p| if is_bid { -p } else { p });
let old_price_opt = {
match cell.hashmap.get(&id) {
Some(Value::Float(f)) => Some(*f),
Some(Value::Int(n)) => Some(*n as f64),
_ => None,
}
};
if size != 0.0 {
if let Some(old_price) = old_price_opt {
index_price = index_price.or(Some(old_price));
if Some(old_price) == index_price {
let target = old_price;
let raw = if is_bid { -old_price } else { old_price };
let entries = &mut cell.entries;
let mut idx = bisect_left_by(entries.len(), target, is_bid, level_price(entries));
while idx < entries.len() && entry_id(entries, idx) != id { idx += 1; }
if idx < entries.len() { entries[idx] = Level { price: raw, entry: delta }; }
return;
}
let target = old_price;
let entries = &mut cell.entries;
let mut old_idx = bisect_left_by(entries.len(), target, is_bid, level_price(entries));
while old_idx < entries.len() && entry_id(entries, old_idx) != id { old_idx += 1; }
if old_idx < entries.len() { entries.remove(old_idx); }
}
let target = match index_price { Some(p) => p, None => return };
let raw = if is_bid { -target } else { target };
let entries = &mut cell.entries;
let mut idx = bisect_left_by(entries.len(), target, is_bid, level_price(entries));
while idx < entries.len() {
let ip = if is_bid { -entries[idx].price } else { entries[idx].price };
if ip != target { break; }
if entry_id(entries, idx) >= id { break; }
idx += 1;
}
entries.insert(idx, Level { price: raw, entry: delta });
cell.hashmap.insert(id, Value::Float(target));
} else if let Some(old_price) = old_price_opt {
let target = old_price;
let entries = &mut cell.entries;
let mut idx = bisect_left_by(entries.len(), target, is_bid, level_price(entries));
while idx < entries.len() && entry_id(entries, idx) != id { idx += 1; }
if idx < entries.len() { entries.remove(idx); }
cell.hashmap.shift_remove(&id);
}
}
_ => {}
}
});
}
fn side_limit(side: &Value) {
let m = match side { Value::Dict(d) => d.as_ref(), _ => return };
let depth = side_depth(m);
with_side_cell(m, |cell| {
if cell.entries.len() > depth { cell.entries.truncate(depth); }
});
}
fn book_reseed_sides(book: &mut Value, snapshot: &Value) {
let kind = book_kind(book).unwrap_or_default();
let side_kind_str = match kind.as_str() {
"IndexedOrderBook" => "IndexedOrderBookSide",
"CountedOrderBook" => "CountedOrderBookSide",
_ => "OrderBookSide",
};
let bids_deltas = crate::get_value(snapshot, &Value::Str("bids".to_string()));
let asks_deltas = crate::get_value(snapshot, &Value::Str("asks".to_string()));
if let Value::Dict(book_arc) = book {
let book_m = Arc::make_mut(book_arc);
let depth = match book_m.get("_depth") {
Some(Value::Int(n)) => *n,
_ => i64::MAX / 2,
};
for (side_key, deltas, is_bid) in [
("bids", &bids_deltas, true),
("asks", &asks_deltas, false),
] {
let sid = match book_m.get(side_key) {
Some(Value::Dict(d)) => match d.get("__side_id") {
Some(Value::Int(n)) => { clear_side_cell(*n); *n }
_ => alloc_side_id(),
},
_ => alloc_side_id(),
};
let mut m = HashMap::new();
m.insert("__sideKind".to_string(), Value::Str(side_kind_str.to_string()));
m.insert("_isBid".to_string(), Value::Bool(is_bid));
m.insert("_depth".to_string(), Value::Int(depth));
m.insert("__side_id".to_string(), Value::Int(sid));
let side = Value::Map(m);
if let Value::Arr(rows) = deltas {
for row in rows.iter() {
if let Value::Arr(_) = row { side_store_array(&side, row.clone()); }
}
}
book_m.insert(side_key.to_string(), side);
}
}
}
pub(crate) fn book_limit(book: &mut Value) {
if book_kind(book).is_none() { return; }
if let Value::Dict(arc) = book {
let m = Arc::make_mut(arc);
if let Some(side) = m.get_mut("bids") { side_limit(side); }
if let Some(side) = m.get_mut("asks") { side_limit(side); }
}
}
pub(crate) fn book_reset(book: &mut Value, snapshot: Value) {
if book_kind(book).is_none() { return; }
book_reseed_sides(book, &snapshot);
let id = match book { Value::Dict(d) => book_id_of(d), _ => None };
if let Some(id) = id {
let ts = match crate::get_value(&snapshot, &Value::Str("timestamp".to_string())) {
Value::Int(n) => Some(n),
Value::Float(f) => Some(f as i64),
_ => None,
};
let dt = ts.and_then(|n| chrono::DateTime::<chrono::Utc>::from_timestamp_millis(n)
.map(|t| t.to_rfc3339_opts(chrono::SecondsFormat::Millis, true)));
book_meta_set(id, "timestamp", ts.map(Value::Int).unwrap_or(Value::Null));
book_meta_set(id, "datetime", dt.map(Value::Str).unwrap_or(Value::Null));
book_meta_set(id, "nonce", crate::get_value(&snapshot, &Value::Str("nonce".to_string())));
book_meta_set(id, "symbol", crate::get_value(&snapshot, &Value::Str("symbol".to_string())));
}
}
pub(crate) fn book_update(book: &mut Value, snapshot: Value) {
if book_kind(book).is_none() { return; }
let new_nonce = match crate::get_value(&snapshot, &Value::Str("nonce".to_string())) {
Value::Int(n) => Some(n), _ => None,
};
let cur_nonce = match book {
Value::Dict(d) => match book_id_of(d).map(|id| book_meta_get(id, "nonce")) {
Some(Value::Int(n)) => Some(n), _ => None,
},
_ => None,
};
if let (Some(n), Some(cur)) = (new_nonce, cur_nonce) {
if n <= cur { return; }
}
book_reset(book, snapshot);
}
pub(crate) fn book_store_array_side(book: &mut Value, side_key: &str, delta: Value) {
if let Value::Dict(arc) = book {
let m = Arc::make_mut(arc);
if let Some(side) = m.get_mut(side_key) {
side_store_array(side, delta);
}
}
}
impl Value {
pub fn limit(&mut self) -> Value {
book_limit(self);
self.clone()
}
pub fn reset(&mut self, snapshot: Value) -> Value {
if matches!(&self, Value::Dict(d) if d.contains_key("url") && d.contains_key("subscriptions")) {
return crate::pro::ws_client::value_reset(self);
}
book_reset(self, snapshot);
self.clone()
}
pub fn reset0(&mut self) -> Value {
book_reset(self, Value::Null);
self.clone()
}
pub fn update(&mut self, snapshot: Value) -> Value {
book_update(self, snapshot);
self.clone()
}
pub fn store(&mut self, price: Value, size: Value) {
if side_kind(self).is_some() {
let delta = Value::List(vec![price, size]);
side_store_array(self, delta);
}
}
pub fn store_to_bids(&mut self, price: Value, size: Value) {
let delta = Value::List(vec![price, size]);
book_store_array_side(self, "bids", delta);
}
pub fn store_to_asks(&mut self, price: Value, size: Value) {
let delta = Value::List(vec![price, size]);
book_store_array_side(self, "asks", delta);
}
pub fn store_array_to_bids(&mut self, delta: Value) {
book_store_array_side(self, "bids", delta);
}
pub fn store_array_to_asks(&mut self, delta: Value) {
book_store_array_side(self, "asks", delta);
}
}
#[cfg(test)]
mod json_int_precision_tests {
use super::Value;
#[test]
fn preserves_large_u64_ids_losslessly() {
let v: serde_json::Value = serde_json::from_str(r#"{"id": 12345678901234567890}"#).unwrap();
let parsed = Value::from_json(&v);
let id = crate::runtime::get_value(&parsed, &Value::Str("id".to_string()));
assert_eq!(id, Value::Str("12345678901234567890".to_string()));
}
#[test]
fn small_ints_stay_ints_floats_stay_floats() {
let v: serde_json::Value = serde_json::from_str(r#"{"a": 42, "b": -7, "c": 1.5}"#).unwrap();
let p = Value::from_json(&v);
assert_eq!(crate::runtime::get_value(&p, &Value::Str("a".to_string())), Value::Int(42));
assert_eq!(crate::runtime::get_value(&p, &Value::Str("b".to_string())), Value::Int(-7));
assert_eq!(crate::runtime::get_value(&p, &Value::Str("c".to_string())), Value::Float(1.5));
}
}