#![allow(clippy::float_cmp, clippy::needless_return, dead_code)]
use crate::Value;
use indexmap::IndexMap as HashMap;
use std::sync::Arc;
fn as_f64(v: &Value) -> Option<f64> {
match v {
Value::Int(n) => Some(*n as f64),
Value::Float(f) => Some(*f),
Value::Str(s) => s.parse().ok(),
Value::Bool(b) => Some(if *b { 1.0 } else { 0.0 }),
_ => None,
}
}
fn as_i64(v: &Value) -> Option<i64> {
match v {
Value::Int(n) => Some(*n),
Value::Float(f) => Some(*f as i64),
Value::Str(s) => s.parse().ok(),
Value::Bool(b) => Some(if *b { 1 } else { 0 }),
_ => None,
}
}
fn both_ints(a: &Value, b: &Value) -> bool {
matches!(a, Value::Int(_)) && matches!(b, Value::Int(_))
}
pub fn add(a: &Value, b: &Value) -> Value {
if let (Value::Str(sa), Value::Str(sb)) = (a, b) {
return Value::Str(format!("{sa}{sb}"));
}
if let Value::Str(_) = a { if let Some(_) = as_f64(b) {} return string_concat(a, b); }
if let Value::Str(_) = b { return string_concat(a, b); }
if both_ints(a, b) {
return Value::Int(as_i64(a).unwrap() + as_i64(b).unwrap());
}
match (as_f64(a), as_f64(b)) {
(Some(x), Some(y)) => Value::Float(x + y),
_ => Value::Null,
}
}
fn string_concat(a: &Value, b: &Value) -> Value {
let sa = stringify_simple(a);
let sb = stringify_simple(b);
Value::Str(format!("{sa}{sb}"))
}
pub fn stringify_param(v: &Value) -> String { stringify_simple(v) }
fn stringify_simple(v: &Value) -> String {
match v {
Value::Str(s) => s.clone(),
Value::Int(n) => n.to_string(),
Value::Float(f) => f.to_string(),
Value::Bool(b) => b.to_string(),
Value::Null => "null".to_string(),
Value::Dict(m) if m.contains_key("__precise") => precise_to_string(m),
_ => format!("{v}"),
}
}
fn precise_to_string(m: &indexmap::IndexMap<String, Value>) -> String {
let decimals: i64 = match m.get("decimals") { Some(Value::Int(n)) => *n, _ => 0 };
let integer: String = match m.get("integer") {
Some(Value::Str(s)) => s.clone(),
_ => return "0".to_string(),
};
let (sign, mut digits) = if let Some(rest) = integer.strip_prefix('-') {
("-".to_string(), rest.to_string())
} else { ("".to_string(), integer) };
if decimals <= 0 {
for _ in 0..(-decimals) { digits.push('0'); }
return format!("{sign}{digits}");
}
let d = decimals as usize;
if digits.len() <= d {
let zeros = "0".repeat(d - digits.len());
format!("{sign}0.{zeros}{digits}")
} else {
let split = digits.len() - d;
let (int_part, frac_part) = digits.split_at(split);
format!("{sign}{int_part}.{frac_part}")
}
}
pub fn subtract(a: &Value, b: &Value) -> Value {
if both_ints(a, b) { return Value::Int(as_i64(a).unwrap() - as_i64(b).unwrap()); }
match (as_f64(a), as_f64(b)) {
(Some(x), Some(y)) => Value::Float(x - y),
_ => Value::Null,
}
}
pub fn multiply(a: &Value, b: &Value) -> Value {
if both_ints(a, b) { return Value::Int(as_i64(a).unwrap() * as_i64(b).unwrap()); }
match (as_f64(a), as_f64(b)) {
(Some(x), Some(y)) => Value::Float(x * y),
_ => Value::Null,
}
}
pub fn divide(a: &Value, b: &Value) -> Value {
match (as_f64(a), as_f64(b)) {
(Some(_), Some(y)) if y == 0.0 => Value::Null,
(Some(x), Some(y)) => Value::Float(x / y),
_ => Value::Null,
}
}
pub fn mod_val(a: &Value, b: &Value) -> Value {
if both_ints(a, b) {
let yi = as_i64(b).unwrap();
if yi == 0 { return Value::Null; }
return Value::Int(as_i64(a).unwrap() % yi);
}
match (as_f64(a), as_f64(b)) {
(Some(_), Some(y)) if y == 0.0 => Value::Null,
(Some(x), Some(y)) => Value::Float(x % y),
_ => Value::Null,
}
}
pub fn negate(v: &Value) -> Value {
match v {
Value::Int(n) => Value::Int(-n),
Value::Float(f) => Value::Float(-f),
_ => match as_f64(v) {
Some(x) => Value::Float(-x),
None => Value::Null,
},
}
}
pub fn is_equal(a: &Value, b: &Value) -> bool {
match (a, b) {
(Value::Null, Value::Null) => true,
(Value::Bool(x), Value::Bool(y)) => x == y,
(Value::Str(x), Value::Str(y)) => x == y,
(Value::Int(x), Value::Int(y)) => x == y,
(Value::Float(x), Value::Float(y)) => x == y,
_ => match (as_f64(a), as_f64(b)) {
(Some(x), Some(y)) => x == y,
_ => false,
},
}
}
pub fn is_greater_than(a: &Value, b: &Value) -> bool {
match (as_f64(a), as_f64(b)) {
(Some(x), Some(y)) => x > y,
_ => match (a, b) { (Value::Str(x), Value::Str(y)) => x > y, _ => false },
}
}
pub fn is_less_than(a: &Value, b: &Value) -> bool {
match (as_f64(a), as_f64(b)) {
(Some(x), Some(y)) => x < y,
_ => match (a, b) { (Value::Str(x), Value::Str(y)) => x < y, _ => false },
}
}
pub fn is_greater_than_or_equal(a: &Value, b: &Value) -> bool {
is_greater_than(a, b) || is_equal(a, b)
}
pub fn is_less_than_or_equal(a: &Value, b: &Value) -> bool {
is_less_than(a, b) || is_equal(a, b)
}
pub trait IsTruthy { fn truthy(&self) -> bool; }
impl IsTruthy for Value { fn truthy(&self) -> bool { self.is_truthy() } }
impl IsTruthy for bool { fn truthy(&self) -> bool { *self } }
impl IsTruthy for &Value { fn truthy(&self) -> bool { (*self).is_truthy() } }
impl IsTruthy for &bool { fn truthy(&self) -> bool { **self } }
pub fn is_true<T: IsTruthy + ?Sized>(v: &T) -> bool { v.truthy() }
pub fn is_array(v: &Value) -> bool { matches!(v, Value::Arr(_)) }
pub fn is_object(v: &Value) -> bool { matches!(v, Value::Dict(_)) }
pub fn is_string(v: &Value) -> bool { matches!(v, Value::Str(_)) }
pub fn is_number(v: &Value) -> bool { matches!(v, Value::Int(_) | Value::Float(_)) }
pub fn is_bool(v: &Value) -> bool { matches!(v, Value::Bool(_)) }
pub fn is_integer(v: &Value) -> bool { matches!(v, Value::Int(_)) }
pub fn is_function(_v: &Value)-> bool { false }
pub fn panic_to_value(payload: Box<dyn std::any::Any + Send>) -> Value {
let msg = payload.downcast_ref::<String>().cloned()
.or_else(|| payload.downcast_ref::<&str>().map(|s| (*s).to_string()))
.unwrap_or_else(|| "panic".to_string());
Value::Str(msg)
}
pub fn panic_msg_to_error(msg: &str) -> crate::ExchangeError {
if let Some(start) = msg.find('[') {
let after = &msg[start + 1..];
if let Some(end) = after.find(']') {
let kind = &after[..end];
let rest = after[end + 1..].trim_start_matches(|c: char| c == ' ' || c == ':');
return crate::ExchangeError::new(kind, rest);
}
}
crate::ExchangeError::new("ExchangeError", msg)
}
pub fn catch_typed<F, T>(f: F) -> crate::Result<T>
where
F: FnOnce() -> T,
{
match std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)) {
Ok(v) => Ok(v),
Err(payload) => {
let msg = payload.downcast_ref::<String>().cloned()
.or_else(|| payload.downcast_ref::<&str>().map(|s| (*s).to_string()))
.unwrap_or_else(|| "panic".to_string());
Err(panic_msg_to_error(&msg))
}
}
}
pub async fn call_typed<F, T>(fut: F) -> crate::Result<T>
where
F: std::future::Future<Output = T>,
{
use futures::FutureExt;
use std::panic::AssertUnwindSafe;
match AssertUnwindSafe(fut).catch_unwind().await {
Ok(v) => Ok(v),
Err(payload) => {
let msg = payload.downcast_ref::<String>().cloned()
.or_else(|| payload.downcast_ref::<&str>().map(|s| (*s).to_string()))
.unwrap_or_else(|| "panic".to_string());
Err(panic_msg_to_error(&msg))
}
}
}
pub fn is_instance(value: &Value, class: &Value) -> bool {
let (msg, cls) = match (value, class) {
(Value::Str(s), Value::Str(c)) => (s, c),
_ => return false,
};
if msg.contains(&format!("[{cls}]")) {
return true;
}
if let Some(actual) = extract_error_kind(msg) {
let mut cur = error_parent(actual);
while let Some(c) = cur {
if c == cls.as_str() {
return true;
}
cur = error_parent(c);
}
}
false
}
fn extract_error_kind(msg: &str) -> Option<&str> {
let start = msg.find('[')? + 1;
let end = msg[start..].find(']')? + start;
Some(&msg[start..end])
}
fn error_parent(class: &str) -> Option<&'static str> {
Some(match class {
"ExchangeError" => "BaseError",
"AuthenticationError" => "ExchangeError",
"PermissionDenied" => "AuthenticationError",
"AccountNotEnabled" => "PermissionDenied",
"AccountSuspended" => "AuthenticationError",
"ArgumentsRequired" => "ExchangeError",
"BadRequest" => "ExchangeError",
"BadSymbol" => "BadRequest",
"OperationRejected" => "ExchangeError",
"NoChange" => "OperationRejected",
"MarginModeAlreadySet" => "NoChange",
"MarketClosed" => "OperationRejected",
"ManualInteractionNeeded" => "OperationRejected",
"RestrictedLocation" => "OperationRejected",
"InsufficientFunds" => "ExchangeError",
"InvalidAddress" => "ExchangeError",
"AddressPending" => "InvalidAddress",
"InvalidOrder" => "ExchangeError",
"OrderNotFound" => "InvalidOrder",
"OrderNotCached" => "InvalidOrder",
"OrderImmediatelyFillable" => "InvalidOrder",
"OrderNotFillable" => "InvalidOrder",
"DuplicateOrderId" => "InvalidOrder",
"ContractUnavailable" => "InvalidOrder",
"NotSupported" => "ExchangeError",
"InvalidProxySettings" => "ExchangeError",
"ExchangeClosedByUser" => "ExchangeError",
"OperationFailed" => "BaseError",
"NetworkError" => "OperationFailed",
"DDoSProtection" => "NetworkError",
"RateLimitExceeded" => "NetworkError",
"ExchangeNotAvailable" => "NetworkError",
"OnMaintenance" => "ExchangeNotAvailable",
"InvalidNonce" => "NetworkError",
"ChecksumError" => "InvalidNonce",
"RequestTimeout" => "NetworkError",
"BadResponse" => "OperationFailed",
"NullResponse" => "BadResponse",
"CancelPending" => "OperationFailed",
"UnsubscribeError" => "BaseError",
_ => return None,
})
}
pub fn get_value(obj: &Value, key: &Value) -> Value {
crate::value::get_value(obj, key)
}
#[allow(clippy::mut_from_ref, invalid_reference_casting)]
pub unsafe fn coerce_value_to_mut(v: &Value) -> &mut Value {
let ptr = v as *const Value as *mut Value;
&mut *ptr
}
pub fn get_value_mut<'a>(obj: &'a mut Value, key: &Value) -> &'a mut Value {
match (obj, key) {
(Value::Dict(m), Value::Str(k)) => Arc::make_mut(m).entry(k.clone()).or_insert(Value::Null),
(Value::Arr(a), Value::Int(i)) => {
let idx = *i as usize;
let a = Arc::make_mut(a);
while a.len() <= idx { a.push(Value::Null); }
&mut a[idx]
}
(other, _) => other,
}
}
pub fn add_element_to_object(obj: &mut Value, key: &Value, val: Value) {
match (obj, key) {
(Value::Dict(m), Value::Str(k)) => {
if crate::value::try_book_meta_write(m, k, &val) { return; }
crate::value::try_ws_subs_write(m, k, &val);
crate::value::try_ws_sub_field_write(m, k, &val);
crate::value::try_cache_hashmap_write(m, k, &val);
Arc::make_mut(m).insert(k.clone(), val);
}
(Value::Dict(m), other) => { Arc::make_mut(m).insert(stringify_simple(other), val); }
(Value::Arr(a), Value::Int(i)) => {
let idx = *i as usize;
let a = Arc::make_mut(a);
while a.len() <= idx { a.push(Value::Null); }
a[idx] = val;
}
_ => {}
}
}
pub fn remove(obj: &mut Value, key: &Value) {
match (obj, key) {
(Value::Dict(m), Value::Str(k)) => {
crate::value::try_ws_subs_remove(m, k);
Arc::make_mut(m).shift_remove(k);
}
(Value::Arr(a), Value::Int(i)) => {
let idx = *i as usize;
let a = Arc::make_mut(a);
if idx < a.len() { a.remove(idx); }
}
_ => {}
}
}
pub fn shift(mut arr: Value) -> Value {
if let Value::Arr(a) = &mut arr {
let inner = Arc::make_mut(a);
if !inner.is_empty() { return inner.remove(0); }
}
Value::Null
}
pub fn in_op(obj: &Value, key: &Value) -> bool {
match (obj, key) {
(Value::Dict(m), Value::Str(k)) => m.contains_key(k),
(Value::Arr(a), v) => a.iter().any(|x| is_equal(x, v)),
_ => false,
}
}
fn is_ws_internal_tag(k: &str) -> bool {
k == "__ws_subs_url" || k == "__ws_sub_ref"
}
pub fn object_keys(v: &Value) -> Value {
match v {
Value::Dict(m) => Value::Array(
m.keys().filter(|k| !is_ws_internal_tag(k))
.map(|k| Value::Str(k.clone())).collect()),
_ => Value::Array(vec![]),
}
}
pub fn object_values(v: &Value) -> Value {
match v {
Value::Dict(m) => Value::Array(
m.iter().filter(|(k, _)| !is_ws_internal_tag(k))
.map(|(_, val)| val.clone()).collect()),
_ => Value::Array(vec![]),
}
}
pub fn get_array_length(v: &Value) -> Value {
if let Value::Dict(d) = v {
if let Some(n) = crate::value::book_cache_len_of(d) {
return Value::Int(n as i64);
}
if let Some(n) = crate::value::cache_len_of_marker(d) {
return Value::Int(n as i64);
}
if d.contains_key("__sideKind") {
return Value::Int(crate::value::side_entries_len(d) as i64);
}
}
Value::Int(v.len() as i64)
}
pub fn get_index_of(haystack: &Value, needle: &Value) -> Value {
match haystack {
Value::Arr(a) => {
for (i, item) in a.iter().enumerate() {
if is_equal(item, needle) { return Value::Int(i as i64); }
}
Value::Int(-1)
}
Value::Str(s) => {
if let Value::Str(n) = needle {
match s.find(n.as_str()) {
Some(i) => Value::Int(i as i64),
None => Value::Int(-1),
}
} else { Value::Int(-1) }
}
_ => Value::Int(-1),
}
}
pub fn json_parse(v: &Value) -> Value {
match v {
Value::Str(s) => match serde_json::from_str::<serde_json::Value>(s) {
Ok(j) => Value::from_json(&j),
Err(_) => Value::Null,
},
_ => Value::Null,
}
}
pub fn quote_json_numbers(s: &str) -> String {
let b = s.as_bytes();
let n = b.len();
let mut out: Vec<u8> = Vec::with_capacity(n + 16);
let mut i = 0;
while i < n {
if b[i] == b'"' && i + 1 < n && b[i + 1] == b':' {
out.push(b'"');
out.push(b':');
let start = i + 2;
let mut j = start;
while j < n && matches!(b[j], b'+' | b'.' | b'0'..=b'9' | b'e' | b'E' | b'-') {
j += 1;
}
if j > start && j < n && (b[j] == b',' || b[j] == b'}') {
out.push(b'"');
out.extend_from_slice(&b[start..j]);
out.push(b'"');
out.push(b[j]);
i = j + 1;
continue;
}
i += 2;
continue;
}
out.push(b[i]);
i += 1;
}
String::from_utf8(out).unwrap_or_else(|_| s.to_string())
}
pub fn json_stringify(v: &Value) -> Value {
Value::Str(serde_json::to_string(&v.to_json()).unwrap_or_default())
}
pub fn math_floor(v: &Value) -> Value {
match as_f64(v) { Some(x) => Value::Float(x.floor()), None => Value::Null }
}
pub fn math_round(v: &Value) -> Value {
match as_f64(v) { Some(x) => Value::Float(x.round()), None => Value::Null }
}
pub fn math_ceil(v: &Value) -> Value {
match as_f64(v) { Some(x) => Value::Float(x.ceil()), None => Value::Null }
}
pub fn ternary(cond: bool, when_true: Value, when_false: Value) -> Value {
if cond { when_true } else { when_false }
}
pub fn println_val(v: &Value) {
println!("{v}");
}
pub async fn promise_all(v: &Value) -> Value {
v.clone()
}
pub fn get_arg(args: &[Value], idx: usize, default: Value) -> Value {
match args.get(idx) {
None => default,
Some(Value::Null) if !matches!(default, Value::Null) => default,
Some(v) => v.clone(),
}
}
pub fn append_to_array(arr: &mut Value, v: Value) {
if crate::value::try_book_cache_push(arr, &v) { return; }
if let Value::Arr(a) = arr {
Arc::make_mut(a).push(v);
}
}
pub fn append_to_object_array(obj: &mut Value, key: &Value, v: Value) {
let field = crate::get_value(obj, key);
if crate::value::try_book_cache_push(&field, &v) {
return;
}
if let (Value::Dict(m), Value::Str(k)) = (obj, key) {
let entry = Arc::make_mut(m).entry(k.clone())
.or_insert_with(|| Value::Array(Vec::new()));
if let Value::Arr(a) = entry {
Arc::make_mut(a).push(v);
}
}
}
pub fn deep_merge_dict(base: &Value, over: &Value) -> Value {
match (base, over) {
(Value::Dict(b), Value::Dict(o)) => {
let mut out = (**b).clone();
for (k, v) in o.iter() {
let merged = match out.get(k) {
Some(existing) => deep_merge_dict(existing, v),
None => v.clone(),
};
out.insert(k.clone(), merged);
}
Value::Map(out)
}
_ => over.clone(),
}
}
pub fn concat_arrays(a: &Value, b: &Value) -> Value {
match (a, b) {
(Value::Arr(x), Value::Arr(y)) => {
let mut out = (**x).clone();
out.extend(y.iter().cloned());
Value::Array(out)
}
_ => Value::Array(vec![]),
}
}
pub use crate::exchange_errors::*;
pub use crate::precise::Precise;
#[allow(non_snake_case)]
pub mod Math {
use super::Value;
use super::{as_f64, as_i64, both_ints};
pub fn min(a: &Value, b: &Value) -> Value {
if both_ints(a, b) { return Value::Int(as_i64(a).unwrap().min(as_i64(b).unwrap())); }
match (as_f64(a), as_f64(b)) { (Some(x), Some(y)) => Value::Float(x.min(y)), _ => Value::Null }
}
pub fn max(a: &Value, b: &Value) -> Value {
if both_ints(a, b) { return Value::Int(as_i64(a).unwrap().max(as_i64(b).unwrap())); }
match (as_f64(a), as_f64(b)) { (Some(x), Some(y)) => Value::Float(x.max(y)), _ => Value::Null }
}
pub fn abs(a: &Value) -> Value {
if let Value::Int(n) = a { if let Some(v) = n.checked_abs() { return Value::Int(v); } }
match as_f64(a) { Some(x) => Value::Float(x.abs()), None => Value::Null }
}
pub fn pow(a: &Value, b: &Value) -> Value {
match (as_f64(a), as_f64(b)) { (Some(x), Some(y)) => Value::Float(x.powf(y)), _ => Value::Null }
}
pub fn floor(v: &Value) -> Value { super::math_floor(v) }
pub fn round(v: &Value) -> Value { super::math_round(v) }
pub fn ceil(v: &Value) -> Value { super::math_ceil(v) }
pub fn log(v: &Value) -> Value {
match as_f64(v) { Some(x) => Value::Float(x.ln()), None => Value::Null }
}
}
pub fn slice(v: &Value, start: &Value, end: &Value) -> Value {
let s = as_i64(start).unwrap_or(0);
let e_opt = as_i64(end);
match v {
Value::Str(s_val) => {
let chars: Vec<char> = s_val.chars().collect();
let len = chars.len() as i64;
let si = if s < 0 { (len + s).max(0) as usize } else { (s as usize).min(chars.len()) };
let ei = match e_opt {
None => chars.len(),
Some(n) => if n < 0 { (len + n).max(0) as usize } else { (n as usize).min(chars.len()) },
};
if si <= ei { Value::Str(chars[si..ei].iter().collect()) } else { Value::Str(String::new()) }
}
Value::Arr(a) => {
let len = a.len() as i64;
let si = if s < 0 { (len + s).max(0) as usize } else { (s as usize).min(a.len()) };
let ei = match e_opt {
None => a.len(),
Some(n) => if n < 0 { (len + n).max(0) as usize } else { (n as usize).min(a.len()) },
};
if si <= ei { Value::Array(a[si..ei].to_vec()) } else { Value::Array(vec![]) }
}
_ => Value::Null,
}
}
pub fn join(arr: &Value, sep: &Value) -> Value {
let s = stringify_simple(sep);
if let Value::Arr(a) = arr {
let parts: Vec<String> = a.iter().map(stringify_simple).collect();
Value::Str(parts.join(&s))
} else { Value::Str(String::new()) }
}
pub fn to_string_val(v: &Value) -> Value {
Value::Str(stringify_simple(v))
}
pub fn parse_int(v: &Value) -> Value {
match v {
Value::Int(_) => v.clone(),
Value::Float(f) => Value::Int(*f as i64),
Value::Str(s) => s.trim().parse::<i64>().map(Value::Int).unwrap_or(Value::Null),
_ => Value::Null,
}
}
pub fn parse_float(v: &Value) -> Value {
match v {
Value::Float(_) => v.clone(),
Value::Int(n) => Value::Float(*n as f64),
Value::Str(s) => s.trim().parse::<f64>().map(Value::Float).unwrap_or(Value::Null),
_ => Value::Null,
}
}
pub fn length(v: &Value) -> Value { get_array_length(v) }
pub fn starts_with(haystack: &Value, prefix: &Value) -> bool {
match (haystack, prefix) {
(Value::Str(h), Value::Str(p)) => h.starts_with(p.as_str()),
_ => false,
}
}
pub fn ends_with(haystack: &Value, suffix: &Value) -> bool {
match (haystack, suffix) {
(Value::Str(h), Value::Str(s)) => h.ends_with(s.as_str()),
_ => false,
}
}
pub fn to_lower(v: &Value) -> Value {
match v { Value::Str(s) => Value::Str(s.to_lowercase()), _ => Value::Null }
}
pub fn to_upper(v: &Value) -> Value {
match v { Value::Str(s) => Value::Str(s.to_uppercase()), _ => Value::Null }
}
pub fn trim(v: &Value) -> Value {
match v { Value::Str(s) => Value::Str(s.trim().to_string()), _ => Value::Null }
}
pub fn index_of(haystack: &Value, needle: &Value) -> Value {
get_index_of(haystack, needle)
}
pub fn contains(haystack: &Value, needle: &Value) -> bool {
match (haystack, needle) {
(Value::Str(h), Value::Str(n)) => h.contains(n.as_str()),
(Value::Arr(a), n) => a.iter().any(|el| is_equal(el, n)),
_ => false,
}
}
pub const TRUNCATE: i64 = 0;
pub const ROUND: i64 = 1;
pub const ROUND_UP: i64 = 2;
pub const ROUND_DOWN: i64 = 3;
pub const DECIMAL_PLACES: i64 = 2;
pub const SIGNIFICANT_DIGITS: i64 = 3;
pub const TICK_SIZE: i64 = 4;
pub const NO_PADDING: i64 = 5;
pub const PAD_WITH_ZERO: i64 = 6;
pub fn string_replace(s: &Value, old: &Value, new_val: &Value) -> Value {
match (s, old, new_val) {
(Value::Str(s), Value::Str(o), Value::Str(n)) => Value::Str(s.replace(o.as_str(), n)),
_ => s.clone(),
}
}
pub fn split(s: &Value, delim: &Value) -> Value {
match (s, delim) {
(Value::Str(s), Value::Str(d)) => {
Value::Array(s.split(d.as_str()).map(|p| Value::Str(p.to_string())).collect())
}
_ => Value::Array(vec![]),
}
}
pub fn concat(a: &Value, b: &Value) -> Value {
match (a, b) {
(Value::Arr(_), Value::Arr(_)) => concat_arrays(a, b),
(Value::Str(x), Value::Str(y)) => Value::Str(format!("{x}{y}")),
_ => Value::Null,
}
}
pub fn repeat(s: &Value, n: &Value) -> Value {
let count = match as_i64(n) { Some(v) => v as usize, None => 0 };
match s { Value::Str(s) => Value::Str(s.repeat(count)), _ => Value::Null }
}
fn base32_decode(s: &str) -> Vec<u8> {
const ALPHA: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
let mut buf: u32 = 0;
let mut bits: u32 = 0;
let mut out = Vec::new();
for c in s.bytes() {
let up = c.to_ascii_uppercase();
let val = match ALPHA.iter().position(|&x| x == up) {
Some(v) => v as u32,
None => continue,
};
buf = (buf << 5) | val;
bits += 5;
if bits >= 8 {
bits -= 8;
out.push((buf >> bits) as u8);
}
}
out
}
pub fn totp(secret: Value) -> Value {
use hmac::{Hmac, Mac};
use sha1::Sha1;
let sec = match &secret {
Value::Str(s) => s.replace(' ', ""),
_ => return Value::Null,
};
let key = base32_decode(&sec);
let epoch = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0);
let counter: u64 = epoch / 30;
let mut mac = match Hmac::<Sha1>::new_from_slice(&key) {
Ok(m) => m,
Err(_) => return Value::Null,
};
mac.update(&counter.to_be_bytes());
let digest = mac.finalize().into_bytes(); let offset = (digest[19] & 0x0f) as usize;
let code = ((digest[offset] as u32 & 0x7f) << 24)
| ((digest[offset + 1] as u32) << 16)
| ((digest[offset + 2] as u32) << 8)
| (digest[offset + 3] as u32);
Value::Str(format!("{:06}", code % 1_000_000))
}
pub fn encode(s: Value) -> Value {
match &s {
Value::Str(st) => Value::Array(st.bytes().map(|b| Value::Int(b as i64)).collect()),
Value::Arr(_) => s,
_ => Value::Array(vec![]),
}
}
pub fn hash(data: Value, algo: Value, digest: Value) -> Value {
let dbytes = crate::exchange::value_to_bytes(&data);
let a = match &algo { Value::Str(s) => s.as_str(), _ => "sha256" };
let dg = match &digest { Value::Str(s) => s.as_str(), _ => "hex" };
let raw = crate::exchange::hash_raw(&dbytes, a);
match dg {
"binary" => Value::Array(raw.iter().map(|b| Value::Int(*b as i64)).collect()),
"base64" => Value::Str(b64_encode(&raw)),
_ => Value::Str(hex::encode(&raw)),
}
}
pub fn hmac(data: Value, secret: Value, algo: Value, digest: Value) -> Value {
use hmac::{Hmac, Mac};
let dbytes = crate::exchange::value_to_bytes(&data);
let sbytes = crate::exchange::value_to_bytes(&secret);
let a = match &algo { Value::Str(s) => s.to_ascii_lowercase(), _ => "sha256".to_string() };
let dg = match &digest { Value::Str(s) => s.as_str(), _ => "hex" };
let raw: Vec<u8> = match a.as_str() {
"sha256" => { let mut m = Hmac::<sha2::Sha256>::new_from_slice(&sbytes).unwrap(); m.update(&dbytes); m.finalize().into_bytes().to_vec() }
"sha512" => { let mut m = Hmac::<sha2::Sha512>::new_from_slice(&sbytes).unwrap(); m.update(&dbytes); m.finalize().into_bytes().to_vec() }
"sha384" => { let mut m = Hmac::<sha2::Sha384>::new_from_slice(&sbytes).unwrap(); m.update(&dbytes); m.finalize().into_bytes().to_vec() }
"sha1" => { let mut m = Hmac::<sha1::Sha1>::new_from_slice(&sbytes).unwrap(); m.update(&dbytes); m.finalize().into_bytes().to_vec() }
"md5" => { let mut m = Hmac::<md5::Md5>::new_from_slice(&sbytes).unwrap(); m.update(&dbytes); m.finalize().into_bytes().to_vec() }
_ => return Value::Null,
};
match dg {
"binary" => Value::Array(raw.iter().map(|b| Value::Int(*b as i64)).collect()),
"base64" => Value::Str(b64_encode(&raw)),
_ => Value::Str(hex::encode(&raw)),
}
}
pub fn crc32(s: Value, signed: Value) -> Value {
let bytes = crate::exchange::value_to_bytes(&s);
let mut crc: u32 = 0xFFFF_FFFF;
for &b in &bytes {
crc ^= b as u32;
for _ in 0..8 {
crc = if crc & 1 != 0 { (crc >> 1) ^ 0xEDB8_8320 } else { crc >> 1 };
}
}
crc = !crc;
if matches!(signed, Value::Bool(true)) {
Value::Int(crc as i32 as i64)
} else {
Value::Int(crc as i64)
}
}
struct PbReader<'a> {
buf: &'a [u8],
pos: usize,
}
impl<'a> PbReader<'a> {
fn new(buf: &'a [u8]) -> Self {
Self { buf, pos: 0 }
}
fn eof(&self) -> bool {
self.pos >= self.buf.len()
}
fn read_varint(&mut self) -> u64 {
let mut result: u64 = 0;
let mut shift = 0u32;
while self.pos < self.buf.len() && shift < 64 {
let b = self.buf[self.pos];
self.pos += 1;
result |= ((b & 0x7f) as u64) << shift;
if b & 0x80 == 0 {
break;
}
shift += 7;
}
result
}
fn read_tag(&mut self) -> (u64, u8) {
let t = self.read_varint();
(t >> 3, (t & 7) as u8)
}
fn read_bytes(&mut self) -> &'a [u8] {
let len = self.read_varint() as usize;
let end = self.pos.saturating_add(len).min(self.buf.len());
let s = &self.buf[self.pos..end];
self.pos = end;
s
}
fn skip(&mut self, wire: u8) {
match wire {
0 => {
self.read_varint();
}
1 => self.pos = self.pos.saturating_add(8).min(self.buf.len()),
2 => {
let len = self.read_varint() as usize;
self.pos = self.pos.saturating_add(len).min(self.buf.len());
}
5 => self.pos = self.pos.saturating_add(4).min(self.buf.len()),
_ => self.pos = self.buf.len(),
}
}
}
fn pb_string(b: &[u8]) -> Value {
Value::Str(String::from_utf8_lossy(b).into_owned())
}
fn pb_decode_depth_item(b: &[u8]) -> Value {
let mut r = PbReader::new(b);
let mut m: HashMap<String, Value> = HashMap::new();
while !r.eof() {
match r.read_tag() {
(1, 2) => {
m.insert("price".to_string(), pb_string(r.read_bytes()));
}
(2, 2) => {
m.insert("quantity".to_string(), pb_string(r.read_bytes()));
}
(_, wire) => r.skip(wire),
}
}
Value::Map(m)
}
fn pb_decode_depths(b: &[u8]) -> Value {
let mut r = PbReader::new(b);
let mut asks: Vec<Value> = Vec::new();
let mut bids: Vec<Value> = Vec::new();
let mut m: HashMap<String, Value> = HashMap::new();
while !r.eof() {
match r.read_tag() {
(1, 2) => asks.push(pb_decode_depth_item(r.read_bytes())),
(2, 2) => bids.push(pb_decode_depth_item(r.read_bytes())),
(3, 2) => {
m.insert("eventType".to_string(), pb_string(r.read_bytes()));
}
(4, 2) => {
m.insert("fromVersion".to_string(), pb_string(r.read_bytes()));
}
(5, 2) => {
m.insert("toVersion".to_string(), pb_string(r.read_bytes()));
}
(_, wire) => r.skip(wire),
}
}
m.insert("asks".to_string(), Value::List(asks));
m.insert("bids".to_string(), Value::List(bids));
Value::Map(m)
}
fn pb_decode_book_ticker(b: &[u8]) -> Value {
let mut r = PbReader::new(b);
let mut m: HashMap<String, Value> = HashMap::new();
let fields = ["bidPrice", "bidQuantity", "askPrice", "askQuantity"];
while !r.eof() {
match r.read_tag() {
(n @ 1..=4, 2) => {
m.insert(fields[(n - 1) as usize].to_string(), pb_string(r.read_bytes()));
}
(_, wire) => r.skip(wire),
}
}
Value::Map(m)
}
fn pb_decode_deal_item(b: &[u8]) -> Value {
let mut r = PbReader::new(b);
let mut m: HashMap<String, Value> = HashMap::new();
while !r.eof() {
match r.read_tag() {
(1, 2) => { m.insert("price".to_string(), pb_string(r.read_bytes())); }
(2, 2) => { m.insert("quantity".to_string(), pb_string(r.read_bytes())); }
(3, 0) => { m.insert("tradeType".to_string(), Value::Int(r.read_varint() as i64)); }
(4, 0) => { m.insert("time".to_string(), Value::Int(r.read_varint() as i64)); }
(_, wire) => r.skip(wire),
}
}
Value::Map(m)
}
fn pb_decode_deals(b: &[u8]) -> Value {
let mut r = PbReader::new(b);
let mut deals: Vec<Value> = Vec::new();
let mut m: HashMap<String, Value> = HashMap::new();
while !r.eof() {
match r.read_tag() {
(1, 2) => deals.push(pb_decode_deal_item(r.read_bytes())),
(2, 2) => { m.insert("eventType".to_string(), pb_string(r.read_bytes())); }
(_, wire) => r.skip(wire),
}
}
m.insert("deals".to_string(), Value::List(deals));
Value::Map(m)
}
fn pb_decode_spot_kline(b: &[u8]) -> Value {
let mut r = PbReader::new(b);
let mut m: HashMap<String, Value> = HashMap::new();
while !r.eof() {
match r.read_tag() {
(1, 2) => { m.insert("interval".to_string(), pb_string(r.read_bytes())); }
(2, 0) => { m.insert("windowStart".to_string(), Value::Int(r.read_varint() as i64)); }
(3, 2) => { m.insert("openingPrice".to_string(), pb_string(r.read_bytes())); }
(4, 2) => { m.insert("closingPrice".to_string(), pb_string(r.read_bytes())); }
(5, 2) => { m.insert("highestPrice".to_string(), pb_string(r.read_bytes())); }
(6, 2) => { m.insert("lowestPrice".to_string(), pb_string(r.read_bytes())); }
(7, 2) => { m.insert("volume".to_string(), pb_string(r.read_bytes())); }
(8, 2) => { m.insert("amount".to_string(), pb_string(r.read_bytes())); }
(9, 0) => { m.insert("windowEnd".to_string(), Value::Int(r.read_varint() as i64)); }
(_, wire) => r.skip(wire),
}
}
Value::Map(m)
}
pub fn decode_mexc_push_data(bytes: &[u8]) -> Value {
let mut r = PbReader::new(bytes);
let mut m: HashMap<String, Value> = HashMap::new();
while !r.eof() {
match r.read_tag() {
(1, 2) => {
m.insert("channel".to_string(), pb_string(r.read_bytes()));
}
(3, 2) => {
m.insert("symbol".to_string(), pb_string(r.read_bytes()));
}
(4, 2) => {
m.insert("symbolId".to_string(), pb_string(r.read_bytes()));
}
(5, 0) => {
m.insert("createTime".to_string(), Value::Int(r.read_varint() as i64));
}
(6, 0) => {
m.insert("sendTime".to_string(), Value::Int(r.read_varint() as i64));
}
(302, 2) => {
m.insert("publicIncreaseDepths".to_string(), pb_decode_depths(r.read_bytes()));
}
(303, 2) => {
m.insert("publicLimitDepths".to_string(), pb_decode_depths(r.read_bytes()));
}
(313, 2) => {
m.insert("publicAggreDepths".to_string(), pb_decode_depths(r.read_bytes()));
}
(301, 2) => {
m.insert("publicDeals".to_string(), pb_decode_deals(r.read_bytes()));
}
(314, 2) => {
m.insert("publicAggreDeals".to_string(), pb_decode_deals(r.read_bytes()));
}
(305, 2) => {
m.insert("publicBookTicker".to_string(), pb_decode_book_ticker(r.read_bytes()));
}
(315, 2) => {
m.insert("publicAggreBookTicker".to_string(), pb_decode_book_ticker(r.read_bytes()));
}
(308, 2) => {
m.insert("publicSpotKline".to_string(), pb_decode_spot_kline(r.read_bytes()));
}
(_, wire) => r.skip(wire),
}
}
Value::Map(m)
}
pub fn rsa(message: Value, key: Value, _hash: Value) -> Value {
use rsa::RsaPrivateKey;
use rsa::pkcs1::DecodeRsaPrivateKey;
use rsa::pkcs1v15::SigningKey;
use rsa::signature::{SignatureEncoding, Signer};
let msg = crate::exchange::value_to_bytes(&message);
let pem = String::from_utf8_lossy(&crate::exchange::value_to_bytes(&key)).into_owned();
let pk = match RsaPrivateKey::from_pkcs1_pem(pem.trim()) {
Ok(k) => k,
Err(_) => return Value::Str(String::new()),
};
let signing_key = SigningKey::<sha2::Sha256>::new(pk);
let sig = signing_key.sign(&msg);
Value::Str(b64_encode(&sig.to_bytes()))
}
pub fn ecdsa(message: Value, secret: Value, _curve: Value, pre_hash: Value) -> Value {
use k256::ecdsa::SigningKey;
let empty = || Value::Map(indexmap::IndexMap::new());
let msg_s = match &message { Value::Str(s) => s.trim_start_matches("0x").to_string(), _ => return empty() };
let sk_s = match &secret { Value::Str(s) => s.trim_start_matches("0x").to_string(), _ => return empty() };
let sk_bytes = match hex::decode(&sk_s) { Ok(b) => b, Err(_) => return empty() };
let digest: Vec<u8> = match &pre_hash {
Value::Str(algo) => crate::exchange::hash_raw(
&crate::exchange::value_to_bytes(&message), algo),
_ => match hex::decode(&msg_s) { Ok(b) => b, Err(_) => return empty() },
};
let sk = match SigningKey::from_slice(&sk_bytes) { Ok(k) => k, Err(_) => return empty() };
let (sig, recid) = match sk.sign_prehash_recoverable(&digest) {
Ok(x) => x,
Err(_) => return empty(),
};
let bytes = sig.to_bytes(); let mut m = indexmap::IndexMap::new();
m.insert("r".to_string(), Value::Str(hex::encode(&bytes[0..32])));
m.insert("s".to_string(), Value::Str(hex::encode(&bytes[32..64])));
m.insert("v".to_string(), Value::Int(recid.to_byte() as i64));
Value::Map(m)
}
fn pem_to_der(pem: &str) -> Vec<u8> {
use base64::{engine::general_purpose::STANDARD, Engine as _};
let body: String = pem
.lines()
.filter(|l| !l.starts_with("-----"))
.collect::<Vec<_>>()
.join("");
STANDARD.decode(body.trim()).unwrap_or_default()
}
pub fn eddsa(request: Value, secret: Value, _curve: Value) -> Value {
use ed25519_dalek::{Signer, SigningKey};
let bad = |why: &str| -> ! {
panic!("{}", crate::exchange_errors::not_supported(
Value::Str(format!("eddsa: {why}"))));
};
let msg: Vec<u8> = match &request {
Value::Arr(_) => crate::exchange::value_to_bytes(&request),
Value::Str(s) => {
let t = s.trim_start_matches("0x");
hex::decode(t).unwrap_or_else(|_| s.as_bytes().to_vec())
}
_ => bad("request must be bytes or a string"),
};
let seed: [u8; 32] = match &secret {
Value::Arr(_) => {
let b = crate::exchange::value_to_bytes(&secret);
if b.len() >= 32 { b[..32].try_into().unwrap() } else { bad("seed shorter than 32 bytes") }
}
Value::Str(sec_s) if sec_s.len() == 32 => {
sec_s.as_bytes().try_into().unwrap()
}
Value::Str(sec_s) => {
let der = pem_to_der(sec_s);
if der.len() >= 48 {
der[16..48].try_into().unwrap()
} else if der.len() >= 32 {
der[der.len() - 32..].try_into().unwrap()
} else {
bad("secret is not a 32-byte seed or a decodable PKCS#8 key");
}
}
_ => bad("secret must be a string or byte array"),
};
let sk = SigningKey::from_bytes(&seed);
let sig = sk.sign(&msg);
Value::Str(b64_encode(&sig.to_bytes()))
}
fn b64_encode(data: &[u8]) -> String {
use base64::{engine::general_purpose::STANDARD, Engine as _};
STANDARD.encode(data)
}
fn b64url_encode(data: &[u8]) -> String {
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine as _};
URL_SAFE_NO_PAD.encode(data)
}
pub fn jwt(request: Value, secret: Value, algorithm: Value, is_rsa: Value, _opts: Value) -> Value {
let rsa_mode = matches!(is_rsa, Value::Bool(true));
let bits = match algorithm.as_str() {
Some("sha512") => "512",
Some("sha384") => "384",
_ => "256",
};
let alg = format!("{}{}", if rsa_mode { "RS" } else { "HS" }, bits);
let header = format!("{{\"alg\":\"{alg}\",\"typ\":\"JWT\"}}");
let payload = serde_json::to_string(&request.to_json()).unwrap_or_else(|_| "{}".to_string());
let token = format!("{}.{}",
b64url_encode(header.as_bytes()),
b64url_encode(payload.as_bytes()));
let sig_std: String = if rsa_mode {
match rsa(Value::Str(token.clone()), secret, algorithm) {
Value::Str(s) => s,
_ => String::new(),
}
} else {
match hmac(encode(Value::Str(token.clone())), secret, algorithm,
Value::Str("base64".to_string())) {
Value::Str(s) => s,
_ => String::new(),
}
};
let sig_url = sig_std.replace('+', "-").replace('/', "_")
.trim_end_matches('=').to_string();
Value::Str(format!("{token}.{sig_url}"))
}
pub fn replace_str(s: &Value, old: &Value, new_val: &Value) -> Value {
string_replace(s, old, new_val)
}
pub fn replace_all_str(s: &Value, old: &Value, new_val: &Value) -> Value {
let (Value::Str(sv), Value::Str(ov), Value::Str(nv)) = (s, old, new_val) else {
return s.clone();
};
Value::Str(sv.replace(ov, nv))
}
pub fn date_now() -> Value {
let ms = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_millis() as i64)
.unwrap_or(0);
Value::Int(ms)
}
pub fn pad_start(s: &Value, len: &Value, pad: &Value) -> Value {
let target = match as_i64(len) { Some(v) => v as usize, None => 0 };
let p = match pad { Value::Str(p) => p.clone(), _ => " ".to_string() };
match s {
Value::Str(sv) => {
if sv.chars().count() >= target { Value::Str(sv.clone()) }
else {
let need = target - sv.chars().count();
Value::Str(format!("{}{}", p.repeat((need / p.chars().count().max(1)).max(1)), sv))
}
}
_ => Value::Null,
}
}
pub fn pad_end(s: &Value, len: &Value, pad: &Value) -> Value {
let target = match as_i64(len) { Some(v) => v as usize, None => 0 };
let p = match pad { Value::Str(p) => p.clone(), _ => " ".to_string() };
match s {
Value::Str(sv) => {
if sv.chars().count() >= target { Value::Str(sv.clone()) }
else {
let need = target - sv.chars().count();
Value::Str(format!("{}{}", sv, p.repeat((need / p.chars().count().max(1)).max(1))))
}
}
_ => Value::Null,
}
}
pub fn to_fixed(x: &Value, digits: &Value) -> Value {
let d = match as_i64(digits) { Some(v) => v.max(0) as usize, None => 0 };
let n = match x {
Value::Int(i) => *i as f64,
Value::Float(f) => *f,
Value::Str(s) => s.trim().parse::<f64>().unwrap_or(0.0),
_ => 0.0,
};
Value::Str(format!("{:.*}", d, n))
}
pub fn empty_map() -> Value { Value::Map(HashMap::new()) }
pub fn empty_array() -> Value { Value::Array(vec![]) }