use {
super::router::{Price, PriceFeedId, TimestampUs},
derive_more::derive::From,
serde::{Deserialize, Serialize},
};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PriceFeedData {
pub price_feed_id: PriceFeedId,
pub source_timestamp_us: TimestampUs,
pub publisher_timestamp_us: TimestampUs,
#[serde(with = "crate::serde_price_as_i64")]
pub price: Option<Price>,
#[serde(with = "crate::serde_price_as_i64")]
pub best_bid_price: Option<Price>,
#[serde(with = "crate::serde_price_as_i64")]
pub best_ask_price: Option<Price>,
}
#[derive(Debug, Clone, Serialize, Deserialize, From)]
#[serde(tag = "type")]
#[serde(rename_all = "camelCase")]
pub enum ServerResponse {
UpdateDeserializationError(UpdateDeserializationErrorResponse),
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateDeserializationErrorResponse {
pub error: String,
}
#[test]
fn price_feed_data_serde() {
let data = [
1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 2, 0, 0, 0, 0, 0, 0, ];
let expected = PriceFeedData {
price_feed_id: PriceFeedId(1),
source_timestamp_us: TimestampUs(2),
publisher_timestamp_us: TimestampUs(3),
price: Some(Price(4.try_into().unwrap())),
best_bid_price: Some(Price(5.try_into().unwrap())),
best_ask_price: Some(Price((2 * 256 + 6).try_into().unwrap())),
};
assert_eq!(
bincode::deserialize::<PriceFeedData>(&data).unwrap(),
expected
);
assert_eq!(bincode::serialize(&expected).unwrap(), data);
let data2 = [
1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ];
let expected2 = PriceFeedData {
price_feed_id: PriceFeedId(1),
source_timestamp_us: TimestampUs(2),
publisher_timestamp_us: TimestampUs(3),
price: Some(Price(4.try_into().unwrap())),
best_bid_price: None,
best_ask_price: None,
};
assert_eq!(
bincode::deserialize::<PriceFeedData>(&data2).unwrap(),
expected2
);
assert_eq!(bincode::serialize(&expected2).unwrap(), data2);
}