df-hyperliquid-core 0.2.3

Hyperliquid SDK for Rust with full API parity: info, exchange, websockets, HIP-3, HIP-4, HyperEVM and CoreWriter. Exact decimals, the fastest order signing and websocket parsing of the SDKs benchmarked, native and wasm.
Documentation
//! JSON that keeps what a signature depends on: keys in the order they were
//! written and numbers as the digits they were written with. An L1 action is
//! hashed as msgpack, which keeps key order, where `serde_json` sorts keys.

mod msgpack;
mod parse;
mod write;

pub use msgpack::Unpackable;
pub use parse::BadJson;

/// A JSON value that keeps key order and the digits of each number.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Json {
    /// `null`.
    Null,
    /// `true` or `false`.
    Bool(bool),
    /// The number exactly as written: no float ever holds it.
    Number(String),
    /// A string, unescaped.
    String(String),
    /// An array, items in order.
    Array(Vec<Json>),
    /// Keys in the order they were written.
    Object(Vec<(String, Json)>),
}

impl Json {
    /// An object's value under `key`, the first if the key repeats; `None` for a
    /// missing key or a value that is not an object.
    #[must_use]
    pub fn get(&self, key: &str) -> Option<&Json> {
        match self {
            Json::Object(entries) => entries.iter().find(|(name, _)| name == key).map(|(_, value)| value),
            _ => None,
        }
    }

    /// The text of a string; `None` for anything else.
    #[must_use]
    pub fn as_str(&self) -> Option<&str> {
        match self {
            Json::String(text) => Some(text),
            _ => None,
        }
    }

    /// `true` or `false`; `None` for anything else, a `"true"` string too.
    #[must_use]
    pub const fn as_bool(&self) -> Option<bool> {
        match self {
            Json::Bool(value) => Some(*value),
            _ => None,
        }
    }

    /// A whole non-negative number, as the venue writes counts and ids.
    #[must_use]
    pub fn as_u64(&self) -> Option<u64> {
        match self {
            Json::Number(digits) => digits.parse().ok(),
            _ => None,
        }
    }

    /// An array's items; `None` for anything else.
    #[must_use]
    pub fn as_array(&self) -> Option<&[Json]> {
        match self {
            Json::Array(items) => Some(items),
            _ => None,
        }
    }

    /// An object's keys and values in the order written; `None` for anything else.
    #[must_use]
    pub fn as_object(&self) -> Option<&[(String, Json)]> {
        match self {
            Json::Object(entries) => Some(entries),
            _ => None,
        }
    }

    /// What kind of value this is, for an error message: `null`, `a boolean`, `a number`,
    /// `a string`, `an array` or `an object`.
    #[must_use]
    pub const fn kind(&self) -> &'static str {
        match self {
            Json::Null => "null",
            Json::Bool(_) => "a boolean",
            Json::Number(_) => "a number",
            Json::String(_) => "a string",
            Json::Array(_) => "an array",
            Json::Object(_) => "an object",
        }
    }
}

#[cfg(test)]
#[path = "tests/json.rs"]
mod tests;