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")]
11pub enum Method {
12 Subscribe,
14 Unsubscribe,
16}
17
18#[derive(Debug, Clone, PartialEq, Eq)]
21pub struct Ack {
22 pub method: Method,
24 pub echo: Json,
26}
27
28impl Ack {
29 pub fn subscription(&self) -> Result<Subscription, BadSubscription> {
31 Subscription::parse(&self.echo)
32 }
33}
34
35#[derive(Debug, Clone, PartialEq, Eq)]
38pub struct Refusal {
39 pub message: String,
41}
42
43impl Refusal {
44 #[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 #[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 "ed
72 }
73 None => return None,
74 };
75 Subscription::parse(subscription).ok()
76 }
77
78 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;