use std::time::{Duration, SystemTime, UNIX_EPOCH};
use crate::packets::decoder::Decoder;
use super::prelude::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CancelMarketOfferPacket {
pub timestamp: SystemTime,
pub offer_counter: u16,
}
impl Decodable for CancelMarketOfferPacket {
const KIND: PacketKind = PacketKind::CancelMarketOffer;
fn decode(mut bytes: &mut &[u8]) -> Result<Self, DecodableError> {
Ok(Self {
timestamp: UNIX_EPOCH + Duration::from_secs(u64::from(bytes.get_u32()?)),
offer_counter: bytes.get_u16()?,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn should_decode_cancel_market_offer() {
let mut payload: &[u8] = &[0x78, 0x56, 0x34, 0x12, 0x34, 0x12];
let packet = CancelMarketOfferPacket::decode(&mut payload)
.expect("CancelMarketOffer packets should decode timestamp and counter");
assert_eq!(
packet
.timestamp
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs(),
0x12345678
);
assert_eq!(packet.offer_counter, 0x1234);
assert!(
payload.is_empty(),
"CancelMarketOffer decoding should consume the whole payload"
);
}
#[test]
fn should_expose_cancel_market_offer_kind_constant() {
assert_eq!(
CancelMarketOfferPacket::KIND,
PacketKind::CancelMarketOffer,
"CancelMarketOffer packets should advertise the correct packet kind"
);
}
}