use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct WsSpreadOrderResult {
pub sprd_id: String,
pub ord_id: String,
pub cl_ord_id: String,
pub tag: String,
pub ts: String,
pub s_code: String,
#[serde(default)]
pub sub_code: Option<String>,
pub s_msg: String,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct WsSpreadCancelResult {
pub sprd_id: String,
pub ord_id: String,
pub cl_ord_id: String,
pub s_code: String,
#[serde(default)]
pub sub_code: Option<String>,
pub s_msg: String,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct WsSpreadAmendResult {
pub sprd_id: String,
pub ord_id: String,
pub cl_ord_id: String,
pub req_id: String,
pub s_code: String,
#[serde(default)]
pub sub_code: Option<String>,
pub s_msg: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum WsEstimatedPriceSettleType {
Settlement,
Delivery,
Exercise,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct WsEstimatedPriceData {
pub inst_id: String,
pub inst_type: String,
pub settle_px: String,
pub settle_type: WsEstimatedPriceSettleType,
pub ts: String,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn current_websocket_response_fields_deserialize() {
let spread: WsSpreadOrderResult = serde_json::from_value(serde_json::json!({
"sprdId": "BTC-USDT_BTC-USDC",
"ordId": "1",
"clOrdId": "client-1",
"tag": "tag",
"ts": "1",
"sCode": "1",
"subCode": "51008",
"sMsg": "failed"
}))
.unwrap();
let estimated: WsEstimatedPriceData = serde_json::from_value(serde_json::json!({
"instId": "BTC-USD-260925",
"instType": "FUTURES",
"settlePx": "100000",
"settleType": "settlement",
"ts": "1"
}))
.unwrap();
assert_eq!(spread.sub_code.as_deref(), Some("51008"));
assert_eq!(
estimated.settle_type,
WsEstimatedPriceSettleType::Settlement
);
}
}