use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize)]
pub struct WsRequest<T> {
pub method: String,
pub params: T,
#[serde(skip_serializing_if = "Option::is_none")]
pub req_id: Option<u64>,
}
impl<T> WsRequest<T> {
pub fn new(method: impl Into<String>, params: T) -> Self {
Self {
method: method.into(),
params,
req_id: None,
}
}
pub fn with_req_id(mut self, id: u64) -> Self {
self.req_id = Some(id);
self
}
}
#[derive(Debug, Clone, Deserialize)]
pub struct WsResponse<T> {
pub method: String,
pub success: bool,
#[serde(default)]
pub result: Option<T>,
#[serde(default)]
pub error: Option<String>,
#[serde(default)]
pub req_id: Option<u64>,
#[serde(default)]
pub time_in: Option<String>,
#[serde(default)]
pub time_out: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
pub struct SubscribeParams {
pub channel: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub symbol: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub token: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub snapshot: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub depth: Option<u32>,
}
impl SubscribeParams {
pub fn public(channel: impl Into<String>, symbols: Vec<String>) -> Self {
Self {
channel: channel.into(),
symbol: Some(symbols),
token: None,
snapshot: None,
depth: None,
}
}
pub fn private(channel: impl Into<String>, token: impl Into<String>) -> Self {
Self {
channel: channel.into(),
symbol: None,
token: Some(token.into()),
snapshot: None,
depth: None,
}
}
pub fn with_snapshot(mut self, snapshot: bool) -> Self {
self.snapshot = Some(snapshot);
self
}
pub fn with_depth(mut self, depth: u32) -> Self {
self.depth = Some(depth);
self
}
}
pub mod channels {
pub const TICKER: &str = "ticker";
pub const BOOK: &str = "book";
pub const LEVEL3: &str = "level3";
pub const OHLC: &str = "ohlc";
pub const TRADE: &str = "trade";
pub const INSTRUMENT: &str = "instrument";
pub const STATUS: &str = "status";
pub const HEARTBEAT: &str = "heartbeat";
pub const EXECUTIONS: &str = "executions";
pub const BALANCES: &str = "balances";
}
#[derive(Debug, Clone, Deserialize)]
pub struct SubscriptionResult {
pub channel: String,
#[serde(default)]
pub symbol: Option<String>,
#[serde(default)]
pub snapshot: Option<bool>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct WsError {
pub code: Option<i32>,
pub message: String,
}