Skip to main content

bybit/
config.rs

1use std::borrow::Cow;
2
3#[derive(Clone, Debug)]
4pub struct Config {
5    pub rest_api_endpoint: Cow<'static, str>,
6    pub ws_endpoint: Cow<'static, str>,
7    pub recv_window: u16,
8}
9
10impl Config {
11    pub const DEFAULT_REST_API_ENDPOINT: &'static str = "https://api.bybit.com";
12    pub const DEFAULT_WS_ENDPOINT: &'static str = "wss://stream.bybit.com/v5";
13
14    pub fn new(
15        rest_api_endpoint: impl AsRef<str>,
16        ws_endpoint: impl AsRef<str>,
17        recv_window: impl Into<u16>,
18    ) -> Self {
19        Self {
20            rest_api_endpoint: Cow::Owned(rest_api_endpoint.as_ref().to_string()),
21            ws_endpoint: Cow::Owned(ws_endpoint.as_ref().to_string()),
22            recv_window: recv_window.into(),
23        }
24    }
25
26    pub const fn default() -> Self {
27        Self {
28            rest_api_endpoint: Cow::Borrowed(Self::DEFAULT_REST_API_ENDPOINT),
29            ws_endpoint: Cow::Borrowed(Self::DEFAULT_WS_ENDPOINT),
30            recv_window: 5000,
31        }
32    }
33
34    pub const fn testnet() -> Self {
35        Self {
36            rest_api_endpoint: Cow::Borrowed("https://api-testnet.bybit.com"),
37            ws_endpoint: Cow::Borrowed("wss://stream-testnet.bybit.com/v5"),
38            recv_window: 5000,
39        }
40    }
41
42    pub fn set_recv_window(self, recv_window: u16) -> Self {
43        Self {
44            recv_window,
45            ..self
46        }
47    }
48}