use std::fmt;
use chrono::{DateTime, Utc};
use serde::Deserialize;
use crate::shared::models::price::Price;
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TickerPrice {
ask_price: Price,
bid_price: Price,
min_size: u64,
max_size: u64,
}
impl TickerPrice {
pub fn ask_price(&self) -> Price {
self.ask_price
}
pub fn bid_price(&self) -> Price {
self.bid_price
}
pub fn min_size(&self) -> u64 {
self.min_size
}
pub fn max_size(&self) -> u64 {
self.max_size
}
pub fn as_data_str(&self) -> String {
format!(
"ask_price: {}\nbid_price: {}\nmin_size: {}\nmax_size: {}",
self.ask_price, self.bid_price, self.min_size, self.max_size
)
}
}
impl fmt::Display for TickerPrice {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Ticker Price:")?;
for line in self.as_data_str().lines() {
write!(f, "\n {line}")?;
}
Ok(())
}
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Ticker {
index: Price,
last_price: Price,
prices: Vec<TickerPrice>,
funding_rate: f64,
funding_time: DateTime<Utc>,
}
impl Ticker {
pub fn index(&self) -> Price {
self.index
}
pub fn last_price(&self) -> Price {
self.last_price
}
pub fn prices(&self) -> &[TickerPrice] {
&self.prices
}
pub fn funding_rate(&self) -> f64 {
self.funding_rate
}
pub fn funding_time(&self) -> DateTime<Utc> {
self.funding_time
}
pub fn as_data_str(&self) -> String {
format!(
"index: {}\nlast_price: {}\nfunding_rate: {:.6}\nfunding_time: {}",
self.index,
self.last_price,
self.funding_rate,
self.funding_time.to_rfc3339()
)
}
}
impl fmt::Display for Ticker {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Ticker:")?;
for line in self.as_data_str().lines() {
write!(f, "\n {line}")?;
}
Ok(())
}
}