1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use crate::{exchange::bybit::channel::BybitChannel, Identifier};
use barter_integration::model::SubscriptionId;
use chrono::{DateTime, Utc};
use serde::{
de::{Error, Unexpected},
Deserialize, Serialize,
};
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Deserialize, Serialize)]
pub struct BybitMessage<T> {
#[serde(alias = "topic", deserialize_with = "de_message_subscription_id")]
pub subscription_id: SubscriptionId,
#[serde(rename = "type")]
pub r#type: String,
#[serde(
alias = "ts",
deserialize_with = "barter_integration::de::de_u64_epoch_ms_as_datetime_utc"
)]
pub time: DateTime<Utc>,
pub data: T,
}
pub fn de_message_subscription_id<'de, D>(deserializer: D) -> Result<SubscriptionId, D::Error>
where
D: serde::de::Deserializer<'de>,
{
let input = <&str as serde::Deserialize>::deserialize(deserializer)?;
let mut tokens = input.split('.');
match (tokens.next(), tokens.next(), tokens.next()) {
(Some("publicTrade"), Some(market), None) => Ok(SubscriptionId::from(format!(
"{}|{market}",
BybitChannel::TRADES.0
))),
_ => Err(Error::invalid_value(
Unexpected::Str(input),
&"invalid message type expected pattern: <type>.<symbol>",
)),
}
}
impl<T> Identifier<Option<SubscriptionId>> for BybitMessage<T> {
fn id(&self) -> Option<SubscriptionId> {
Some(self.subscription_id.clone())
}
}