nautilus_okx/websocket/
subscription.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! OKX-specific subscription helpers.
17
18use 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}