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