use std::ops::RangeInclusive;
use super::ts_to_dt;
use crate::{Publisher, RType, RecordHeader};
pub trait Record: AsRef<[u8]> {
type Header: RecordHeaderKind;
fn record_size(&self) -> usize;
fn rtype(&self) -> crate::Result<RType>;
fn raw_rtype(&self) -> u16;
fn publisher_id(&self) -> u16;
fn publisher(&self) -> crate::Result<Publisher>;
fn instrument_id(&self) -> u64;
fn raw_ts_event(&self) -> u64;
fn raw_index_ts(&self) -> u64 {
self.raw_ts_event()
}
fn index_ts(&self) -> Option<time::OffsetDateTime> {
ts_to_dt(self.raw_index_ts())
}
fn index_date(&self) -> Option<time::Date> {
self.index_ts().map(|dt| dt.date())
}
}
pub trait RecordMut: Record {
fn header_mut(&mut self) -> &mut Self::Header;
}
pub trait HasRType: Record {
fn has_rtype(rtype: u16) -> bool;
}
pub trait RecordHeaderKind: private::Sealed + Copy + std::fmt::Debug + 'static {
type RType: Copy + Into<u16>;
type InstrumentId: Copy + Into<u64>;
const HEADER_LEN: usize = size_of::<Self>();
const DBN_VERSIONS: RangeInclusive<u8>;
fn frame_len(data: &[u8]) -> crate::Result<usize>;
fn record_size(&self) -> usize;
fn raw_rtype(&self) -> u16;
}
pub trait RecordHeaderMut: RecordHeaderKind {
fn set_rtype(&mut self, rtype: Self::RType);
fn set_publisher_id(&mut self, publisher_id: u16);
fn set_instrument_id(&mut self, instrument_id: Self::InstrumentId);
fn set_ts_event(&mut self, ts_event: u64);
unsafe fn set_record_size(&mut self, size: usize);
}
impl RecordHeaderKind for RecordHeader {
type RType = u8;
type InstrumentId = u32;
const DBN_VERSIONS: RangeInclusive<u8> = 1..=3;
fn frame_len(data: &[u8]) -> crate::Result<usize> {
let length = data[0] as usize * RecordHeader::LENGTH_MULTIPLIER;
if length < Self::HEADER_LEN {
return Err(crate::Error::decode(format!(
"invalid record with impossible length {length} which is shorter than the header"
)));
}
Ok(length)
}
fn record_size(&self) -> usize {
RecordHeader::record_size(self)
}
fn raw_rtype(&self) -> u16 {
self.rtype as u16
}
}
impl RecordHeaderKind for crate::v4::RecordHeader {
type RType = u16;
type InstrumentId = u64;
const DBN_VERSIONS: RangeInclusive<u8> = 4..=4;
fn frame_len(data: &[u8]) -> crate::Result<usize> {
use crate::v4::framing::{is_framing_rtype, FramingHeader};
let header = FramingHeader::from_bytes(data.first_chunk().unwrap());
if is_framing_rtype(header.rtype) {
header.frame_len()
} else {
Ok(Self::HEADER_LEN + usize::from(header.length))
}
}
fn record_size(&self) -> usize {
crate::v4::RecordHeader::record_size(self)
}
fn raw_rtype(&self) -> u16 {
self.rtype
}
}
impl RecordHeaderMut for RecordHeader {
fn set_rtype(&mut self, rtype: u8) {
self.rtype = rtype;
}
fn set_publisher_id(&mut self, publisher_id: u16) {
self.publisher_id = publisher_id;
}
fn set_instrument_id(&mut self, instrument_id: u32) {
self.instrument_id = instrument_id;
}
fn set_ts_event(&mut self, ts_event: u64) {
self.ts_event = ts_event;
}
unsafe fn set_record_size(&mut self, size: usize) {
assert!(
size >= Self::HEADER_LEN && size.is_multiple_of(RecordHeader::LENGTH_MULTIPLIER),
"record size {size} isn't a multiple of {} at least the header size",
RecordHeader::LENGTH_MULTIPLIER
);
self.length = u8::try_from(size / RecordHeader::LENGTH_MULTIPLIER)
.unwrap_or_else(|_| panic!("record size {size} overflows `length`"));
}
}
impl RecordHeaderMut for crate::v4::RecordHeader {
fn set_rtype(&mut self, rtype: u16) {
self.rtype = rtype;
}
fn set_publisher_id(&mut self, publisher_id: u16) {
self.publisher_id = publisher_id;
}
fn set_instrument_id(&mut self, instrument_id: u64) {
self.instrument_id = instrument_id;
}
fn set_ts_event(&mut self, ts_event: u64) {
self.ts_event = ts_event;
}
unsafe fn set_record_size(&mut self, size: usize) {
let body = size
.checked_sub(Self::HEADER_LEN)
.unwrap_or_else(|| panic!("record size {size} is shorter than the header"));
self.length =
u16::try_from(body).unwrap_or_else(|_| panic!("record size {size} overflows `length`"));
}
}
mod private {
pub trait Sealed {}
impl Sealed for super::RecordHeader {}
impl Sealed for crate::v4::RecordHeader {}
}