nautilus_okx/websocket/
subscription.rs1use ustr::Ustr;
19
20use crate::{
21 common::enums::OKXInstrumentType,
22 websocket::{
23 enums::OKXWsChannel,
24 messages::{OKXSubscriptionArg, OKXWebSocketArg},
25 },
26};
27
28fn topic_from_parts(
29 channel: &OKXWsChannel,
30 inst_id: Option<&Ustr>,
31 inst_family: Option<&Ustr>,
32 inst_type: Option<&OKXInstrumentType>,
33 bar: Option<&Ustr>,
34) -> String {
35 let base = channel.as_ref();
36
37 if let Some(inst_id) = inst_id {
38 let inst_id = inst_id.as_str();
39 if let Some(bar) = bar {
40 format!("{base}:{inst_id}:{}", bar.as_str())
41 } else {
42 format!("{base}:{inst_id}")
43 }
44 } else if let Some(inst_family) = inst_family {
45 format!("{base}:{}", inst_family.as_str())
46 } else if let Some(inst_type) = inst_type {
47 format!("{base}:{}", inst_type.as_ref())
48 } else {
49 base.to_string()
50 }
51}
52
53pub(crate) fn topic_from_subscription_arg(arg: &OKXSubscriptionArg) -> String {
54 topic_from_parts(
55 &arg.channel,
56 arg.inst_id.as_ref(),
57 arg.inst_family.as_ref(),
58 arg.inst_type.as_ref(),
59 None,
60 )
61}
62
63pub(crate) fn topic_from_websocket_arg(arg: &OKXWebSocketArg) -> String {
64 topic_from_parts(
65 &arg.channel,
66 arg.inst_id.as_ref(),
67 arg.inst_family.as_ref(),
68 arg.inst_type.as_ref(),
69 arg.bar.as_ref(),
70 )
71}