Skip to main content

df_hyperliquid_core/live/frame/
control.rs

1//! The venue's word on what was sent: an [`Ack`] when it took a subscribe or an unsubscribe, a
2//! [`Refusal`] in its own words when it refused something, a post included.
3
4use crate::live::{BadSubscription, Subscription};
5use crate::utils::Json;
6use serde::Deserialize;
7
8/// Which way a subscription went.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
10#[serde(rename_all = "lowercase")]
11pub enum Method {
12    /// `subscribe`.
13    Subscribe,
14    /// `unsubscribe`.
15    Unsubscribe,
16}
17
18/// The venue's acknowledgement, echoing the subscription as it read it: keys reordered, left
19/// out options filled in, the user in lowercase.
20#[derive(Debug, Clone, PartialEq, Eq)]
21pub struct Ack {
22    /// Whether a subscribe or an unsubscribe was taken.
23    pub method: Method,
24    /// `subscription`: the subscription as the venue echoed it.
25    pub echo: Json,
26}
27
28impl Ack {
29    /// The subscription it names.
30    pub fn subscription(&self) -> Result<Subscription, BadSubscription> {
31        Subscription::parse(&self.echo)
32    }
33}
34
35/// The venue refusing something: `Already subscribed: {...}`, `Already unsubscribed: {...}`,
36/// `Error parsing JSON into valid websocket request: {...}` with what was sent.
37#[derive(Debug, Clone, PartialEq, Eq)]
38pub struct Refusal {
39    /// The venue's message, as sent.
40    pub message: String,
41}
42
43impl Refusal {
44    /// The post it refuses: from an `id=<n>` at the end of the message, or the `id` of a post
45    /// it quotes back.
46    #[must_use]
47    pub fn post_id(&self) -> Option<u64> {
48        if let Some((_, digits)) = self.message.rsplit_once("id=")
49            && !digits.is_empty()
50            && digits.bytes().all(|byte| byte.is_ascii_digit())
51        {
52            return digits.parse().ok();
53        }
54        let quoted = self.quoted()?;
55        match quoted.get("method").and_then(Json::as_str) {
56            Some("post") => quoted.get("id").and_then(Json::as_u64),
57            _ => None,
58        }
59    }
60
61    /// The subscription it refuses, when it quotes one this crate reads.
62    #[must_use]
63    pub fn subscription(&self) -> Option<Subscription> {
64        let quoted = self.quoted()?;
65        let subscription = match quoted.get("subscription") {
66            Some(inside) => inside,
67            None if ["Already subscribed", "Already unsubscribed", "Invalid subscription"]
68                .iter()
69                .any(|start| self.message.starts_with(start)) =>
70            {
71                &quoted
72            }
73            None => return None,
74        };
75        Subscription::parse(subscription).ok()
76    }
77
78    /// The JSON the message ends with, when it ends with some.
79    fn quoted(&self) -> Option<Json> {
80        let at = self.message.find('{')?;
81        Json::parse(self.message.get(at..)?).ok()
82    }
83}
84
85#[cfg(test)]
86#[path = "tests/control.rs"]
87mod tests;