use std::fmt;
use crate::live::Subscription;
impl Subscription {
#[must_use]
pub fn channel(&self) -> String {
self.parts('/')
}
#[must_use]
pub fn answered_on(&self) -> String {
match self {
Subscription::AccountOrders { market, .. } => format!("account_orders:{market}"),
other => other.parts(':'),
}
}
fn parts(&self, between: char) -> String {
let kind = self.kind();
let all = |market: &Option<crate::utils::MarketId>| match market {
Some(market) => market.to_string(),
None => "all".to_owned(),
};
match self {
Subscription::Height | Subscription::Rfq => kind.to_owned(),
Subscription::OrderBook { market } | Subscription::Ticker { market } | Subscription::Trade { market } => {
format!("{kind}{between}{market}")
}
Subscription::MarketStats { market }
| Subscription::SpotMarketStats { market }
| Subscription::BoMarketStats { market } => format!("{kind}{between}{}", all(market)),
Subscription::Candle { market, resolution } | Subscription::MarkPriceCandle { market, resolution } => {
format!("{kind}{between}{market}{between}{resolution}")
}
Subscription::UpDownCandle { symbol, duration, resolution } => {
format!("{kind}{between}{symbol}{between}{duration}{between}{resolution}")
}
Subscription::AccountMarket { market, account } | Subscription::AccountOrders { market, account } => {
format!("{kind}{between}{market}{between}{account}")
}
Subscription::AccountAll { account }
| Subscription::UserStats { account }
| Subscription::AccountTx { account }
| Subscription::AccountAllOrders { account }
| Subscription::PoolData { account }
| Subscription::PoolInfo { account }
| Subscription::Notification { account }
| Subscription::AccountAllTrades { account }
| Subscription::AccountAllPositions { account }
| Subscription::AccountAllAssets { account }
| Subscription::AccountSpotAvgEntryPrices { account }
| Subscription::LivePoints { account } => format!("{kind}{between}{account}"),
}
}
}
impl fmt::Display for Subscription {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.channel())
}
}