use std::{ffi::c_char, marker::PhantomData};
use crate::{
v4::{
types::{CStr, Char, Decimal, EnumField, OpenEnum, Symbol, TimestampNs},
RecordHeader,
},
FlagSet,
};
mod registry;
pub use registry::*;
pub(crate) const fn uses_label_def(type_id: u8) -> bool {
matches!(
type_id,
ENUM_CHAR_ID
| ENUM_8_ID
| ENUM_16_ID
| BIT_SET_8_ID
| BIT_SET_16_ID
| BIT_SET_32_ID
| BIT_SET_64_ID
)
}
pub const FLAG_IS_ARRAY: u8 = 1 << 7;
pub const FLAG_IS_EXTENSION: u8 = 1 << 6;
pub trait FieldType: Sized {
const TYPE_ID: u8;
const ARRAY_WIRE_SIZE: Option<u16> = None;
}
pub trait FixedWidth: FieldType {
const WIRE_SIZE: u16 = match scalar_size(Self::TYPE_ID) {
Some(size) => size,
None => panic!("FixedWidth type without a scalar width"),
};
type Storage;
}
pub trait DecodeField: FieldType {
type Value<'a>;
fn decode(bytes: &[u8], scale: i8) -> Option<Self::Value<'_>>;
}
macro_rules! impl_decode_field_le {
($($t:ty),*) => {$(
impl DecodeField for $t {
type Value<'a> = $t;
fn decode(bytes: &[u8], _scale: i8) -> Option<$t> {
Some(<$t>::from_le_bytes(bytes.get(..std::mem::size_of::<$t>())?.try_into().ok()?))
}
}
)*};
}
impl_decode_field_le!(u8, u16, u32, u64, i8, i16, i32, i64, f32, f64);
impl DecodeField for Decimal {
type Value<'a> = Decimal;
fn decode(bytes: &[u8], scale: i8) -> Option<Decimal> {
Some(Decimal {
raw: i64::decode(bytes, scale)?,
scale,
})
}
}
impl DecodeField for TimestampNs {
type Value<'a> = TimestampNs;
fn decode(bytes: &[u8], scale: i8) -> Option<TimestampNs> {
u64::decode(bytes, scale).map(TimestampNs)
}
}
impl DecodeField for Char {
type Value<'a> = Char;
fn decode(bytes: &[u8], scale: i8) -> Option<Char> {
u8::decode(bytes, scale).map(|c| Char(c as c_char))
}
}
impl DecodeField for FlagSet {
type Value<'a> = FlagSet;
fn decode(bytes: &[u8], scale: i8) -> Option<FlagSet> {
u8::decode(bytes, scale).map(FlagSet::new)
}
}
impl DecodeField for CStr {
type Value<'a> = &'a str;
fn decode(bytes: &[u8], _scale: i8) -> Option<&str> {
let end = bytes.iter().position(|&b| b == 0).unwrap_or(bytes.len());
std::str::from_utf8(&bytes[..end]).ok()
}
}
impl DecodeField for Symbol {
type Value<'a> = &'a str;
fn decode(bytes: &[u8], scale: i8) -> Option<&str> {
CStr::decode(bytes, scale)
}
}
impl<E> DecodeField for OpenEnum<E>
where
E: EnumField,
E::Repr: for<'a> DecodeField<Value<'a> = E::Repr>,
{
type Value<'a> = OpenEnum<E>;
fn decode(bytes: &[u8], scale: i8) -> Option<OpenEnum<E>> {
Some(OpenEnum::from_raw(E::Repr::decode(bytes, scale)?))
}
}
#[derive(Debug, Clone, Copy)]
pub struct Field<T: FieldType> {
id: u16,
_type: PhantomData<T>,
}
impl<T: FieldType> Field<T> {
pub const fn new(id: u16) -> Self {
Self {
id,
_type: PhantomData,
}
}
pub const fn id(&self) -> u16 {
self.id
}
pub const fn type_id(&self) -> u8 {
T::TYPE_ID
}
pub const fn flags(&self) -> u8 {
(T::ARRAY_WIRE_SIZE.is_some() as u8) * FLAG_IS_ARRAY
}
}
impl<T: FixedWidth> Field<T> {
pub const fn size(&self) -> u16 {
T::WIRE_SIZE
}
}
pub const PADDING: u16 = 0;
impl FieldType for u8 {
const TYPE_ID: u8 = 0x01;
}
impl FieldType for u16 {
const TYPE_ID: u8 = 0x02;
}
impl FieldType for u32 {
const TYPE_ID: u8 = 0x03;
}
impl FieldType for u64 {
const TYPE_ID: u8 = 0x04;
}
impl FieldType for i8 {
const TYPE_ID: u8 = 0x05;
}
impl FieldType for i16 {
const TYPE_ID: u8 = 0x06;
}
impl FieldType for i32 {
const TYPE_ID: u8 = 0x07;
}
impl FieldType for i64 {
const TYPE_ID: u8 = 0x08;
}
impl FieldType for f32 {
const TYPE_ID: u8 = 0x09;
}
impl FieldType for f64 {
const TYPE_ID: u8 = 0x0A;
}
impl FieldType for Decimal {
const TYPE_ID: u8 = 0x0B;
}
impl FieldType for TimestampNs {
const TYPE_ID: u8 = 0x0C;
}
impl FieldType for Char {
const TYPE_ID: u8 = 0x0D;
}
impl FieldType for CStr {
const TYPE_ID: u8 = CSTR_ID;
}
impl FieldType for Symbol {
const TYPE_ID: u8 = SYMBOL_ID;
}
impl FieldType for RecordHeader {
const TYPE_ID: u8 = STRUCT_ID;
}
impl<T: FixedWidth, const N: usize> FieldType for [T; N] {
const TYPE_ID: u8 = T::TYPE_ID;
const ARRAY_WIRE_SIZE: Option<u16> = Some(T::WIRE_SIZE * N as u16);
}
impl<E> FieldType for OpenEnum<E>
where
E: EnumField,
{
const TYPE_ID: u8 = E::TYPE_ID;
}
pub const ENUM_CHAR_ID: u8 = 0x20;
pub const ENUM_8_ID: u8 = 0x21;
pub const ENUM_16_ID: u8 = 0x22;
pub const BIT_SET_8_ID: u8 = 0x30;
pub const BIT_SET_16_ID: u8 = 0x31;
pub const BIT_SET_32_ID: u8 = 0x32;
pub const BIT_SET_64_ID: u8 = 0x33;
pub const CSTR_ID: u8 = 0x0E;
pub const SYMBOL_ID: u8 = 0x0F;
pub const STRUCT_ID: u8 = 0x40;
pub const PADDING_ID: u8 = 0xFF;
impl FieldType for FlagSet {
const TYPE_ID: u8 = BIT_SET_8_ID;
}
macro_rules! impl_fixed_width {
($($t:ty => $storage:ty),*) => {$(
impl FixedWidth for $t {
type Storage = $storage;
}
)*};
}
impl_fixed_width!(
Decimal => i64,
u8 => u8,
u16 => u16,
u32 => u32,
u64 => u64,
i8 => i8,
i16 => i16,
i32 => i32,
i64 => i64,
f32 => f32,
f64 => f64,
TimestampNs => u64,
Char => c_char,
FlagSet => FlagSet
);
impl<T: FixedWidth, const N: usize> FixedWidth for [T; N] {
const WIRE_SIZE: u16 = T::WIRE_SIZE * N as u16;
type Storage = [T::Storage; N];
}
impl<E: EnumField> FixedWidth for OpenEnum<E> {
type Storage = E::Storage;
}
pub(crate) const fn scalar_size(type_id: u8) -> Option<u16> {
Some(match type_id {
0x01 | 0x05 | 0x0D | ENUM_CHAR_ID | ENUM_8_ID | BIT_SET_8_ID => 1,
0x02 | 0x06 | ENUM_16_ID | BIT_SET_16_ID => 2,
0x03 | 0x07 | 0x09 | BIT_SET_32_ID => 4,
0x04 | 0x08 | 0x0A | 0x0B | 0x0C | BIT_SET_64_ID => 8,
_ => return None,
})
}
#[cfg(test)]
mod tests {
use std::collections::HashSet;
use super::*;
#[test]
fn registry_field_ids_are_unique() {
let ids = [
PADDING,
LENGTH.id(),
RTYPE.id(),
PUBLISHER_ID.id(),
INSTRUMENT_ID.id(),
TS_EVENT.id(),
PRICE.id(),
SIZE.id(),
FLAGS.id(),
ORDER_ID.id(),
CHANNEL_ID.id(),
HD.id(),
];
let mut seen = HashSet::new();
for id in ids {
assert!(seen.insert(id), "duplicate field ID {id:#06X}");
}
}
}