1use derive_builder::UninitializedFieldError;
2use serde::de::DeserializeOwned;
3use serde::{Deserialize, Serialize};
4use serde_json::Value;
5use std::fmt;
6use std::marker::PhantomData;
7use std::{collections::HashMap, future::Future, pin::Pin};
8use thiserror::Error;
9
10use super::errors::ConnectorError;
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub enum TimeUnit {
14 Millisecond,
15 Microsecond,
16}
17
18impl fmt::Display for TimeUnit {
19 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20 match self {
21 TimeUnit::Millisecond => write!(f, "millisecond"),
22 TimeUnit::Microsecond => write!(f, "microsecond"),
23 }
24 }
25}
26
27impl TimeUnit {
28 #[must_use]
29 pub fn as_upper_str(&self) -> &'static str {
30 match self {
31 TimeUnit::Millisecond => "MILLISECOND",
32 TimeUnit::Microsecond => "MICROSECOND",
33 }
34 }
35 #[must_use]
36 pub fn as_lower_str(&self) -> &'static str {
37 match self {
38 TimeUnit::Millisecond => "millisecond",
39 TimeUnit::Microsecond => "microsecond",
40 }
41 }
42}
43
44#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
45#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
46pub enum RateLimitType {
47 RequestWeight,
48 Orders,
49 RawRequests,
50}
51
52#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
53#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
54pub enum Interval {
55 Second,
56 Minute,
57 Hour,
58 Day,
59}
60
61#[derive(Debug, Clone)]
62pub struct RestApiRateLimit {
63 pub rate_limit_type: RateLimitType,
64 pub interval: Interval,
65 pub interval_num: u32,
66 pub count: u32,
67 pub retry_after: Option<u32>,
68}
69
70pub type DataFuture<T> = Pin<Box<dyn Future<Output = T> + Send>>;
71
72pub struct RestApiResponse<T> {
73 pub(crate) data_fn: Box<
74 dyn FnOnce() -> Pin<Box<dyn Future<Output = Result<T, ConnectorError>> + Send>>
75 + Send
76 + Sync,
77 >,
78 pub status: u16,
79 pub headers: HashMap<String, String>,
80 pub rate_limits: Option<Vec<RestApiRateLimit>>,
81}
82
83impl<T> RestApiResponse<T>
84where
85 T: Send + 'static,
86{
87 pub async fn data(self) -> Result<T, ConnectorError> {
105 (self.data_fn)().await
106 }
107}
108
109#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
110pub struct WebsocketApiRateLimit {
111 #[serde(rename = "rateLimitType")]
112 pub rate_limit_type: RateLimitType,
113 #[serde(rename = "interval")]
114 pub interval: Interval,
115 #[serde(rename = "intervalNum")]
116 pub interval_num: u32,
117 pub limit: u32,
118 #[serde(default)]
119 pub count: u32,
120}
121
122#[derive(Debug)]
123pub struct WebsocketApiResponse<T> {
124 pub(crate) _marker: PhantomData<T>,
125
126 pub raw: Value,
127 pub rate_limits: Option<Vec<WebsocketApiRateLimit>>,
128}
129
130impl<T> WebsocketApiResponse<T>
131where
132 T: DeserializeOwned,
133{
134 pub fn data(self) -> serde_json::Result<T> {
153 serde_json::from_value(self.raw)
154 }
155}
156
157#[derive(Debug, Error)]
158pub enum ParamBuildError {
159 #[error("missing required field `{0}`")]
160 UninitializedField(&'static str),
161}
162
163impl From<UninitializedFieldError> for ParamBuildError {
164 fn from(err: UninitializedFieldError) -> Self {
165 ParamBuildError::UninitializedField(err.field_name())
166 }
167}
168
169#[derive(Debug, Error)]
170pub enum ConfigBuildError {
171 #[error("Configuration missing or invalid `{0}`")]
172 UninitializedField(&'static str),
173}
174
175impl From<UninitializedFieldError> for ConfigBuildError {
176 fn from(err: UninitializedFieldError) -> Self {
177 ConfigBuildError::UninitializedField(err.field_name())
178 }
179}
180
181#[derive(Debug, Clone, PartialEq)]
182pub enum WebsocketEvent {
183 Open,
184 Message(String),
185 Error(String),
186 Close(u16, String),
187 Ping,
188 Pong,
189}
190
191#[derive(Debug, Clone, PartialEq)]
192pub enum WebsocketMode {
193 Single,
194 Pool(usize),
195}
196
197impl WebsocketMode {
198 #[must_use]
199 pub fn pool_size(&self) -> usize {
200 match *self {
201 WebsocketMode::Single => 1,
202 WebsocketMode::Pool(sz) => sz,
203 }
204 }
205}
206
207#[derive(Debug, Clone, Default)]
208pub struct WebsocketApiConnectConfig {
209 pub mode: Option<WebsocketMode>,
210}
211
212#[derive(Debug, Clone, Default)]
213pub struct WebsocketStreamsConnectConfig {
214 pub streams: Vec<String>,
215 pub mode: Option<WebsocketMode>,
216}
217
218#[derive(Debug, Clone, PartialEq, Eq)]
219pub enum StreamId {
220 Str(String),
221 Number(u32),
222}
223
224impl From<String> for StreamId {
225 fn from(v: String) -> Self {
226 StreamId::Str(v)
227 }
228}
229impl From<u32> for StreamId {
230 fn from(v: u32) -> Self {
231 StreamId::Number(v)
232 }
233}