barter_integration/subscription.rs
1use serde::{Deserialize, Serialize};
2use smol_str::SmolStr;
3use std::fmt::{Display, Formatter};
4
5/// New type representing a unique `String` identifier for a stream that has been subscribed to.
6/// This is used to identify data structures received over the socket.
7///
8/// For example, `Barter-Data` uses this identifier to associate received data structures from the
9/// execution with the original `Barter-Data` `Subscription` that was actioned over the socket.
10///
11/// Note: Each execution will require the use of different `String` identifiers depending on the
12/// data structures they send.
13///
14/// eg/ [`SubscriptionId`] of an `FtxTrade` is "{BASE}/{QUOTE}" (ie/ market).
15/// eg/ [`SubscriptionId`] of a `BinanceTrade` is "{base}{symbol}@trade" (ie/ channel).
16#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize)]
17pub struct SubscriptionId(pub SmolStr);
18
19impl Display for SubscriptionId {
20 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
21 write!(f, "{}", self.0)
22 }
23}
24
25impl AsRef<str> for SubscriptionId {
26 fn as_ref(&self) -> &str {
27 &self.0
28 }
29}
30
31impl<S> From<S> for SubscriptionId
32where
33 S: Into<SmolStr>,
34{
35 fn from(input: S) -> Self {
36 Self(input.into())
37 }
38}