df_hyperliquid_core/live/frame/
control.rs1use crate::live::{BadSubscription, Subscription};
5use crate::utils::Json;
6use serde::Deserialize;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
10#[serde(rename_all = "lowercase")]
11#[non_exhaustive]
12pub enum Method {
13 Subscribe,
15 Unsubscribe,
17}
18
19#[derive(Debug, Clone, PartialEq, Eq)]
22#[non_exhaustive]
23pub struct Ack {
24 pub method: Method,
26 pub echo: Json,
28}
29
30impl Ack {
31 pub fn subscription(&self) -> Result<Subscription, BadSubscription> {
33 Subscription::parse(&self.echo)
34 }
35}
36
37#[derive(Debug, Clone, PartialEq, Eq)]
40#[non_exhaustive]
41pub struct Refusal {
42 pub message: String,
44}
45
46impl Refusal {
47 #[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 #[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 "ed
75 }
76 None => return None,
77 };
78 Subscription::parse(subscription).ok()
79 }
80
81 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;