#![doc = include_str!("../README.md")]
use std::collections::VecDeque;
use std::error::Error;
use std::fmt;
use std::fs::{self, File, OpenOptions};
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
mod session;
pub use session::*;
const FNV_OFFSET_BASIS: u64 = 0xcbf2_9ce4_8422_2325;
const FNV_PRIME: u64 = 0x0000_0100_0000_01b3;
const SEQUENCE_SNAPSHOT_MAGIC: &[u8; 8] = b"OFIXSEQ\0";
const SEQUENCE_SNAPSHOT_VERSION: u16 = 1;
const SEQUENCE_SNAPSHOT_FILE: &str = "fix-sequence.snapshot";
const SEQUENCE_SNAPSHOT_TMP_FILE: &str = "fix-sequence.snapshot.tmp";
const DURABLE_RESEND_MAGIC: &[u8; 8] = b"OFIXRSD\0";
const DURABLE_RESEND_VERSION: u16 = 1;
pub const SOH: u8 = 0x01;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FixTag(pub u32);
impl FixTag {
pub const BEGIN_STRING: Self = Self(8);
pub const ACCOUNT: Self = Self(1);
pub const BODY_LENGTH: Self = Self(9);
pub const BEGIN_SEQ_NO: Self = Self(7);
pub const END_SEQ_NO: Self = Self(16);
pub const MSG_TYPE: Self = Self(35);
pub const MSG_SEQ_NUM: Self = Self(34);
pub const NEW_SEQ_NO: Self = Self(36);
pub const POSS_DUP_FLAG: Self = Self(43);
pub const REF_SEQ_NUM: Self = Self(45);
pub const SENDER_COMP_ID: Self = Self(49);
pub const SENDING_TIME: Self = Self(52);
pub const TARGET_COMP_ID: Self = Self(56);
pub const CL_ORD_ID: Self = Self(11);
pub const ORIG_CL_ORD_ID: Self = Self(41);
pub const ORDER_ID: Self = Self(37);
pub const EXEC_ID: Self = Self(17);
pub const EXEC_TYPE: Self = Self(150);
pub const ORD_STATUS: Self = Self(39);
pub const SYMBOL: Self = Self(55);
pub const SIDE: Self = Self(54);
pub const TRADING_SESSION_ID: Self = Self(336);
pub const ENCODED_TEXT_LEN: Self = Self(354);
pub const ORDER_QTY: Self = Self(38);
pub const ORD_TYPE: Self = Self(40);
pub const PRICE: Self = Self(44);
pub const TIME_IN_FORCE: Self = Self(59);
pub const STOP_PX: Self = Self(99);
pub const LAST_QTY: Self = Self(32);
pub const LAST_PX: Self = Self(31);
pub const CUM_QTY: Self = Self(14);
pub const LEAVES_QTY: Self = Self(151);
pub const AVG_PX: Self = Self(6);
pub const TRANSACT_TIME: Self = Self(60);
pub const TEXT: Self = Self(58);
pub const ENCRYPT_METHOD: Self = Self(98);
pub const TEST_REQ_ID: Self = Self(112);
pub const ORIG_SENDING_TIME: Self = Self(122);
pub const HEART_BT_INT: Self = Self(108);
pub const GAP_FILL_FLAG: Self = Self(123);
pub const RESET_SEQ_NUM_FLAG: Self = Self(141);
pub const REF_TAG_ID: Self = Self(371);
pub const REF_MSG_TYPE: Self = Self(372);
pub const SESSION_REJECT_REASON: Self = Self(373);
pub const BUSINESS_REJECT_REF_ID: Self = Self(379);
pub const BUSINESS_REJECT_REASON: Self = Self(380);
pub const SECONDARY_CL_ORD_ID: Self = Self(526);
pub const MASS_CANCEL_REQUEST_TYPE: Self = Self(530);
pub const MASS_STATUS_REQ_ID: Self = Self(584);
pub const MASS_STATUS_REQ_TYPE: Self = Self(585);
pub const TRADING_SESSION_SUB_ID: Self = Self(625);
pub const ACCT_ID_SOURCE: Self = Self(660);
pub const CHECK_SUM: Self = Self(10);
}
impl fmt::Display for FixTag {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum FixVersion {
Fix40,
Fix41,
Fix42,
Fix43,
Fix44,
FixT11,
}
impl FixVersion {
pub const fn as_bytes(self) -> &'static [u8] {
match self {
Self::Fix40 => b"FIX.4.0",
Self::Fix41 => b"FIX.4.1",
Self::Fix42 => b"FIX.4.2",
Self::Fix43 => b"FIX.4.3",
Self::Fix44 => b"FIX.4.4",
Self::FixT11 => b"FIXT.1.1",
}
}
pub fn from_bytes(value: &[u8]) -> Option<Self> {
match value {
b"FIX.4.0" => Some(Self::Fix40),
b"FIX.4.1" => Some(Self::Fix41),
b"FIX.4.2" => Some(Self::Fix42),
b"FIX.4.3" => Some(Self::Fix43),
b"FIX.4.4" => Some(Self::Fix44),
b"FIXT.1.1" => Some(Self::FixT11),
_ => None,
}
}
}
impl fmt::Display for FixVersion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(std::str::from_utf8(self.as_bytes()).unwrap_or("<invalid>"))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FixMsgType(&'static [u8]);
impl FixMsgType {
pub const HEARTBEAT: Self = Self(b"0");
pub const TEST_REQUEST: Self = Self(b"1");
pub const RESEND_REQUEST: Self = Self(b"2");
pub const REJECT: Self = Self(b"3");
pub const SEQUENCE_RESET: Self = Self(b"4");
pub const LOGOUT: Self = Self(b"5");
pub const EXECUTION_REPORT: Self = Self(b"8");
pub const ORDER_CANCEL_REJECT: Self = Self(b"9");
pub const LOGON: Self = Self(b"A");
pub const NEW_ORDER_SINGLE: Self = Self(b"D");
pub const ORDER_CANCEL_REQUEST: Self = Self(b"F");
pub const ORDER_CANCEL_REPLACE_REQUEST: Self = Self(b"G");
pub const ORDER_STATUS_REQUEST: Self = Self(b"H");
pub const BUSINESS_MESSAGE_REJECT: Self = Self(b"j");
pub const ORDER_MASS_CANCEL_REQUEST: Self = Self(b"q");
pub const ORDER_MASS_STATUS_REQUEST: Self = Self(b"AF");
pub const fn from_static(value: &'static [u8]) -> Self {
Self(value)
}
pub fn from_bytes(value: &[u8]) -> Option<Self> {
match value {
b"0" => Some(Self::HEARTBEAT),
b"1" => Some(Self::TEST_REQUEST),
b"2" => Some(Self::RESEND_REQUEST),
b"3" => Some(Self::REJECT),
b"4" => Some(Self::SEQUENCE_RESET),
b"5" => Some(Self::LOGOUT),
b"8" => Some(Self::EXECUTION_REPORT),
b"9" => Some(Self::ORDER_CANCEL_REJECT),
b"A" => Some(Self::LOGON),
b"D" => Some(Self::NEW_ORDER_SINGLE),
b"F" => Some(Self::ORDER_CANCEL_REQUEST),
b"G" => Some(Self::ORDER_CANCEL_REPLACE_REQUEST),
b"H" => Some(Self::ORDER_STATUS_REQUEST),
b"AF" => Some(Self::ORDER_MASS_STATUS_REQUEST),
b"j" => Some(Self::BUSINESS_MESSAGE_REJECT),
b"q" => Some(Self::ORDER_MASS_CANCEL_REQUEST),
_ => None,
}
}
pub const fn as_bytes(self) -> &'static [u8] {
self.0
}
pub fn name(self) -> &'static str {
match self.0 {
b"0" => "Heartbeat",
b"1" => "TestRequest",
b"2" => "ResendRequest",
b"3" => "Reject",
b"4" => "SequenceReset",
b"5" => "Logout",
b"8" => "ExecutionReport",
b"9" => "OrderCancelReject",
b"A" => "Logon",
b"D" => "NewOrderSingle",
b"F" => "OrderCancelRequest",
b"G" => "OrderCancelReplaceRequest",
b"H" => "OrderStatusRequest",
b"AF" => "OrderMassStatusRequest",
b"j" => "BusinessMessageReject",
b"q" => "OrderMassCancelRequest",
_ => "Custom",
}
}
}
impl fmt::Display for FixMsgType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(std::str::from_utf8(self.0).unwrap_or("<invalid>"))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum FixOrderSide {
Buy,
Sell,
SellShort,
}
impl FixOrderSide {
pub const fn as_bytes(self) -> &'static [u8] {
match self {
Self::Buy => b"1",
Self::Sell => b"2",
Self::SellShort => b"5",
}
}
pub fn from_bytes(value: &[u8]) -> Option<Self> {
match value {
b"1" => Some(Self::Buy),
b"2" => Some(Self::Sell),
b"5" => Some(Self::SellShort),
_ => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum FixOrdType {
Market,
Limit,
Stop,
StopLimit,
}
impl FixOrdType {
pub const fn as_bytes(self) -> &'static [u8] {
match self {
Self::Market => b"1",
Self::Limit => b"2",
Self::Stop => b"3",
Self::StopLimit => b"4",
}
}
pub fn from_bytes(value: &[u8]) -> Option<Self> {
match value {
b"1" => Some(Self::Market),
b"2" => Some(Self::Limit),
b"3" => Some(Self::Stop),
b"4" => Some(Self::StopLimit),
_ => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum FixTimeInForce {
Day,
GoodTillCancel,
ImmediateOrCancel,
FillOrKill,
GoodTillDate,
}
impl FixTimeInForce {
pub const fn as_bytes(self) -> &'static [u8] {
match self {
Self::Day => b"0",
Self::GoodTillCancel => b"1",
Self::ImmediateOrCancel => b"3",
Self::FillOrKill => b"4",
Self::GoodTillDate => b"6",
}
}
pub fn from_bytes(value: &[u8]) -> Option<Self> {
match value {
b"0" => Some(Self::Day),
b"1" => Some(Self::GoodTillCancel),
b"3" => Some(Self::ImmediateOrCancel),
b"4" => Some(Self::FillOrKill),
b"6" => Some(Self::GoodTillDate),
_ => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum FixMassCancelRequestType {
Security,
UnderlyingSecurity,
Product,
CfiCode,
SecurityType,
TradingSession,
AllOrders,
}
impl FixMassCancelRequestType {
pub const fn as_bytes(self) -> &'static [u8] {
match self {
Self::Security => b"1",
Self::UnderlyingSecurity => b"2",
Self::Product => b"3",
Self::CfiCode => b"4",
Self::SecurityType => b"5",
Self::TradingSession => b"6",
Self::AllOrders => b"7",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum FixMassStatusReqType {
Security,
UnderlyingSecurity,
Product,
CfiCode,
SecurityType,
TradingSession,
AllOrders,
PartyId,
}
impl FixMassStatusReqType {
pub const fn as_bytes(self) -> &'static [u8] {
match self {
Self::Security => b"1",
Self::UnderlyingSecurity => b"2",
Self::Product => b"3",
Self::CfiCode => b"4",
Self::SecurityType => b"5",
Self::TradingSession => b"6",
Self::AllOrders => b"7",
Self::PartyId => b"8",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FixFieldView<'a> {
pub tag: FixTag,
pub value: &'a [u8],
}
impl<'a> FixFieldView<'a> {
pub const fn empty() -> Self {
Self {
tag: FixTag(0),
value: &[],
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FixRepeatingGroupDefinition<'a> {
count_tag: FixTag,
delimiter_tag: FixTag,
field_tags: &'a [FixTag],
}
impl<'a> FixRepeatingGroupDefinition<'a> {
pub const fn new(count_tag: FixTag, delimiter_tag: FixTag, field_tags: &'a [FixTag]) -> Self {
Self {
count_tag,
delimiter_tag,
field_tags,
}
}
pub const fn count_tag(self) -> FixTag {
self.count_tag
}
pub const fn delimiter_tag(self) -> FixTag {
self.delimiter_tag
}
pub const fn field_tags(self) -> &'a [FixTag] {
self.field_tags
}
fn allows(self, tag: FixTag) -> bool {
self.field_tags.contains(&tag)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FixRepeatingGroupEntry {
start: usize,
end: usize,
}
impl FixRepeatingGroupEntry {
pub const fn empty() -> Self {
Self { start: 0, end: 0 }
}
pub const fn field_range(self) -> (usize, usize) {
(self.start, self.end)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FixRepeatingGroup<'a> {
fields: &'a [FixFieldView<'a>],
}
impl<'a> FixRepeatingGroup<'a> {
pub const fn fields(self) -> &'a [FixFieldView<'a>] {
self.fields
}
pub fn get(self, tag: FixTag) -> Option<&'a [u8]> {
self.fields
.iter()
.find(|field| field.tag == tag)
.map(|field| field.value)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FixRepeatingGroupView<'a, 'scratch> {
fields: &'a [FixFieldView<'a>],
entries: &'scratch [FixRepeatingGroupEntry],
}
impl<'a, 'scratch> FixRepeatingGroupView<'a, 'scratch> {
pub const fn len(self) -> usize {
self.entries.len()
}
pub const fn is_empty(self) -> bool {
self.entries.is_empty()
}
pub fn get(self, index: usize) -> Option<FixRepeatingGroup<'a>> {
let entry = self.entries.get(index)?;
Some(FixRepeatingGroup {
fields: &self.fields[entry.start..entry.end],
})
}
pub fn iter(&self) -> impl Iterator<Item = FixRepeatingGroup<'a>> + '_ {
self.entries.iter().map(move |entry| FixRepeatingGroup {
fields: &self.fields[entry.start..entry.end],
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FixMessageView<'a> {
raw: &'a [u8],
fields: &'a [FixFieldView<'a>],
}
impl<'a> FixMessageView<'a> {
pub const fn raw(&self) -> &'a [u8] {
self.raw
}
pub const fn fields(&self) -> &'a [FixFieldView<'a>] {
self.fields
}
pub fn get(&self, tag: FixTag) -> Option<&'a [u8]> {
self.fields
.iter()
.find(|field| field.tag == tag)
.map(|field| field.value)
}
pub fn msg_type(&self) -> Option<&'a [u8]> {
self.get(FixTag::MSG_TYPE)
}
pub fn typed_msg_type(&self) -> Option<FixMsgType> {
FixMsgType::from_bytes(self.msg_type()?)
}
pub fn begin_string(&self) -> Option<&'a [u8]> {
self.get(FixTag::BEGIN_STRING)
}
pub fn version(&self) -> Option<FixVersion> {
FixVersion::from_bytes(self.begin_string()?)
}
pub fn msg_seq_num(&self) -> Option<u64> {
parse_u64(self.get(FixTag::MSG_SEQ_NUM)?).ok()
}
pub fn poss_dup(&self) -> bool {
self.get(FixTag::POSS_DUP_FLAG) == Some(b"Y".as_slice())
}
pub fn gap_fill(&self) -> bool {
self.get(FixTag::GAP_FILL_FLAG) == Some(b"Y".as_slice())
}
pub fn new_seq_no(&self) -> Option<u64> {
parse_u64(self.get(FixTag::NEW_SEQ_NO)?).ok()
}
pub fn begin_seq_no(&self) -> Option<u64> {
parse_u64(self.get(FixTag::BEGIN_SEQ_NO)?).ok()
}
pub fn end_seq_no(&self) -> Option<u64> {
parse_u64(self.get(FixTag::END_SEQ_NO)?).ok()
}
pub fn debug_render(&self) -> String {
debug_render(self.raw)
}
pub fn repeating_group<'scratch>(
&self,
definition: FixRepeatingGroupDefinition<'_>,
scratch: &'scratch mut [FixRepeatingGroupEntry],
) -> Result<FixRepeatingGroupView<'a, 'scratch>, FixGroupError> {
let count_index = self
.fields
.iter()
.position(|field| field.tag == definition.count_tag)
.ok_or(FixGroupError::MissingCountTag(definition.count_tag))?;
let count_value = self.fields[count_index].value;
let count = parse_usize(count_value)
.map_err(|_| FixGroupError::InvalidCount(definition.count_tag))?;
if count > scratch.len() {
return Err(FixGroupError::ScratchTooSmall {
required: count,
capacity: scratch.len(),
});
}
let mut cursor = count_index.saturating_add(1);
for (index, entry) in scratch.iter_mut().take(count).enumerate() {
if cursor >= self.fields.len() || self.fields[cursor].tag != definition.delimiter_tag {
return Err(FixGroupError::MissingDelimiter {
index,
tag: definition.delimiter_tag,
});
}
let start = cursor;
cursor = cursor.saturating_add(1);
while cursor < self.fields.len() {
let field = self.fields[cursor];
if field.tag == definition.delimiter_tag {
break;
}
if !definition.allows(field.tag) {
break;
}
cursor = cursor.saturating_add(1);
}
*entry = FixRepeatingGroupEntry { start, end: cursor };
}
for (index, field) in self.fields.iter().enumerate().skip(cursor) {
if definition.allows(field.tag) {
return Err(FixGroupError::UnexpectedField {
index,
tag: field.tag,
});
}
}
Ok(FixRepeatingGroupView {
fields: self.fields,
entries: &scratch[..count],
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum FixGroupError {
MissingCountTag(FixTag),
InvalidCount(FixTag),
ScratchTooSmall {
required: usize,
capacity: usize,
},
MissingDelimiter {
index: usize,
tag: FixTag,
},
UnexpectedField {
index: usize,
tag: FixTag,
},
}
impl fmt::Display for FixGroupError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::MissingCountTag(tag) => {
write!(f, "FIX repeating-group count tag {tag} is missing")
}
Self::InvalidCount(tag) => write!(f, "FIX repeating-group count tag {tag} is invalid"),
Self::ScratchTooSmall { required, capacity } => write!(
f,
"FIX repeating-group scratch too small: required {required}, capacity {capacity}"
),
Self::MissingDelimiter { index, tag } => {
write!(
f,
"FIX repeating-group entry {index} is missing delimiter tag {tag}"
)
}
Self::UnexpectedField { index, tag } => {
write!(
f,
"FIX repeating-group field {index} has unexpected tag {tag}"
)
}
}
}
}
impl Error for FixGroupError {}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum FixParseError {
Empty,
MalformedField,
InvalidTag,
MissingRequiredTag(FixTag),
ScratchTooSmall {
required: usize,
capacity: usize,
},
InvalidBodyLength,
BodyLengthMismatch {
expected: usize,
actual: usize,
},
InvalidChecksum,
ChecksumMismatch {
expected: u8,
actual: u8,
},
}
impl fmt::Display for FixParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Empty => write!(f, "FIX frame is empty"),
Self::MalformedField => write!(f, "FIX field is malformed"),
Self::InvalidTag => write!(f, "FIX field tag is invalid"),
Self::MissingRequiredTag(tag) => write!(f, "required FIX tag {tag} is missing"),
Self::ScratchTooSmall { required, capacity } => write!(
f,
"FIX parse scratch too small: required {required}, capacity {capacity}"
),
Self::InvalidBodyLength => write!(f, "FIX BodyLength(9) is invalid"),
Self::BodyLengthMismatch { expected, actual } => write!(
f,
"FIX BodyLength(9) mismatch: expected {expected}, actual {actual}"
),
Self::InvalidChecksum => write!(f, "FIX CheckSum(10) is invalid"),
Self::ChecksumMismatch { expected, actual } => write!(
f,
"FIX CheckSum(10) mismatch: expected {expected:03}, actual {actual:03}"
),
}
}
}
impl Error for FixParseError {}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum FixEncodeError {
ValueContainsSoh(FixTag),
ReservedTag(FixTag),
MissingRequiredTag(FixTag),
DuplicateRepeatingGroupCountTag(FixTag),
MissingRepeatingGroupDelimiter {
group: usize,
tag: FixTag,
},
InvalidRepeatingGroupField {
group: usize,
index: usize,
tag: FixTag,
},
RepeatedRepeatingGroupDelimiter {
group: usize,
index: usize,
tag: FixTag,
},
}
impl fmt::Display for FixEncodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ValueContainsSoh(tag) => write!(f, "FIX value for tag {tag} contains SOH"),
Self::ReservedTag(tag) => write!(f, "FIX tag {tag} is owned by the encoder"),
Self::MissingRequiredTag(tag) => write!(f, "FIX source message is missing tag {tag}"),
Self::DuplicateRepeatingGroupCountTag(tag) => {
write!(
f,
"FIX repeating-group count tag {tag} was supplied more than once"
)
}
Self::MissingRepeatingGroupDelimiter { group, tag } => write!(
f,
"FIX repeating-group entry {group} is missing delimiter tag {tag}"
),
Self::InvalidRepeatingGroupField { group, index, tag } => write!(
f,
"FIX repeating-group entry {group} field {index} has invalid tag {tag}"
),
Self::RepeatedRepeatingGroupDelimiter { group, index, tag } => write!(
f,
"FIX repeating-group entry {group} field {index} repeats delimiter tag {tag}"
),
}
}
}
impl Error for FixEncodeError {}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum FixProfileError {
MissingBeginString,
UnsupportedVersion,
VersionMismatch {
expected: FixVersion,
actual: FixVersion,
},
MissingMsgType,
UnsupportedMsgType,
MissingRequiredTag {
msg_type: FixMsgType,
tag: FixTag,
},
DisallowedTag {
msg_type: FixMsgType,
tag: FixTag,
},
}
impl fmt::Display for FixProfileError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::MissingBeginString => write!(f, "FIX BeginString(8) is missing"),
Self::UnsupportedVersion => write!(f, "FIX BeginString(8) is unsupported"),
Self::VersionMismatch { expected, actual } => {
write!(
f,
"FIX version mismatch: expected {expected}, actual {actual}"
)
}
Self::MissingMsgType => write!(f, "FIX MsgType(35) is missing"),
Self::UnsupportedMsgType => write!(f, "FIX MsgType(35) is unsupported"),
Self::MissingRequiredTag { msg_type, tag } => {
write!(f, "FIX message {msg_type} is missing required tag {tag}")
}
Self::DisallowedTag { msg_type, tag } => {
write!(f, "FIX message {msg_type} contains disallowed tag {tag}")
}
}
}
}
impl Error for FixProfileError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum FixRejectParseError {
InvalidMsgType,
MissingTag(FixTag),
InvalidNumber(FixTag),
}
impl fmt::Display for FixRejectParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidMsgType => write!(f, "FIX message type is not a reject message"),
Self::MissingTag(tag) => write!(f, "FIX reject message is missing tag {tag}"),
Self::InvalidNumber(tag) => write!(f, "FIX reject numeric tag {tag} is invalid"),
}
}
}
impl Error for FixRejectParseError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FixSessionRejectView<'a> {
ref_seq_num: u64,
ref_tag_id: Option<FixTag>,
ref_msg_type: Option<&'a [u8]>,
session_reject_reason: Option<u64>,
text: Option<&'a [u8]>,
}
impl<'a> FixSessionRejectView<'a> {
pub const fn ref_seq_num(&self) -> u64 {
self.ref_seq_num
}
pub const fn ref_tag_id(&self) -> Option<FixTag> {
self.ref_tag_id
}
pub const fn ref_msg_type(&self) -> Option<&'a [u8]> {
self.ref_msg_type
}
pub const fn session_reject_reason(&self) -> Option<u64> {
self.session_reject_reason
}
pub const fn text(&self) -> Option<&'a [u8]> {
self.text
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FixBusinessMessageRejectView<'a> {
ref_seq_num: Option<u64>,
ref_msg_type: &'a [u8],
business_reject_ref_id: Option<&'a [u8]>,
business_reject_reason: u64,
text: Option<&'a [u8]>,
}
impl<'a> FixBusinessMessageRejectView<'a> {
pub const fn ref_seq_num(&self) -> Option<u64> {
self.ref_seq_num
}
pub const fn ref_msg_type(&self) -> &'a [u8] {
self.ref_msg_type
}
pub const fn business_reject_ref_id(&self) -> Option<&'a [u8]> {
self.business_reject_ref_id
}
pub const fn business_reject_reason(&self) -> u64 {
self.business_reject_reason
}
pub const fn text(&self) -> Option<&'a [u8]> {
self.text
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum FixSessionState {
Disconnected,
Connecting,
LogonSent,
Ready,
ResendRequested,
Recovering,
LogoutSent,
Stopped,
Degraded,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FixResendRange {
pub begin_seq_no: u64,
pub end_seq_no: u64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum FixSequenceAction {
Accept {
seq_no: u64,
},
Duplicate {
seq_no: u64,
expected: u64,
},
Gap {
expected: u64,
received: u64,
resend: FixResendRange,
},
TooLow {
expected: u64,
received: u64,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum FixSequenceError {
MissingMsgSeqNum,
ZeroSeqNo,
SequenceResetWouldDecrease {
current: u64,
requested: u64,
},
}
impl fmt::Display for FixSequenceError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::MissingMsgSeqNum => write!(f, "FIX MsgSeqNum(34) is missing"),
Self::ZeroSeqNo => write!(f, "FIX sequence number must be greater than zero"),
Self::SequenceResetWouldDecrease { current, requested } => write!(
f,
"FIX sequence reset would decrease next inbound sequence: current {current}, requested {requested}"
),
}
}
}
impl Error for FixSequenceError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FixSessionId<'a> {
version: FixVersion,
sender_comp_id: &'a [u8],
target_comp_id: &'a [u8],
qualifier: &'a [u8],
}
impl<'a> FixSessionId<'a> {
pub fn new(
version: FixVersion,
sender_comp_id: &'a [u8],
target_comp_id: &'a [u8],
) -> Result<Self, FixEncodeError> {
Self::with_qualifier(version, sender_comp_id, target_comp_id, b"")
}
pub fn with_qualifier(
version: FixVersion,
sender_comp_id: &'a [u8],
target_comp_id: &'a [u8],
qualifier: &'a [u8],
) -> Result<Self, FixEncodeError> {
validate_value(FixTag::SENDER_COMP_ID, sender_comp_id)?;
validate_value(FixTag::TARGET_COMP_ID, target_comp_id)?;
validate_value(FixTag::TEXT, qualifier)?;
Ok(Self {
version,
sender_comp_id,
target_comp_id,
qualifier,
})
}
pub const fn version(&self) -> FixVersion {
self.version
}
pub const fn sender_comp_id(&self) -> &'a [u8] {
self.sender_comp_id
}
pub const fn target_comp_id(&self) -> &'a [u8] {
self.target_comp_id
}
pub const fn qualifier(&self) -> &'a [u8] {
self.qualifier
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FixSequenceSnapshot<'a> {
session_id: FixSessionId<'a>,
next_inbound: u64,
next_outbound: u64,
trading_day: &'a [u8],
}
impl<'a> FixSequenceSnapshot<'a> {
pub fn new(
session_id: FixSessionId<'a>,
next_inbound: u64,
next_outbound: u64,
trading_day: &'a [u8],
) -> Result<Self, FixEncodeError> {
validate_value(FixTag::TEXT, trading_day)?;
Ok(Self {
session_id,
next_inbound: clamp_seq_no(next_inbound),
next_outbound: clamp_seq_no(next_outbound),
trading_day,
})
}
pub const fn session_id(&self) -> FixSessionId<'a> {
self.session_id
}
pub const fn next_inbound(&self) -> u64 {
self.next_inbound
}
pub const fn next_outbound(&self) -> u64 {
self.next_outbound
}
pub const fn trading_day(&self) -> &'a [u8] {
self.trading_day
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct FixOwnedSessionId {
version: FixVersion,
sender_comp_id: Vec<u8>,
target_comp_id: Vec<u8>,
qualifier: Vec<u8>,
}
impl FixOwnedSessionId {
pub fn new(
version: FixVersion,
sender_comp_id: impl Into<Vec<u8>>,
target_comp_id: impl Into<Vec<u8>>,
) -> Result<Self, FixEncodeError> {
Self::with_qualifier(version, sender_comp_id, target_comp_id, Vec::new())
}
pub fn with_qualifier(
version: FixVersion,
sender_comp_id: impl Into<Vec<u8>>,
target_comp_id: impl Into<Vec<u8>>,
qualifier: impl Into<Vec<u8>>,
) -> Result<Self, FixEncodeError> {
let sender_comp_id = sender_comp_id.into();
let target_comp_id = target_comp_id.into();
let qualifier = qualifier.into();
validate_value(FixTag::SENDER_COMP_ID, &sender_comp_id)?;
validate_value(FixTag::TARGET_COMP_ID, &target_comp_id)?;
validate_value(FixTag::TEXT, &qualifier)?;
Ok(Self {
version,
sender_comp_id,
target_comp_id,
qualifier,
})
}
pub fn from_borrowed(session_id: FixSessionId<'_>) -> Self {
Self {
version: session_id.version(),
sender_comp_id: session_id.sender_comp_id().to_vec(),
target_comp_id: session_id.target_comp_id().to_vec(),
qualifier: session_id.qualifier().to_vec(),
}
}
pub fn as_borrowed(&self) -> Result<FixSessionId<'_>, FixEncodeError> {
FixSessionId::with_qualifier(
self.version,
&self.sender_comp_id,
&self.target_comp_id,
&self.qualifier,
)
}
pub const fn version(&self) -> FixVersion {
self.version
}
pub fn sender_comp_id(&self) -> &[u8] {
&self.sender_comp_id
}
pub fn target_comp_id(&self) -> &[u8] {
&self.target_comp_id
}
pub fn qualifier(&self) -> &[u8] {
&self.qualifier
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct FixOwnedSequenceSnapshot {
session_id: FixOwnedSessionId,
next_inbound: u64,
next_outbound: u64,
trading_day: Vec<u8>,
checksum: u64,
}
impl FixOwnedSequenceSnapshot {
pub fn new(
session_id: FixOwnedSessionId,
next_inbound: u64,
next_outbound: u64,
trading_day: impl Into<Vec<u8>>,
) -> Result<Self, FixEncodeError> {
let trading_day = trading_day.into();
validate_value(FixTag::TEXT, &trading_day)?;
let mut snapshot = Self {
session_id,
next_inbound: clamp_seq_no(next_inbound),
next_outbound: clamp_seq_no(next_outbound),
trading_day,
checksum: 0,
};
snapshot.checksum = sequence_snapshot_checksum_owned(&snapshot);
Ok(snapshot)
}
pub fn from_borrowed(snapshot: &FixSequenceSnapshot<'_>) -> Self {
let mut owned = Self {
session_id: FixOwnedSessionId::from_borrowed(snapshot.session_id()),
next_inbound: snapshot.next_inbound(),
next_outbound: snapshot.next_outbound(),
trading_day: snapshot.trading_day().to_vec(),
checksum: 0,
};
owned.checksum = sequence_snapshot_checksum_owned(&owned);
owned
}
pub fn as_borrowed(&self) -> Result<FixSequenceSnapshot<'_>, FixEncodeError> {
FixSequenceSnapshot::new(
self.session_id.as_borrowed()?,
self.next_inbound,
self.next_outbound,
&self.trading_day,
)
}
pub const fn session_id(&self) -> &FixOwnedSessionId {
&self.session_id
}
pub const fn next_inbound(&self) -> u64 {
self.next_inbound
}
pub const fn next_outbound(&self) -> u64 {
self.next_outbound
}
pub fn trading_day(&self) -> &[u8] {
&self.trading_day
}
pub const fn checksum(&self) -> u64 {
self.checksum
}
pub fn validate_checksum(&self) -> bool {
self.checksum == sequence_snapshot_checksum_owned(self)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct FixSequenceStoreConfig {
root: PathBuf,
sync_on_save: bool,
}
impl FixSequenceStoreConfig {
pub fn new(root: impl Into<PathBuf>) -> Self {
Self {
root: root.into(),
sync_on_save: true,
}
}
pub const fn with_sync_on_save(mut self, sync_on_save: bool) -> Self {
self.sync_on_save = sync_on_save;
self
}
pub fn root(&self) -> &Path {
&self.root
}
pub const fn sync_on_save(&self) -> bool {
self.sync_on_save
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct FixSequenceSnapshotManifest {
pub path: PathBuf,
pub bytes: u64,
pub checksum: u64,
pub next_inbound: u64,
pub next_outbound: u64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum FixSequenceStoreError {
Io(String),
Encode(FixEncodeError),
InvalidMagic,
UnsupportedVersion(u16),
Truncated,
InvalidVersion,
FieldTooLarge,
ChecksumMismatch {
expected: u64,
actual: u64,
},
}
impl fmt::Display for FixSequenceStoreError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Io(err) => write!(f, "FIX sequence store I/O error: {err}"),
Self::Encode(err) => write!(f, "FIX sequence store encode error: {err}"),
Self::InvalidMagic => write!(f, "invalid FIX sequence snapshot magic"),
Self::UnsupportedVersion(version) => {
write!(f, "unsupported FIX sequence snapshot version {version}")
}
Self::Truncated => write!(f, "truncated FIX sequence snapshot"),
Self::InvalidVersion => write!(f, "invalid FIX begin string in sequence snapshot"),
Self::FieldTooLarge => write!(f, "FIX sequence snapshot field is too large"),
Self::ChecksumMismatch { expected, actual } => write!(
f,
"FIX sequence snapshot checksum mismatch: expected {expected}, actual {actual}"
),
}
}
}
impl Error for FixSequenceStoreError {}
impl From<FixEncodeError> for FixSequenceStoreError {
fn from(value: FixEncodeError) -> Self {
Self::Encode(value)
}
}
pub trait FixSequenceSnapshotStore {
fn save_snapshot(
&mut self,
snapshot: &FixSequenceSnapshot<'_>,
) -> Result<FixSequenceSnapshotManifest, FixSequenceStoreError>;
fn load_latest(&self) -> Result<Option<FixOwnedSequenceSnapshot>, FixSequenceStoreError>;
}
#[derive(Debug, Clone)]
pub struct FileFixSequenceSnapshotStore {
config: FixSequenceStoreConfig,
}
impl FileFixSequenceSnapshotStore {
pub fn open(config: FixSequenceStoreConfig) -> Result<Self, FixSequenceStoreError> {
fs::create_dir_all(config.root()).map_err(io_error)?;
Ok(Self { config })
}
pub const fn config(&self) -> &FixSequenceStoreConfig {
&self.config
}
pub fn snapshot_path(&self) -> PathBuf {
self.config.root().join(SEQUENCE_SNAPSHOT_FILE)
}
fn temp_path(&self) -> PathBuf {
self.config.root().join(SEQUENCE_SNAPSHOT_TMP_FILE)
}
}
impl FixSequenceSnapshotStore for FileFixSequenceSnapshotStore {
fn save_snapshot(
&mut self,
snapshot: &FixSequenceSnapshot<'_>,
) -> Result<FixSequenceSnapshotManifest, FixSequenceStoreError> {
let owned = FixOwnedSequenceSnapshot::from_borrowed(snapshot);
let bytes = encode_sequence_snapshot(&owned)?;
let checksum = owned.checksum();
let final_path = self.snapshot_path();
let tmp_path = self.temp_path();
{
let mut file = OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open(&tmp_path)
.map_err(io_error)?;
file.write_all(&bytes).map_err(io_error)?;
file.flush().map_err(io_error)?;
if self.config.sync_on_save() {
file.sync_all().map_err(io_error)?;
}
}
fs::rename(&tmp_path, &final_path).map_err(io_error)?;
Ok(FixSequenceSnapshotManifest {
path: final_path,
bytes: usize_to_u64(bytes.len()),
checksum,
next_inbound: owned.next_inbound(),
next_outbound: owned.next_outbound(),
})
}
fn load_latest(&self) -> Result<Option<FixOwnedSequenceSnapshot>, FixSequenceStoreError> {
let path = self.snapshot_path();
if !path.exists() {
return Ok(None);
}
let mut file = File::open(path).map_err(io_error)?;
let mut bytes = Vec::new();
file.read_to_end(&mut bytes).map_err(io_error)?;
decode_sequence_snapshot(&bytes).map(Some)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum FixSentMessageKind {
Application,
Administrative,
Reject,
}
impl FixSentMessageKind {
pub const fn replayable(self) -> bool {
matches!(self, Self::Application | Self::Reject)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FixResendStoreConfig {
max_messages: usize,
max_bytes: usize,
}
impl FixResendStoreConfig {
pub const fn new(max_messages: usize, max_bytes: usize) -> Self {
Self {
max_messages,
max_bytes,
}
}
pub const fn max_messages(&self) -> usize {
self.max_messages
}
pub const fn max_bytes(&self) -> usize {
self.max_bytes
}
}
impl Default for FixResendStoreConfig {
fn default() -> Self {
Self {
max_messages: 1024,
max_bytes: 1024 * 1024,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum FixResendStoreError {
ZeroSeqNo,
SequenceRegression {
latest: u64,
received: u64,
},
}
impl fmt::Display for FixResendStoreError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ZeroSeqNo => write!(
f,
"FIX resend store sequence number must be greater than zero"
),
Self::SequenceRegression { latest, received } => write!(
f,
"FIX resend store sequence regression: latest {latest}, received {received}"
),
}
}
}
impl Error for FixResendStoreError {}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct FixDurableResendStoreConfig {
path: PathBuf,
sync_on_record: bool,
}
impl FixDurableResendStoreConfig {
pub fn new(path: impl Into<PathBuf>) -> Self {
Self {
path: path.into(),
sync_on_record: true,
}
}
pub const fn with_sync_on_record(mut self, sync_on_record: bool) -> Self {
self.sync_on_record = sync_on_record;
self
}
pub fn path(&self) -> &Path {
&self.path
}
pub const fn sync_on_record(&self) -> bool {
self.sync_on_record
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct FixDurableResendAppend {
pub seq_no: u64,
pub kind: FixSentMessageKind,
pub offset: u64,
pub bytes: u64,
pub checksum: u64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub struct FixDurableResendReplayReport {
pub records: u64,
pub bytes: u64,
pub first_seq_no: Option<u64>,
pub last_seq_no: Option<u64>,
pub checksum: u64,
pub retained_messages: u64,
pub dropped_messages: u64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum FixDurableResendStoreError {
Io(String),
ResendStore(FixResendStoreError),
InvalidMagic,
UnsupportedVersion(u16),
Truncated,
InvalidKind(u8),
FrameTooLarge,
SequenceRegression {
latest: u64,
received: u64,
},
RawHashMismatch {
expected: u64,
actual: u64,
},
PreviousChecksumMismatch {
expected: u64,
actual: u64,
},
FrameChecksumMismatch {
expected: u64,
actual: u64,
},
}
impl fmt::Display for FixDurableResendStoreError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Io(err) => write!(f, "FIX durable resend store I/O error: {err}"),
Self::ResendStore(err) => write!(f, "FIX durable resend replay error: {err}"),
Self::InvalidMagic => write!(f, "invalid FIX durable resend frame magic"),
Self::UnsupportedVersion(version) => {
write!(f, "unsupported FIX durable resend frame version {version}")
}
Self::Truncated => write!(f, "truncated FIX durable resend frame"),
Self::InvalidKind(kind) => write!(f, "invalid FIX durable resend kind {kind}"),
Self::FrameTooLarge => write!(f, "FIX durable resend frame is too large"),
Self::SequenceRegression { latest, received } => write!(
f,
"FIX durable resend sequence regression: latest {latest}, received {received}"
),
Self::RawHashMismatch { expected, actual } => write!(
f,
"FIX durable resend raw hash mismatch: expected {expected}, actual {actual}"
),
Self::PreviousChecksumMismatch { expected, actual } => write!(
f,
"FIX durable resend previous checksum mismatch: expected {expected}, actual {actual}"
),
Self::FrameChecksumMismatch { expected, actual } => write!(
f,
"FIX durable resend frame checksum mismatch: expected {expected}, actual {actual}"
),
}
}
}
impl Error for FixDurableResendStoreError {}
impl From<FixResendStoreError> for FixDurableResendStoreError {
fn from(value: FixResendStoreError) -> Self {
Self::ResendStore(value)
}
}
pub trait FixDurableResendMessageStore {
fn record_sent(
&mut self,
seq_no: u64,
kind: FixSentMessageKind,
raw: &[u8],
) -> Result<FixDurableResendAppend, FixDurableResendStoreError>;
fn load_into(
&self,
target: &mut FixResendStore,
) -> Result<FixDurableResendReplayReport, FixDurableResendStoreError>;
}
#[derive(Debug)]
pub struct FileFixDurableResendStore {
config: FixDurableResendStoreConfig,
file: File,
scratch: Vec<u8>,
records: u64,
bytes: u64,
newest_seq_no: Option<u64>,
previous_checksum: u64,
}
impl FileFixDurableResendStore {
pub fn open(config: FixDurableResendStoreConfig) -> Result<Self, FixDurableResendStoreError> {
if let Some(parent) = config.path().parent() {
if !parent.as_os_str().is_empty() {
fs::create_dir_all(parent).map_err(durable_resend_io_error)?;
}
}
let report = inspect_durable_resend_path(config.path())?;
let mut file = OpenOptions::new()
.create(true)
.append(true)
.read(true)
.open(config.path())
.map_err(durable_resend_io_error)?;
file.seek(SeekFrom::End(0))
.map_err(durable_resend_io_error)?;
Ok(Self {
config,
file,
scratch: Vec::with_capacity(512),
records: report.records,
bytes: report.bytes,
newest_seq_no: report.last_seq_no,
previous_checksum: report.checksum,
})
}
pub const fn config(&self) -> &FixDurableResendStoreConfig {
&self.config
}
pub fn inspect_path(
path: impl AsRef<Path>,
) -> Result<FixDurableResendReplayReport, FixDurableResendStoreError> {
inspect_durable_resend_path(path.as_ref())
}
}
impl FixDurableResendMessageStore for FileFixDurableResendStore {
fn record_sent(
&mut self,
seq_no: u64,
kind: FixSentMessageKind,
raw: &[u8],
) -> Result<FixDurableResendAppend, FixDurableResendStoreError> {
if seq_no == 0 {
return Err(FixDurableResendStoreError::ResendStore(
FixResendStoreError::ZeroSeqNo,
));
}
if let Some(latest) = self.newest_seq_no {
if seq_no <= latest {
return Err(FixDurableResendStoreError::SequenceRegression {
latest,
received: seq_no,
});
}
}
let offset = self.bytes;
encode_durable_resend_record(&mut self.scratch, seq_no, kind, raw, self.previous_checksum)?;
self.file
.write_all(&self.scratch)
.map_err(durable_resend_io_error)?;
self.file.flush().map_err(durable_resend_io_error)?;
if self.config.sync_on_record() {
self.file.sync_data().map_err(durable_resend_io_error)?;
}
let bytes = usize_to_u64(self.scratch.len());
let checksum = durable_resend_frame_checksum(seq_no, kind, raw, self.previous_checksum);
self.records = self.records.saturating_add(1);
self.bytes = self.bytes.saturating_add(bytes);
self.newest_seq_no = Some(seq_no);
self.previous_checksum = checksum;
Ok(FixDurableResendAppend {
seq_no,
kind,
offset,
bytes,
checksum,
})
}
fn load_into(
&self,
target: &mut FixResendStore,
) -> Result<FixDurableResendReplayReport, FixDurableResendStoreError> {
let records = read_durable_resend_records(self.config.path())?;
let mut report = FixDurableResendReplayReport::default();
for record in records {
report.records = report.records.saturating_add(1);
report.bytes = report.bytes.saturating_add(record.encoded_bytes);
report.first_seq_no.get_or_insert(record.seq_no);
report.last_seq_no = Some(record.seq_no);
report.checksum = record.checksum;
target.record_sent(record.seq_no, record.kind, &record.raw)?;
}
let metrics = target.metrics();
report.retained_messages = metrics.retained_messages();
report.dropped_messages = metrics.dropped_messages();
Ok(report)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FixStoredMessage {
seq_no: u64,
kind: FixSentMessageKind,
raw: Vec<u8>,
}
impl FixStoredMessage {
pub const fn seq_no(&self) -> u64 {
self.seq_no
}
pub const fn kind(&self) -> FixSentMessageKind {
self.kind
}
pub fn raw(&self) -> &[u8] {
&self.raw
}
pub const fn replayable(&self) -> bool {
self.kind.replayable()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FixResendRetention {
retained: bool,
evicted_messages: u64,
evicted_bytes: u64,
}
impl FixResendRetention {
pub const fn retained(&self) -> bool {
self.retained
}
pub const fn evicted_messages(&self) -> u64 {
self.evicted_messages
}
pub const fn evicted_bytes(&self) -> u64 {
self.evicted_bytes
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FixResendStoreMetrics {
retained_messages: u64,
retained_bytes: u64,
dropped_messages: u64,
dropped_bytes: u64,
evicted_messages: u64,
evicted_bytes: u64,
oldest_seq_no: Option<u64>,
newest_seq_no: Option<u64>,
}
impl FixResendStoreMetrics {
pub const fn retained_messages(&self) -> u64 {
self.retained_messages
}
pub const fn retained_bytes(&self) -> u64 {
self.retained_bytes
}
pub const fn dropped_messages(&self) -> u64 {
self.dropped_messages
}
pub const fn dropped_bytes(&self) -> u64 {
self.dropped_bytes
}
pub const fn evicted_messages(&self) -> u64 {
self.evicted_messages
}
pub const fn evicted_bytes(&self) -> u64 {
self.evicted_bytes
}
pub const fn oldest_seq_no(&self) -> Option<u64> {
self.oldest_seq_no
}
pub const fn newest_seq_no(&self) -> Option<u64> {
self.newest_seq_no
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum FixResendAction<'a> {
Replay {
seq_no: u64,
raw: &'a [u8],
},
GapFill {
begin_seq_no: u64,
end_seq_no: u64,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FixResendPlanSummary {
replay_messages: u64,
gap_fill_messages: u64,
gap_fill_sequences: u64,
}
impl FixResendPlanSummary {
pub const fn replay_messages(&self) -> u64 {
self.replay_messages
}
pub const fn gap_fill_messages(&self) -> u64 {
self.gap_fill_messages
}
pub const fn gap_fill_sequences(&self) -> u64 {
self.gap_fill_sequences
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum FixTranscriptDirection {
Inbound,
Outbound,
}
impl FixTranscriptDirection {
const fn as_byte(self) -> u8 {
match self {
Self::Inbound => b'I',
Self::Outbound => b'O',
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FixTranscriptMsgType {
len: u8,
bytes: [u8; 8],
}
impl FixTranscriptMsgType {
pub const fn empty() -> Self {
Self {
len: 0,
bytes: [0; 8],
}
}
pub fn new(value: &[u8]) -> Result<Self, FixTranscriptError> {
if value.len() > 8 {
return Err(FixTranscriptError::MsgTypeTooLong {
capacity: 8,
actual: value.len(),
});
}
let mut bytes = [0u8; 8];
bytes[..value.len()].copy_from_slice(value);
Ok(Self {
len: value.len() as u8,
bytes,
})
}
pub fn as_bytes(&self) -> &[u8] {
&self.bytes[..self.len as usize]
}
pub const fn is_empty(&self) -> bool {
self.len == 0
}
}
impl Default for FixTranscriptMsgType {
fn default() -> Self {
Self::empty()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FixTranscriptConfig {
max_records: usize,
max_raw_bytes: usize,
retain_raw: bool,
}
impl FixTranscriptConfig {
pub const fn new(max_records: usize, max_raw_bytes: usize, retain_raw: bool) -> Self {
Self {
max_records,
max_raw_bytes,
retain_raw,
}
}
pub const fn max_records(&self) -> usize {
self.max_records
}
pub const fn max_raw_bytes(&self) -> usize {
self.max_raw_bytes
}
pub const fn retain_raw(&self) -> bool {
self.retain_raw
}
}
impl Default for FixTranscriptConfig {
fn default() -> Self {
Self {
max_records: 4096,
max_raw_bytes: 4 * 1024 * 1024,
retain_raw: true,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum FixTranscriptError {
MsgTypeTooLong {
capacity: usize,
actual: usize,
},
}
impl fmt::Display for FixTranscriptError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::MsgTypeTooLong { capacity, actual } => write!(
f,
"FIX transcript message type length {actual} exceeds capacity {capacity}"
),
}
}
}
impl Error for FixTranscriptError {}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FixTranscriptRecord {
ordinal: u64,
timestamp_ns: u64,
direction: FixTranscriptDirection,
seq_no: Option<u64>,
msg_type: FixTranscriptMsgType,
raw_len: usize,
raw_checksum: u8,
raw_hash: u64,
raw_retained: bool,
raw: Vec<u8>,
}
impl FixTranscriptRecord {
pub const fn ordinal(&self) -> u64 {
self.ordinal
}
pub const fn timestamp_ns(&self) -> u64 {
self.timestamp_ns
}
pub const fn direction(&self) -> FixTranscriptDirection {
self.direction
}
pub const fn seq_no(&self) -> Option<u64> {
self.seq_no
}
pub fn msg_type(&self) -> &[u8] {
self.msg_type.as_bytes()
}
pub const fn raw_len(&self) -> usize {
self.raw_len
}
pub const fn raw_checksum(&self) -> u8 {
self.raw_checksum
}
pub const fn raw_hash(&self) -> u64 {
self.raw_hash
}
pub const fn raw_retained(&self) -> bool {
self.raw_retained
}
pub fn raw(&self) -> &[u8] {
&self.raw
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FixTranscriptRetention {
retained: bool,
raw_retained: bool,
evicted_records: u64,
evicted_raw_bytes: u64,
}
impl FixTranscriptRetention {
pub const fn retained(&self) -> bool {
self.retained
}
pub const fn raw_retained(&self) -> bool {
self.raw_retained
}
pub const fn evicted_records(&self) -> u64 {
self.evicted_records
}
pub const fn evicted_raw_bytes(&self) -> u64 {
self.evicted_raw_bytes
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FixTranscriptMetrics {
captured_records: u64,
retained_records: u64,
retained_raw_bytes: u64,
dropped_records: u64,
dropped_raw_bytes: u64,
evicted_records: u64,
evicted_raw_bytes: u64,
oldest_ordinal: Option<u64>,
newest_ordinal: Option<u64>,
rolling_hash: u64,
}
impl FixTranscriptMetrics {
pub const fn captured_records(&self) -> u64 {
self.captured_records
}
pub const fn retained_records(&self) -> u64 {
self.retained_records
}
pub const fn retained_raw_bytes(&self) -> u64 {
self.retained_raw_bytes
}
pub const fn dropped_records(&self) -> u64 {
self.dropped_records
}
pub const fn dropped_raw_bytes(&self) -> u64 {
self.dropped_raw_bytes
}
pub const fn evicted_records(&self) -> u64 {
self.evicted_records
}
pub const fn evicted_raw_bytes(&self) -> u64 {
self.evicted_raw_bytes
}
pub const fn oldest_ordinal(&self) -> Option<u64> {
self.oldest_ordinal
}
pub const fn newest_ordinal(&self) -> Option<u64> {
self.newest_ordinal
}
pub const fn rolling_hash(&self) -> u64 {
self.rolling_hash
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FixTranscriptCapture {
config: FixTranscriptConfig,
records: VecDeque<FixTranscriptRecord>,
retained_raw_bytes: usize,
captured_records: u64,
dropped_records: u64,
dropped_raw_bytes: u64,
evicted_records: u64,
evicted_raw_bytes: u64,
rolling_hash: u64,
}
impl Default for FixTranscriptCapture {
fn default() -> Self {
Self::new(FixTranscriptConfig::default())
}
}
impl FixTranscriptCapture {
pub fn new(config: FixTranscriptConfig) -> Self {
Self {
config,
records: VecDeque::with_capacity(config.max_records.min(64)),
retained_raw_bytes: 0,
captured_records: 0,
dropped_records: 0,
dropped_raw_bytes: 0,
evicted_records: 0,
evicted_raw_bytes: 0,
rolling_hash: FNV_OFFSET_BASIS,
}
}
pub const fn config(&self) -> FixTranscriptConfig {
self.config
}
pub fn records(&self) -> impl Iterator<Item = &FixTranscriptRecord> {
self.records.iter()
}
pub fn record_message(
&mut self,
direction: FixTranscriptDirection,
timestamp_ns: u64,
message: &FixMessageView<'_>,
) -> Result<FixTranscriptRetention, FixTranscriptError> {
self.record_frame(
direction,
timestamp_ns,
message.msg_seq_num(),
message.msg_type().unwrap_or(&[]),
message.raw(),
)
}
pub fn record_frame(
&mut self,
direction: FixTranscriptDirection,
timestamp_ns: u64,
seq_no: Option<u64>,
msg_type: &[u8],
raw: &[u8],
) -> Result<FixTranscriptRetention, FixTranscriptError> {
let msg_type = FixTranscriptMsgType::new(msg_type)?;
self.captured_records = self.captured_records.saturating_add(1);
let ordinal = self.captured_records;
let raw_hash = hash_bytes(raw);
let raw_checksum = checksum(raw);
self.rolling_hash = update_transcript_hash(
self.rolling_hash,
ordinal,
timestamp_ns,
direction,
seq_no,
msg_type.as_bytes(),
raw,
);
if self.config.max_records == 0 {
self.dropped_records = self.dropped_records.saturating_add(1);
self.dropped_raw_bytes = self
.dropped_raw_bytes
.saturating_add(usize_to_u64(raw.len()));
return Ok(FixTranscriptRetention {
retained: false,
raw_retained: false,
evicted_records: 0,
evicted_raw_bytes: 0,
});
}
let retain_raw = self.config.retain_raw
&& self.config.max_raw_bytes > 0
&& raw.len() <= self.config.max_raw_bytes;
let raw_vec = if retain_raw { raw.to_vec() } else { Vec::new() };
if !retain_raw && !raw.is_empty() {
self.dropped_raw_bytes = self
.dropped_raw_bytes
.saturating_add(usize_to_u64(raw.len()));
}
let retained_raw_len = raw_vec.len();
self.retained_raw_bytes = self.retained_raw_bytes.saturating_add(retained_raw_len);
self.records.push_back(FixTranscriptRecord {
ordinal,
timestamp_ns,
direction,
seq_no,
msg_type,
raw_len: raw.len(),
raw_checksum,
raw_hash,
raw_retained: retain_raw,
raw: raw_vec,
});
let (evicted_records, evicted_raw_bytes) = self.evict_to_bounds();
Ok(FixTranscriptRetention {
retained: true,
raw_retained: retain_raw,
evicted_records,
evicted_raw_bytes,
})
}
pub fn clear_retained(&mut self) {
self.records.clear();
self.retained_raw_bytes = 0;
}
pub fn metrics(&self) -> FixTranscriptMetrics {
FixTranscriptMetrics {
captured_records: self.captured_records,
retained_records: usize_to_u64(self.records.len()),
retained_raw_bytes: usize_to_u64(self.retained_raw_bytes),
dropped_records: self.dropped_records,
dropped_raw_bytes: self.dropped_raw_bytes,
evicted_records: self.evicted_records,
evicted_raw_bytes: self.evicted_raw_bytes,
oldest_ordinal: self.records.front().map(FixTranscriptRecord::ordinal),
newest_ordinal: if self.captured_records == 0 {
None
} else {
Some(self.captured_records)
},
rolling_hash: self.rolling_hash,
}
}
fn evict_to_bounds(&mut self) -> (u64, u64) {
let mut evicted_records = 0u64;
let mut evicted_raw_bytes = 0u64;
while self.records.len() > self.config.max_records
|| self.retained_raw_bytes > self.config.max_raw_bytes
{
let Some(record) = self.records.pop_front() else {
break;
};
evicted_records = evicted_records.saturating_add(1);
let raw_len = if record.raw_retained {
record.raw.len()
} else {
0
};
self.retained_raw_bytes = self.retained_raw_bytes.saturating_sub(raw_len);
evicted_raw_bytes = evicted_raw_bytes.saturating_add(usize_to_u64(raw_len));
}
self.evicted_records = self.evicted_records.saturating_add(evicted_records);
self.evicted_raw_bytes = self.evicted_raw_bytes.saturating_add(evicted_raw_bytes);
(evicted_records, evicted_raw_bytes)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FixResendStore {
config: FixResendStoreConfig,
messages: VecDeque<FixStoredMessage>,
retained_bytes: usize,
dropped_messages: u64,
dropped_bytes: u64,
evicted_messages: u64,
evicted_bytes: u64,
newest_seq_no: Option<u64>,
}
impl Default for FixResendStore {
fn default() -> Self {
Self::new(FixResendStoreConfig::default())
}
}
impl FixResendStore {
pub fn new(config: FixResendStoreConfig) -> Self {
Self {
config,
messages: VecDeque::with_capacity(config.max_messages.min(64)),
retained_bytes: 0,
dropped_messages: 0,
dropped_bytes: 0,
evicted_messages: 0,
evicted_bytes: 0,
newest_seq_no: None,
}
}
pub const fn config(&self) -> FixResendStoreConfig {
self.config
}
pub fn messages(&self) -> impl Iterator<Item = &FixStoredMessage> {
self.messages.iter()
}
pub fn get(&self, seq_no: u64) -> Option<&FixStoredMessage> {
self.messages
.iter()
.find(|message| message.seq_no == seq_no)
}
pub fn record_sent(
&mut self,
seq_no: u64,
kind: FixSentMessageKind,
raw: &[u8],
) -> Result<FixResendRetention, FixResendStoreError> {
if seq_no == 0 {
return Err(FixResendStoreError::ZeroSeqNo);
}
if let Some(latest) = self.newest_seq_no {
if seq_no <= latest {
return Err(FixResendStoreError::SequenceRegression {
latest,
received: seq_no,
});
}
}
self.newest_seq_no = Some(seq_no);
if self.config.max_messages == 0
|| self.config.max_bytes == 0
|| raw.len() > self.config.max_bytes
{
self.dropped_messages = self.dropped_messages.saturating_add(1);
self.dropped_bytes = self.dropped_bytes.saturating_add(raw.len() as u64);
return Ok(FixResendRetention {
retained: false,
evicted_messages: 0,
evicted_bytes: 0,
});
}
self.messages.push_back(FixStoredMessage {
seq_no,
kind,
raw: raw.to_vec(),
});
self.retained_bytes = self.retained_bytes.saturating_add(raw.len());
let mut evicted_messages = 0_u64;
let mut evicted_bytes = 0_u64;
while self.messages.len() > self.config.max_messages
|| self.retained_bytes > self.config.max_bytes
{
if let Some(evicted) = self.messages.pop_front() {
let len = evicted.raw.len();
self.retained_bytes = self.retained_bytes.saturating_sub(len);
evicted_messages = evicted_messages.saturating_add(1);
evicted_bytes = evicted_bytes.saturating_add(len as u64);
} else {
break;
}
}
self.evicted_messages = self.evicted_messages.saturating_add(evicted_messages);
self.evicted_bytes = self.evicted_bytes.saturating_add(evicted_bytes);
Ok(FixResendRetention {
retained: true,
evicted_messages,
evicted_bytes,
})
}
pub fn plan_resend_range<'a>(
&'a self,
range: FixResendRange,
out: &mut Vec<FixResendAction<'a>>,
) -> FixResendPlanSummary {
out.clear();
let Some(end_seq_no) = self.resolve_resend_end(range.end_seq_no) else {
return FixResendPlanSummary {
replay_messages: 0,
gap_fill_messages: 0,
gap_fill_sequences: 0,
};
};
if range.begin_seq_no == 0 || range.begin_seq_no > end_seq_no {
return FixResendPlanSummary {
replay_messages: 0,
gap_fill_messages: 0,
gap_fill_sequences: 0,
};
}
let mut cursor = range.begin_seq_no;
let mut replay_messages = 0_u64;
let mut gap_fill_messages = 0_u64;
let mut gap_fill_sequences = 0_u64;
for message in self
.messages
.iter()
.filter(|message| message.seq_no >= range.begin_seq_no && message.seq_no <= end_seq_no)
{
if !message.replayable() {
continue;
}
if cursor < message.seq_no {
push_gap_fill(
out,
cursor,
message.seq_no.saturating_sub(1),
&mut gap_fill_messages,
&mut gap_fill_sequences,
);
}
out.push(FixResendAction::Replay {
seq_no: message.seq_no,
raw: message.raw(),
});
replay_messages = replay_messages.saturating_add(1);
cursor = message.seq_no.saturating_add(1);
}
if cursor <= end_seq_no {
push_gap_fill(
out,
cursor,
end_seq_no,
&mut gap_fill_messages,
&mut gap_fill_sequences,
);
}
FixResendPlanSummary {
replay_messages,
gap_fill_messages,
gap_fill_sequences,
}
}
pub fn metrics(&self) -> FixResendStoreMetrics {
FixResendStoreMetrics {
retained_messages: self.messages.len() as u64,
retained_bytes: self.retained_bytes as u64,
dropped_messages: self.dropped_messages,
dropped_bytes: self.dropped_bytes,
evicted_messages: self.evicted_messages,
evicted_bytes: self.evicted_bytes,
oldest_seq_no: self.messages.front().map(|message| message.seq_no),
newest_seq_no: self.newest_seq_no,
}
}
fn resolve_resend_end(&self, requested_end: u64) -> Option<u64> {
if requested_end == 0 {
self.newest_seq_no
} else {
Some(requested_end)
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FixSequenceTracker {
next_inbound: u64,
next_outbound: u64,
}
impl Default for FixSequenceTracker {
fn default() -> Self {
Self::new()
}
}
impl FixSequenceTracker {
pub const fn new() -> Self {
Self {
next_inbound: 1,
next_outbound: 1,
}
}
pub const fn from_next(next_inbound: u64, next_outbound: u64) -> Self {
Self {
next_inbound: clamp_seq_no(next_inbound),
next_outbound: clamp_seq_no(next_outbound),
}
}
pub const fn next_inbound(&self) -> u64 {
self.next_inbound
}
pub const fn next_outbound(&self) -> u64 {
self.next_outbound
}
pub fn assign_outbound(&mut self) -> u64 {
let seq_no = self.next_outbound;
self.next_outbound = self.next_outbound.saturating_add(1);
seq_no
}
pub fn observe_message(
&mut self,
message: &FixMessageView<'_>,
) -> Result<FixSequenceAction, FixSequenceError> {
let seq_no = message
.msg_seq_num()
.ok_or(FixSequenceError::MissingMsgSeqNum)?;
self.observe_inbound(seq_no, message.poss_dup())
}
pub fn observe_inbound(
&mut self,
seq_no: u64,
poss_dup: bool,
) -> Result<FixSequenceAction, FixSequenceError> {
if seq_no == 0 {
return Err(FixSequenceError::ZeroSeqNo);
}
let expected = self.next_inbound;
if seq_no == expected {
self.next_inbound = self.next_inbound.saturating_add(1);
Ok(FixSequenceAction::Accept { seq_no })
} else if seq_no > expected {
Ok(FixSequenceAction::Gap {
expected,
received: seq_no,
resend: FixResendRange {
begin_seq_no: expected,
end_seq_no: seq_no.saturating_sub(1),
},
})
} else if poss_dup {
Ok(FixSequenceAction::Duplicate { seq_no, expected })
} else {
Ok(FixSequenceAction::TooLow {
expected,
received: seq_no,
})
}
}
pub fn apply_sequence_reset(&mut self, new_seq_no: u64) -> Result<(), FixSequenceError> {
if new_seq_no == 0 {
return Err(FixSequenceError::ZeroSeqNo);
}
if new_seq_no < self.next_inbound {
return Err(FixSequenceError::SequenceResetWouldDecrease {
current: self.next_inbound,
requested: new_seq_no,
});
}
self.next_inbound = new_seq_no;
Ok(())
}
pub fn set_next_inbound(&mut self, next_inbound: u64) {
self.next_inbound = clamp_seq_no(next_inbound);
}
pub fn set_next_outbound(&mut self, next_outbound: u64) {
self.next_outbound = clamp_seq_no(next_outbound);
}
pub fn snapshot<'a>(
&self,
session_id: FixSessionId<'a>,
trading_day: &'a [u8],
) -> Result<FixSequenceSnapshot<'a>, FixEncodeError> {
FixSequenceSnapshot::new(
session_id,
self.next_inbound,
self.next_outbound,
trading_day,
)
}
pub const fn from_snapshot(snapshot: &FixSequenceSnapshot<'_>) -> Self {
Self::from_next(snapshot.next_inbound(), snapshot.next_outbound())
}
pub fn reset_to_one(&mut self) {
self.next_inbound = 1;
self.next_outbound = 1;
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FixMessageRule<'a> {
msg_type: FixMsgType,
required_tags: &'a [FixTag],
disallowed_tags: &'a [FixTag],
}
impl<'a> FixMessageRule<'a> {
pub const fn new(
msg_type: FixMsgType,
required_tags: &'a [FixTag],
disallowed_tags: &'a [FixTag],
) -> Self {
Self {
msg_type,
required_tags,
disallowed_tags,
}
}
pub const fn msg_type(&self) -> FixMsgType {
self.msg_type
}
pub const fn required_tags(&self) -> &'a [FixTag] {
self.required_tags
}
pub const fn disallowed_tags(&self) -> &'a [FixTag] {
self.disallowed_tags
}
pub fn validate(&self, message: &FixMessageView<'_>) -> Result<(), FixProfileError> {
for tag in self.required_tags {
if message.get(*tag).is_none() {
return Err(FixProfileError::MissingRequiredTag {
msg_type: self.msg_type,
tag: *tag,
});
}
}
for tag in self.disallowed_tags {
if message.get(*tag).is_some() {
return Err(FixProfileError::DisallowedTag {
msg_type: self.msg_type,
tag: *tag,
});
}
}
Ok(())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FixDictionary<'a> {
version: FixVersion,
rules: &'a [FixMessageRule<'a>],
}
impl<'a> FixDictionary<'a> {
pub const fn new(version: FixVersion, rules: &'a [FixMessageRule<'a>]) -> Self {
Self { version, rules }
}
pub const fn version(&self) -> FixVersion {
self.version
}
pub const fn rules(&self) -> &'a [FixMessageRule<'a>] {
self.rules
}
pub fn rule_for(&self, msg_type: FixMsgType) -> Option<&'a FixMessageRule<'a>> {
self.rules.iter().find(|rule| rule.msg_type == msg_type)
}
pub fn rule_for_bytes(&self, msg_type: &[u8]) -> Option<&'a FixMessageRule<'a>> {
self.rules
.iter()
.find(|rule| rule.msg_type.as_bytes() == msg_type)
}
pub fn validate(&self, message: &FixMessageView<'_>) -> Result<(), FixProfileError> {
let actual_version = message
.begin_string()
.ok_or(FixProfileError::MissingBeginString)
.and_then(|value| {
FixVersion::from_bytes(value).ok_or(FixProfileError::UnsupportedVersion)
})?;
if actual_version != self.version {
return Err(FixProfileError::VersionMismatch {
expected: self.version,
actual: actual_version,
});
}
let msg_type = message.msg_type().ok_or(FixProfileError::MissingMsgType)?;
let rule = self
.rule_for_bytes(msg_type)
.ok_or(FixProfileError::UnsupportedMsgType)?;
rule.validate(message)
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct FixDecoder;
impl FixDecoder {
pub const fn new() -> Self {
Self
}
pub fn parse<'a>(
&self,
raw: &'a [u8],
scratch: &'a mut [FixFieldView<'a>],
) -> Result<FixMessageView<'a>, FixParseError> {
parse_message(raw, scratch)
}
}
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct FixEncoder {
buffer: Vec<u8>,
}
impl FixEncoder {
pub const fn new() -> Self {
Self { buffer: Vec::new() }
}
pub fn with_capacity(capacity: usize) -> Self {
Self {
buffer: Vec::with_capacity(capacity),
}
}
pub fn encode(
&mut self,
begin_string: &[u8],
msg_type: &[u8],
fields: &[(FixTag, &[u8])],
) -> Result<&[u8], FixEncodeError> {
encode_message(&mut self.buffer, begin_string, msg_type, fields)?;
Ok(&self.buffer)
}
pub fn encode_with_repeating_group(
&mut self,
begin_string: &[u8],
msg_type: &[u8],
fields: &[(FixTag, &[u8])],
definition: FixRepeatingGroupDefinition<'_>,
groups: &[&[(FixTag, &[u8])]],
) -> Result<&[u8], FixEncodeError> {
encode_message_with_repeating_group(
&mut self.buffer,
begin_string,
msg_type,
fields,
definition,
groups,
)?;
Ok(&self.buffer)
}
pub fn encode_typed(
&mut self,
version: FixVersion,
msg_type: FixMsgType,
fields: &[(FixTag, &[u8])],
) -> Result<&[u8], FixEncodeError> {
self.encode(version.as_bytes(), msg_type.as_bytes(), fields)
}
pub fn buffer(&self) -> &[u8] {
&self.buffer
}
pub fn clear(&mut self) {
self.buffer.clear();
}
pub fn into_buffer(self) -> Vec<u8> {
self.buffer
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FixSessionHeader<'a> {
sender_comp_id: &'a [u8],
target_comp_id: &'a [u8],
msg_seq_num: u64,
sending_time: &'a [u8],
}
impl<'a> FixSessionHeader<'a> {
pub const fn new(
sender_comp_id: &'a [u8],
target_comp_id: &'a [u8],
msg_seq_num: u64,
sending_time: &'a [u8],
) -> Self {
Self {
sender_comp_id,
target_comp_id,
msg_seq_num,
sending_time,
}
}
pub const fn sender_comp_id(&self) -> &'a [u8] {
self.sender_comp_id
}
pub const fn target_comp_id(&self) -> &'a [u8] {
self.target_comp_id
}
pub const fn msg_seq_num(&self) -> u64 {
self.msg_seq_num
}
pub const fn sending_time(&self) -> &'a [u8] {
self.sending_time
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FixNewOrderSingle<'a> {
cl_ord_id: &'a [u8],
account: Option<&'a [u8]>,
symbol: &'a [u8],
side: FixOrderSide,
transact_time: &'a [u8],
order_qty: &'a [u8],
ord_type: FixOrdType,
price: Option<&'a [u8]>,
stop_px: Option<&'a [u8]>,
time_in_force: Option<FixTimeInForce>,
}
impl<'a> FixNewOrderSingle<'a> {
pub const fn new(
cl_ord_id: &'a [u8],
symbol: &'a [u8],
side: FixOrderSide,
transact_time: &'a [u8],
order_qty: &'a [u8],
ord_type: FixOrdType,
) -> Self {
Self {
cl_ord_id,
account: None,
symbol,
side,
transact_time,
order_qty,
ord_type,
price: None,
stop_px: None,
time_in_force: None,
}
}
pub const fn with_account(mut self, account: &'a [u8]) -> Self {
self.account = Some(account);
self
}
pub const fn with_price(mut self, price: &'a [u8]) -> Self {
self.price = Some(price);
self
}
pub const fn with_stop_px(mut self, stop_px: &'a [u8]) -> Self {
self.stop_px = Some(stop_px);
self
}
pub const fn with_time_in_force(mut self, time_in_force: FixTimeInForce) -> Self {
self.time_in_force = Some(time_in_force);
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FixOrderCancelRequest<'a> {
orig_cl_ord_id: &'a [u8],
cl_ord_id: &'a [u8],
account: Option<&'a [u8]>,
symbol: &'a [u8],
side: FixOrderSide,
transact_time: &'a [u8],
}
impl<'a> FixOrderCancelRequest<'a> {
pub const fn new(
orig_cl_ord_id: &'a [u8],
cl_ord_id: &'a [u8],
symbol: &'a [u8],
side: FixOrderSide,
transact_time: &'a [u8],
) -> Self {
Self {
orig_cl_ord_id,
cl_ord_id,
account: None,
symbol,
side,
transact_time,
}
}
pub const fn with_account(mut self, account: &'a [u8]) -> Self {
self.account = Some(account);
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FixOrderCancelReplaceRequest<'a> {
orig_cl_ord_id: &'a [u8],
cl_ord_id: &'a [u8],
account: Option<&'a [u8]>,
symbol: &'a [u8],
side: FixOrderSide,
transact_time: &'a [u8],
order_qty: &'a [u8],
ord_type: FixOrdType,
price: Option<&'a [u8]>,
stop_px: Option<&'a [u8]>,
time_in_force: Option<FixTimeInForce>,
}
impl<'a> FixOrderCancelReplaceRequest<'a> {
pub const fn new(
orig_cl_ord_id: &'a [u8],
cl_ord_id: &'a [u8],
symbol: &'a [u8],
side: FixOrderSide,
transact_time: &'a [u8],
order_qty: &'a [u8],
ord_type: FixOrdType,
) -> Self {
Self {
orig_cl_ord_id,
cl_ord_id,
account: None,
symbol,
side,
transact_time,
order_qty,
ord_type,
price: None,
stop_px: None,
time_in_force: None,
}
}
pub const fn with_account(mut self, account: &'a [u8]) -> Self {
self.account = Some(account);
self
}
pub const fn with_price(mut self, price: &'a [u8]) -> Self {
self.price = Some(price);
self
}
pub const fn with_stop_px(mut self, stop_px: &'a [u8]) -> Self {
self.stop_px = Some(stop_px);
self
}
pub const fn with_time_in_force(mut self, time_in_force: FixTimeInForce) -> Self {
self.time_in_force = Some(time_in_force);
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FixOrderStatusRequest<'a> {
cl_ord_id: &'a [u8],
order_id: Option<&'a [u8]>,
}
impl<'a> FixOrderStatusRequest<'a> {
pub const fn new(cl_ord_id: &'a [u8]) -> Self {
Self {
cl_ord_id,
order_id: None,
}
}
pub const fn with_order_id(mut self, order_id: &'a [u8]) -> Self {
self.order_id = Some(order_id);
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FixOrderMassCancelRequest<'a> {
cl_ord_id: &'a [u8],
mass_cancel_request_type: FixMassCancelRequestType,
transact_time: &'a [u8],
secondary_cl_ord_id: Option<&'a [u8]>,
trading_session_id: Option<&'a [u8]>,
trading_session_sub_id: Option<&'a [u8]>,
symbol: Option<&'a [u8]>,
side: Option<FixOrderSide>,
text: Option<&'a [u8]>,
}
impl<'a> FixOrderMassCancelRequest<'a> {
pub const fn new(
cl_ord_id: &'a [u8],
mass_cancel_request_type: FixMassCancelRequestType,
transact_time: &'a [u8],
) -> Self {
Self {
cl_ord_id,
mass_cancel_request_type,
transact_time,
secondary_cl_ord_id: None,
trading_session_id: None,
trading_session_sub_id: None,
symbol: None,
side: None,
text: None,
}
}
pub const fn with_secondary_cl_ord_id(mut self, secondary_cl_ord_id: &'a [u8]) -> Self {
self.secondary_cl_ord_id = Some(secondary_cl_ord_id);
self
}
pub const fn with_trading_session_id(mut self, trading_session_id: &'a [u8]) -> Self {
self.trading_session_id = Some(trading_session_id);
self
}
pub const fn with_trading_session_sub_id(mut self, trading_session_sub_id: &'a [u8]) -> Self {
self.trading_session_sub_id = Some(trading_session_sub_id);
self
}
pub const fn with_symbol(mut self, symbol: &'a [u8]) -> Self {
self.symbol = Some(symbol);
self
}
pub const fn with_side(mut self, side: FixOrderSide) -> Self {
self.side = Some(side);
self
}
pub const fn with_text(mut self, text: &'a [u8]) -> Self {
self.text = Some(text);
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FixOrderMassStatusRequest<'a> {
mass_status_req_id: &'a [u8],
mass_status_req_type: FixMassStatusReqType,
account: Option<&'a [u8]>,
acct_id_source: Option<&'a [u8]>,
trading_session_id: Option<&'a [u8]>,
trading_session_sub_id: Option<&'a [u8]>,
symbol: Option<&'a [u8]>,
side: Option<FixOrderSide>,
}
impl<'a> FixOrderMassStatusRequest<'a> {
pub const fn new(
mass_status_req_id: &'a [u8],
mass_status_req_type: FixMassStatusReqType,
) -> Self {
Self {
mass_status_req_id,
mass_status_req_type,
account: None,
acct_id_source: None,
trading_session_id: None,
trading_session_sub_id: None,
symbol: None,
side: None,
}
}
pub const fn with_account(mut self, account: &'a [u8]) -> Self {
self.account = Some(account);
self
}
pub const fn with_acct_id_source(mut self, acct_id_source: &'a [u8]) -> Self {
self.acct_id_source = Some(acct_id_source);
self
}
pub const fn with_trading_session_id(mut self, trading_session_id: &'a [u8]) -> Self {
self.trading_session_id = Some(trading_session_id);
self
}
pub const fn with_trading_session_sub_id(mut self, trading_session_sub_id: &'a [u8]) -> Self {
self.trading_session_sub_id = Some(trading_session_sub_id);
self
}
pub const fn with_symbol(mut self, symbol: &'a [u8]) -> Self {
self.symbol = Some(symbol);
self
}
pub const fn with_side(mut self, side: FixOrderSide) -> Self {
self.side = Some(side);
self
}
}
pub fn parse_message<'a>(
raw: &'a [u8],
scratch: &'a mut [FixFieldView<'a>],
) -> Result<FixMessageView<'a>, FixParseError> {
if raw.is_empty() {
return Err(FixParseError::Empty);
}
let mut count = 0usize;
let mut body_start = None;
let mut checksum_start = None;
let mut declared_body_len = None;
let mut declared_checksum = None;
let mut pos = 0usize;
while pos < raw.len() {
let field_start = pos;
let delimiter = raw[field_start..]
.iter()
.position(|b| *b == SOH)
.map(|offset| field_start + offset)
.unwrap_or(raw.len());
if delimiter == field_start {
return Err(FixParseError::MalformedField);
}
let field = &raw[field_start..delimiter];
let eq = field
.iter()
.position(|b| *b == b'=')
.ok_or(FixParseError::MalformedField)?;
if eq == 0 {
return Err(FixParseError::InvalidTag);
}
let tag = FixTag(parse_u32(&field[..eq]).map_err(|_| FixParseError::InvalidTag)?);
let value = &field[eq + 1..];
if count == scratch.len() {
return Err(FixParseError::ScratchTooSmall {
required: count.saturating_add(1),
capacity: scratch.len(),
});
}
scratch[count] = FixFieldView { tag, value };
count += 1;
if tag == FixTag::BODY_LENGTH {
declared_body_len =
Some(parse_usize(value).map_err(|_| FixParseError::InvalidBodyLength)?);
body_start = Some(delimiter.saturating_add(1));
} else if tag == FixTag::CHECK_SUM {
checksum_start = Some(field_start);
declared_checksum = Some(parse_checksum(value)?);
}
pos = delimiter.saturating_add(1);
}
let body_start = body_start.ok_or(FixParseError::MissingRequiredTag(FixTag::BODY_LENGTH))?;
let checksum_start =
checksum_start.ok_or(FixParseError::MissingRequiredTag(FixTag::CHECK_SUM))?;
let expected_body_len =
declared_body_len.ok_or(FixParseError::MissingRequiredTag(FixTag::BODY_LENGTH))?;
let expected_checksum =
declared_checksum.ok_or(FixParseError::MissingRequiredTag(FixTag::CHECK_SUM))?;
if scratch[..count]
.iter()
.all(|field| field.tag != FixTag::BEGIN_STRING)
{
return Err(FixParseError::MissingRequiredTag(FixTag::BEGIN_STRING));
}
let actual_body_len = checksum_start.saturating_sub(body_start);
if expected_body_len != actual_body_len {
return Err(FixParseError::BodyLengthMismatch {
expected: expected_body_len,
actual: actual_body_len,
});
}
let actual_checksum = checksum(&raw[..checksum_start]);
if expected_checksum != actual_checksum {
return Err(FixParseError::ChecksumMismatch {
expected: expected_checksum,
actual: actual_checksum,
});
}
Ok(FixMessageView {
raw,
fields: &scratch[..count],
})
}
pub fn parse_session_reject<'a>(
message: &FixMessageView<'a>,
) -> Result<FixSessionRejectView<'a>, FixRejectParseError> {
if message.msg_type() != Some(FixMsgType::REJECT.as_bytes()) {
return Err(FixRejectParseError::InvalidMsgType);
}
Ok(FixSessionRejectView {
ref_seq_num: parse_required_u64(message, FixTag::REF_SEQ_NUM)?,
ref_tag_id: parse_optional_fix_tag(message, FixTag::REF_TAG_ID)?,
ref_msg_type: message.get(FixTag::REF_MSG_TYPE),
session_reject_reason: parse_optional_reject_u64(message, FixTag::SESSION_REJECT_REASON)?,
text: message.get(FixTag::TEXT),
})
}
pub fn parse_business_message_reject<'a>(
message: &FixMessageView<'a>,
) -> Result<FixBusinessMessageRejectView<'a>, FixRejectParseError> {
if message.msg_type() != Some(FixMsgType::BUSINESS_MESSAGE_REJECT.as_bytes()) {
return Err(FixRejectParseError::InvalidMsgType);
}
let ref_msg_type = message
.get(FixTag::REF_MSG_TYPE)
.ok_or(FixRejectParseError::MissingTag(FixTag::REF_MSG_TYPE))?;
Ok(FixBusinessMessageRejectView {
ref_seq_num: parse_optional_reject_u64(message, FixTag::REF_SEQ_NUM)?,
ref_msg_type,
business_reject_ref_id: message.get(FixTag::BUSINESS_REJECT_REF_ID),
business_reject_reason: parse_required_u64(message, FixTag::BUSINESS_REJECT_REASON)?,
text: message.get(FixTag::TEXT),
})
}
pub fn encode_message(
out: &mut Vec<u8>,
begin_string: &[u8],
msg_type: &[u8],
fields: &[(FixTag, &[u8])],
) -> Result<(), FixEncodeError> {
encode_message_parts(out, begin_string, msg_type, &[], fields)
}
pub fn encode_message_with_repeating_group(
out: &mut Vec<u8>,
begin_string: &[u8],
msg_type: &[u8],
fields: &[(FixTag, &[u8])],
definition: FixRepeatingGroupDefinition<'_>,
groups: &[&[(FixTag, &[u8])]],
) -> Result<(), FixEncodeError> {
encode_message_parts_with_repeating_group(
out,
begin_string,
msg_type,
&[],
fields,
Some(definition),
groups,
)
}
pub fn encode_poss_dup_replay(
out: &mut Vec<u8>,
source: &FixMessageView<'_>,
sending_time: &[u8],
) -> Result<(), FixEncodeError> {
let begin_string = source
.begin_string()
.ok_or(FixEncodeError::MissingRequiredTag(FixTag::BEGIN_STRING))?;
let msg_type = source
.msg_type()
.ok_or(FixEncodeError::MissingRequiredTag(FixTag::MSG_TYPE))?;
let orig_sending_time = source
.get(FixTag::ORIG_SENDING_TIME)
.or_else(|| source.get(FixTag::SENDING_TIME))
.ok_or(FixEncodeError::MissingRequiredTag(FixTag::SENDING_TIME))?;
validate_value(FixTag::BEGIN_STRING, begin_string)?;
validate_value(FixTag::MSG_TYPE, msg_type)?;
validate_value(FixTag::SENDING_TIME, sending_time)?;
validate_value(FixTag::ORIG_SENDING_TIME, orig_sending_time)?;
out.clear();
write_field(out, FixTag::BEGIN_STRING, begin_string);
write_field(out, FixTag::BODY_LENGTH, b"0000000000");
let body_start = out.len();
write_field(out, FixTag::MSG_TYPE, msg_type);
let mut wrote_replay_header = false;
for field in source.fields() {
match field.tag {
FixTag::BEGIN_STRING
| FixTag::BODY_LENGTH
| FixTag::MSG_TYPE
| FixTag::CHECK_SUM
| FixTag::POSS_DUP_FLAG
| FixTag::ORIG_SENDING_TIME => {}
FixTag::SENDING_TIME => {
write_replay_header(out, sending_time, orig_sending_time);
wrote_replay_header = true;
}
tag => {
validate_value(tag, field.value)?;
write_field(out, tag, field.value);
}
}
}
if !wrote_replay_header {
return Err(FixEncodeError::MissingRequiredTag(FixTag::SENDING_TIME));
}
let body_len = out.len().saturating_sub(body_start);
patch_body_length(out, body_len);
let sum = checksum(out);
write_checksum(out, sum);
Ok(())
}
pub fn encode_logon(
out: &mut Vec<u8>,
version: FixVersion,
header: FixSessionHeader<'_>,
heartbeat_interval_secs: u64,
reset_seq_num: bool,
) -> Result<(), FixEncodeError> {
let mut heartbeat = [0u8; 20];
let heartbeat_len = write_u64_digits(&mut heartbeat, heartbeat_interval_secs);
let extra = [
(FixTag::ENCRYPT_METHOD, b"0".as_slice()),
(FixTag::HEART_BT_INT, &heartbeat[..heartbeat_len]),
(FixTag::RESET_SEQ_NUM_FLAG, b"Y".as_slice()),
];
let extra_len = if reset_seq_num { 3 } else { 2 };
encode_session_message(out, version, FixMsgType::LOGON, header, &extra[..extra_len])
}
pub fn encode_heartbeat(
out: &mut Vec<u8>,
version: FixVersion,
header: FixSessionHeader<'_>,
test_req_id: Option<&[u8]>,
) -> Result<(), FixEncodeError> {
let mut extra = [(FixTag::TEST_REQ_ID, b"".as_slice())];
let extra_len = if let Some(test_req_id) = test_req_id {
extra[0] = (FixTag::TEST_REQ_ID, test_req_id);
1
} else {
0
};
encode_session_message(
out,
version,
FixMsgType::HEARTBEAT,
header,
&extra[..extra_len],
)
}
pub fn encode_test_request(
out: &mut Vec<u8>,
version: FixVersion,
header: FixSessionHeader<'_>,
test_req_id: &[u8],
) -> Result<(), FixEncodeError> {
let extra = [(FixTag::TEST_REQ_ID, test_req_id)];
encode_session_message(out, version, FixMsgType::TEST_REQUEST, header, &extra)
}
pub fn encode_resend_request(
out: &mut Vec<u8>,
version: FixVersion,
header: FixSessionHeader<'_>,
range: FixResendRange,
) -> Result<(), FixEncodeError> {
let mut begin = [0u8; 20];
let begin_len = write_u64_digits(&mut begin, range.begin_seq_no);
let mut end = [0u8; 20];
let end_len = write_u64_digits(&mut end, range.end_seq_no);
let extra = [
(FixTag::BEGIN_SEQ_NO, &begin[..begin_len]),
(FixTag::END_SEQ_NO, &end[..end_len]),
];
encode_session_message(out, version, FixMsgType::RESEND_REQUEST, header, &extra)
}
pub fn encode_sequence_reset_gap_fill(
out: &mut Vec<u8>,
version: FixVersion,
header: FixSessionHeader<'_>,
new_seq_no: u64,
) -> Result<(), FixEncodeError> {
let mut new_seq = [0u8; 20];
let new_seq_len = write_u64_digits(&mut new_seq, new_seq_no);
let extra = [
(FixTag::GAP_FILL_FLAG, b"Y".as_slice()),
(FixTag::NEW_SEQ_NO, &new_seq[..new_seq_len]),
];
encode_session_message(out, version, FixMsgType::SEQUENCE_RESET, header, &extra)
}
pub fn encode_logout(
out: &mut Vec<u8>,
version: FixVersion,
header: FixSessionHeader<'_>,
text: Option<&[u8]>,
) -> Result<(), FixEncodeError> {
let mut extra = [(FixTag::TEXT, b"".as_slice())];
let extra_len = if let Some(text) = text {
extra[0] = (FixTag::TEXT, text);
1
} else {
0
};
encode_session_message(
out,
version,
FixMsgType::LOGOUT,
header,
&extra[..extra_len],
)
}
pub fn encode_new_order_single(
out: &mut Vec<u8>,
version: FixVersion,
header: FixSessionHeader<'_>,
request: FixNewOrderSingle<'_>,
) -> Result<(), FixEncodeError> {
let mut fields = [
(FixTag::CL_ORD_ID, request.cl_ord_id),
(FixTag::ACCOUNT, b"".as_slice()),
(FixTag::SYMBOL, request.symbol),
(FixTag::SIDE, request.side.as_bytes()),
(FixTag::TRANSACT_TIME, request.transact_time),
(FixTag::ORDER_QTY, request.order_qty),
(FixTag::ORD_TYPE, request.ord_type.as_bytes()),
(FixTag::PRICE, b"".as_slice()),
(FixTag::STOP_PX, b"".as_slice()),
(FixTag::TIME_IN_FORCE, b"".as_slice()),
];
let mut len = 1usize;
if let Some(account) = request.account {
fields[len] = (FixTag::ACCOUNT, account);
len += 1;
}
fields[len] = (FixTag::SYMBOL, request.symbol);
len += 1;
fields[len] = (FixTag::SIDE, request.side.as_bytes());
len += 1;
fields[len] = (FixTag::TRANSACT_TIME, request.transact_time);
len += 1;
fields[len] = (FixTag::ORDER_QTY, request.order_qty);
len += 1;
fields[len] = (FixTag::ORD_TYPE, request.ord_type.as_bytes());
len += 1;
if let Some(price) = request.price {
fields[len] = (FixTag::PRICE, price);
len += 1;
}
if let Some(stop_px) = request.stop_px {
fields[len] = (FixTag::STOP_PX, stop_px);
len += 1;
}
if let Some(time_in_force) = request.time_in_force {
fields[len] = (FixTag::TIME_IN_FORCE, time_in_force.as_bytes());
len += 1;
}
encode_session_message(
out,
version,
FixMsgType::NEW_ORDER_SINGLE,
header,
&fields[..len],
)
}
pub fn encode_order_cancel_request(
out: &mut Vec<u8>,
version: FixVersion,
header: FixSessionHeader<'_>,
request: FixOrderCancelRequest<'_>,
) -> Result<(), FixEncodeError> {
let mut fields = [
(FixTag::ORIG_CL_ORD_ID, request.orig_cl_ord_id),
(FixTag::CL_ORD_ID, request.cl_ord_id),
(FixTag::ACCOUNT, b"".as_slice()),
(FixTag::SYMBOL, request.symbol),
(FixTag::SIDE, request.side.as_bytes()),
(FixTag::TRANSACT_TIME, request.transact_time),
];
let mut len = 2usize;
if let Some(account) = request.account {
fields[len] = (FixTag::ACCOUNT, account);
len += 1;
}
fields[len] = (FixTag::SYMBOL, request.symbol);
len += 1;
fields[len] = (FixTag::SIDE, request.side.as_bytes());
len += 1;
fields[len] = (FixTag::TRANSACT_TIME, request.transact_time);
len += 1;
encode_session_message(
out,
version,
FixMsgType::ORDER_CANCEL_REQUEST,
header,
&fields[..len],
)
}
pub fn encode_order_cancel_replace_request(
out: &mut Vec<u8>,
version: FixVersion,
header: FixSessionHeader<'_>,
request: FixOrderCancelReplaceRequest<'_>,
) -> Result<(), FixEncodeError> {
let mut fields = [
(FixTag::ORIG_CL_ORD_ID, request.orig_cl_ord_id),
(FixTag::CL_ORD_ID, request.cl_ord_id),
(FixTag::ACCOUNT, b"".as_slice()),
(FixTag::SYMBOL, request.symbol),
(FixTag::SIDE, request.side.as_bytes()),
(FixTag::TRANSACT_TIME, request.transact_time),
(FixTag::ORDER_QTY, request.order_qty),
(FixTag::ORD_TYPE, request.ord_type.as_bytes()),
(FixTag::PRICE, b"".as_slice()),
(FixTag::STOP_PX, b"".as_slice()),
(FixTag::TIME_IN_FORCE, b"".as_slice()),
];
let mut len = 2usize;
if let Some(account) = request.account {
fields[len] = (FixTag::ACCOUNT, account);
len += 1;
}
fields[len] = (FixTag::SYMBOL, request.symbol);
len += 1;
fields[len] = (FixTag::SIDE, request.side.as_bytes());
len += 1;
fields[len] = (FixTag::TRANSACT_TIME, request.transact_time);
len += 1;
fields[len] = (FixTag::ORDER_QTY, request.order_qty);
len += 1;
fields[len] = (FixTag::ORD_TYPE, request.ord_type.as_bytes());
len += 1;
if let Some(price) = request.price {
fields[len] = (FixTag::PRICE, price);
len += 1;
}
if let Some(stop_px) = request.stop_px {
fields[len] = (FixTag::STOP_PX, stop_px);
len += 1;
}
if let Some(time_in_force) = request.time_in_force {
fields[len] = (FixTag::TIME_IN_FORCE, time_in_force.as_bytes());
len += 1;
}
encode_session_message(
out,
version,
FixMsgType::ORDER_CANCEL_REPLACE_REQUEST,
header,
&fields[..len],
)
}
pub fn encode_order_status_request(
out: &mut Vec<u8>,
version: FixVersion,
header: FixSessionHeader<'_>,
request: FixOrderStatusRequest<'_>,
) -> Result<(), FixEncodeError> {
let mut fields = [
(FixTag::CL_ORD_ID, request.cl_ord_id),
(FixTag::ORDER_ID, b"".as_slice()),
];
let mut len = 1usize;
if let Some(order_id) = request.order_id {
fields[len] = (FixTag::ORDER_ID, order_id);
len += 1;
}
encode_session_message(
out,
version,
FixMsgType::ORDER_STATUS_REQUEST,
header,
&fields[..len],
)
}
pub fn encode_order_mass_cancel_request(
out: &mut Vec<u8>,
version: FixVersion,
header: FixSessionHeader<'_>,
request: FixOrderMassCancelRequest<'_>,
) -> Result<(), FixEncodeError> {
let mut fields = [
(FixTag::CL_ORD_ID, request.cl_ord_id),
(
FixTag::MASS_CANCEL_REQUEST_TYPE,
request.mass_cancel_request_type.as_bytes(),
),
(FixTag::TRANSACT_TIME, request.transact_time),
(FixTag::SECONDARY_CL_ORD_ID, b"".as_slice()),
(FixTag::TRADING_SESSION_ID, b"".as_slice()),
(FixTag::TRADING_SESSION_SUB_ID, b"".as_slice()),
(FixTag::SYMBOL, b"".as_slice()),
(FixTag::SIDE, b"".as_slice()),
(FixTag::TEXT, b"".as_slice()),
];
let mut len = 3usize;
if let Some(secondary_cl_ord_id) = request.secondary_cl_ord_id {
fields[len] = (FixTag::SECONDARY_CL_ORD_ID, secondary_cl_ord_id);
len += 1;
}
if let Some(trading_session_id) = request.trading_session_id {
fields[len] = (FixTag::TRADING_SESSION_ID, trading_session_id);
len += 1;
}
if let Some(trading_session_sub_id) = request.trading_session_sub_id {
fields[len] = (FixTag::TRADING_SESSION_SUB_ID, trading_session_sub_id);
len += 1;
}
if let Some(symbol) = request.symbol {
fields[len] = (FixTag::SYMBOL, symbol);
len += 1;
}
if let Some(side) = request.side {
fields[len] = (FixTag::SIDE, side.as_bytes());
len += 1;
}
if let Some(text) = request.text {
fields[len] = (FixTag::TEXT, text);
len += 1;
}
encode_session_message(
out,
version,
FixMsgType::ORDER_MASS_CANCEL_REQUEST,
header,
&fields[..len],
)
}
pub fn encode_order_mass_status_request(
out: &mut Vec<u8>,
version: FixVersion,
header: FixSessionHeader<'_>,
request: FixOrderMassStatusRequest<'_>,
) -> Result<(), FixEncodeError> {
let mut fields = [
(FixTag::MASS_STATUS_REQ_ID, request.mass_status_req_id),
(
FixTag::MASS_STATUS_REQ_TYPE,
request.mass_status_req_type.as_bytes(),
),
(FixTag::ACCOUNT, b"".as_slice()),
(FixTag::ACCT_ID_SOURCE, b"".as_slice()),
(FixTag::TRADING_SESSION_ID, b"".as_slice()),
(FixTag::TRADING_SESSION_SUB_ID, b"".as_slice()),
(FixTag::SYMBOL, b"".as_slice()),
(FixTag::SIDE, b"".as_slice()),
];
let mut len = 2usize;
if let Some(account) = request.account {
fields[len] = (FixTag::ACCOUNT, account);
len += 1;
}
if let Some(acct_id_source) = request.acct_id_source {
fields[len] = (FixTag::ACCT_ID_SOURCE, acct_id_source);
len += 1;
}
if let Some(trading_session_id) = request.trading_session_id {
fields[len] = (FixTag::TRADING_SESSION_ID, trading_session_id);
len += 1;
}
if let Some(trading_session_sub_id) = request.trading_session_sub_id {
fields[len] = (FixTag::TRADING_SESSION_SUB_ID, trading_session_sub_id);
len += 1;
}
if let Some(symbol) = request.symbol {
fields[len] = (FixTag::SYMBOL, symbol);
len += 1;
}
if let Some(side) = request.side {
fields[len] = (FixTag::SIDE, side.as_bytes());
len += 1;
}
encode_session_message(
out,
version,
FixMsgType::ORDER_MASS_STATUS_REQUEST,
header,
&fields[..len],
)
}
fn encode_session_message(
out: &mut Vec<u8>,
version: FixVersion,
msg_type: FixMsgType,
header: FixSessionHeader<'_>,
fields: &[(FixTag, &[u8])],
) -> Result<(), FixEncodeError> {
let mut seq_no = [0u8; 20];
let seq_len = write_u64_digits(&mut seq_no, header.msg_seq_num());
let header_fields = [
(FixTag::SENDER_COMP_ID, header.sender_comp_id()),
(FixTag::TARGET_COMP_ID, header.target_comp_id()),
(FixTag::MSG_SEQ_NUM, &seq_no[..seq_len]),
(FixTag::SENDING_TIME, header.sending_time()),
];
encode_message_parts(
out,
version.as_bytes(),
msg_type.as_bytes(),
&header_fields,
fields,
)
}
fn encode_message_parts(
out: &mut Vec<u8>,
begin_string: &[u8],
msg_type: &[u8],
header_fields: &[(FixTag, &[u8])],
fields: &[(FixTag, &[u8])],
) -> Result<(), FixEncodeError> {
encode_message_parts_with_repeating_group(
out,
begin_string,
msg_type,
header_fields,
fields,
None,
&[],
)
}
fn encode_message_parts_with_repeating_group(
out: &mut Vec<u8>,
begin_string: &[u8],
msg_type: &[u8],
header_fields: &[(FixTag, &[u8])],
fields: &[(FixTag, &[u8])],
definition: Option<FixRepeatingGroupDefinition<'_>>,
groups: &[&[(FixTag, &[u8])]],
) -> Result<(), FixEncodeError> {
validate_value(FixTag::BEGIN_STRING, begin_string)?;
validate_value(FixTag::MSG_TYPE, msg_type)?;
if let Some(definition) = definition {
if matches!(
definition.count_tag,
FixTag::BEGIN_STRING | FixTag::BODY_LENGTH | FixTag::MSG_TYPE | FixTag::CHECK_SUM
) {
return Err(FixEncodeError::ReservedTag(definition.count_tag));
}
if header_fields
.iter()
.chain(fields.iter())
.any(|(tag, _)| *tag == definition.count_tag)
{
return Err(FixEncodeError::DuplicateRepeatingGroupCountTag(
definition.count_tag,
));
}
validate_repeating_group(definition, groups)?;
}
out.clear();
write_field(out, FixTag::BEGIN_STRING, begin_string);
write_field(out, FixTag::BODY_LENGTH, b"0000000000");
let body_start = out.len();
write_field(out, FixTag::MSG_TYPE, msg_type);
for (tag, value) in header_fields.iter().chain(fields.iter()) {
if matches!(
*tag,
FixTag::BEGIN_STRING | FixTag::BODY_LENGTH | FixTag::MSG_TYPE | FixTag::CHECK_SUM
) {
return Err(FixEncodeError::ReservedTag(*tag));
}
validate_value(*tag, value)?;
write_field(out, *tag, value);
}
if let Some(definition) = definition {
let mut count = [0u8; 20];
let count_len = write_u64_digits(&mut count, usize_to_u64(groups.len()));
write_field(out, definition.count_tag, &count[..count_len]);
for group in groups {
for (tag, value) in group.iter().copied() {
write_field(out, tag, value);
}
}
}
let body_len = out.len().saturating_sub(body_start);
patch_body_length(out, body_len);
let sum = checksum(out);
write_checksum(out, sum);
Ok(())
}
fn validate_repeating_group(
definition: FixRepeatingGroupDefinition<'_>,
groups: &[&[(FixTag, &[u8])]],
) -> Result<(), FixEncodeError> {
for (group_index, group) in groups.iter().enumerate() {
let first = group
.first()
.ok_or(FixEncodeError::MissingRepeatingGroupDelimiter {
group: group_index,
tag: definition.delimiter_tag,
})?;
if first.0 != definition.delimiter_tag {
return Err(FixEncodeError::MissingRepeatingGroupDelimiter {
group: group_index,
tag: definition.delimiter_tag,
});
}
for (field_index, &(tag, value)) in group.iter().enumerate() {
if matches!(
tag,
FixTag::BEGIN_STRING | FixTag::BODY_LENGTH | FixTag::MSG_TYPE | FixTag::CHECK_SUM
) {
return Err(FixEncodeError::ReservedTag(tag));
}
if !definition.allows(tag) {
return Err(FixEncodeError::InvalidRepeatingGroupField {
group: group_index,
index: field_index,
tag,
});
}
if field_index > 0 && tag == definition.delimiter_tag {
return Err(FixEncodeError::RepeatedRepeatingGroupDelimiter {
group: group_index,
index: field_index,
tag,
});
}
validate_value(tag, value)?;
}
}
Ok(())
}
pub fn checksum(bytes: &[u8]) -> u8 {
bytes
.iter()
.fold(0u32, |acc, byte| acc.wrapping_add(u32::from(*byte))) as u8
}
fn hash_bytes(bytes: &[u8]) -> u64 {
let mut hash = FNV_OFFSET_BASIS;
for byte in bytes {
hash ^= u64::from(*byte);
hash = hash.wrapping_mul(FNV_PRIME);
}
hash
}
fn usize_to_u64(value: usize) -> u64 {
u64::try_from(value).unwrap_or(u64::MAX)
}
fn hash_u64(mut hash: u64, value: u64) -> u64 {
for byte in value.to_le_bytes() {
hash ^= u64::from(byte);
hash = hash.wrapping_mul(FNV_PRIME);
}
hash
}
fn hash_bytes_into(mut hash: u64, bytes: &[u8]) -> u64 {
for byte in bytes {
hash ^= u64::from(*byte);
hash = hash.wrapping_mul(FNV_PRIME);
}
hash
}
fn sequence_snapshot_checksum_owned(snapshot: &FixOwnedSequenceSnapshot) -> u64 {
let mut hash = FNV_OFFSET_BASIS;
hash = hash_bytes_into(hash, snapshot.session_id.version().as_bytes());
hash = hash_bytes_into(hash, snapshot.session_id.sender_comp_id());
hash = hash_bytes_into(hash, snapshot.session_id.target_comp_id());
hash = hash_bytes_into(hash, snapshot.session_id.qualifier());
hash = hash_u64(hash, snapshot.next_inbound());
hash = hash_u64(hash, snapshot.next_outbound());
hash_bytes_into(hash, snapshot.trading_day())
}
fn encode_sequence_snapshot(
snapshot: &FixOwnedSequenceSnapshot,
) -> Result<Vec<u8>, FixSequenceStoreError> {
let version = snapshot.session_id.version().as_bytes();
let sender = snapshot.session_id.sender_comp_id();
let target = snapshot.session_id.target_comp_id();
let qualifier = snapshot.session_id.qualifier();
let trading_day = snapshot.trading_day();
let capacity = SEQUENCE_SNAPSHOT_MAGIC.len()
+ 2
+ 8
+ 8
+ 8
+ 5 * 2
+ version.len()
+ sender.len()
+ target.len()
+ qualifier.len()
+ trading_day.len();
let mut out = Vec::with_capacity(capacity);
out.extend_from_slice(SEQUENCE_SNAPSHOT_MAGIC);
put_snapshot_u16(&mut out, SEQUENCE_SNAPSHOT_VERSION);
put_snapshot_u64(&mut out, snapshot.next_inbound());
put_snapshot_u64(&mut out, snapshot.next_outbound());
put_snapshot_bytes(&mut out, version)?;
put_snapshot_bytes(&mut out, sender)?;
put_snapshot_bytes(&mut out, target)?;
put_snapshot_bytes(&mut out, qualifier)?;
put_snapshot_bytes(&mut out, trading_day)?;
put_snapshot_u64(&mut out, snapshot.checksum());
Ok(out)
}
fn decode_sequence_snapshot(
bytes: &[u8],
) -> Result<FixOwnedSequenceSnapshot, FixSequenceStoreError> {
let mut cursor = SnapshotCursor::new(bytes);
if cursor.read_exact(SEQUENCE_SNAPSHOT_MAGIC.len())? != SEQUENCE_SNAPSHOT_MAGIC {
return Err(FixSequenceStoreError::InvalidMagic);
}
let version = cursor.read_u16()?;
if version != SEQUENCE_SNAPSHOT_VERSION {
return Err(FixSequenceStoreError::UnsupportedVersion(version));
}
let next_inbound = cursor.read_u64()?;
let next_outbound = cursor.read_u64()?;
let begin_string = cursor.read_vec()?;
let sender_comp_id = cursor.read_vec()?;
let target_comp_id = cursor.read_vec()?;
let qualifier = cursor.read_vec()?;
let trading_day = cursor.read_vec()?;
let expected_checksum = cursor.read_u64()?;
if !cursor.is_done() {
return Err(FixSequenceStoreError::Truncated);
}
let version =
FixVersion::from_bytes(&begin_string).ok_or(FixSequenceStoreError::InvalidVersion)?;
let mut snapshot = FixOwnedSequenceSnapshot::new(
FixOwnedSessionId::with_qualifier(version, sender_comp_id, target_comp_id, qualifier)?,
next_inbound,
next_outbound,
trading_day,
)?;
let actual = snapshot.checksum();
if expected_checksum != actual {
return Err(FixSequenceStoreError::ChecksumMismatch {
expected: expected_checksum,
actual,
});
}
snapshot.checksum = expected_checksum;
Ok(snapshot)
}
fn put_snapshot_u16(out: &mut Vec<u8>, value: u16) {
out.extend_from_slice(&value.to_le_bytes());
}
fn put_snapshot_u64(out: &mut Vec<u8>, value: u64) {
out.extend_from_slice(&value.to_le_bytes());
}
fn put_snapshot_bytes(out: &mut Vec<u8>, value: &[u8]) -> Result<(), FixSequenceStoreError> {
let len = u16::try_from(value.len()).map_err(|_| FixSequenceStoreError::FieldTooLarge)?;
put_snapshot_u16(out, len);
out.extend_from_slice(value);
Ok(())
}
fn io_error(err: std::io::Error) -> FixSequenceStoreError {
FixSequenceStoreError::Io(err.to_string())
}
struct SnapshotCursor<'a> {
bytes: &'a [u8],
offset: usize,
}
impl<'a> SnapshotCursor<'a> {
const fn new(bytes: &'a [u8]) -> Self {
Self { bytes, offset: 0 }
}
fn read_exact(&mut self, len: usize) -> Result<&'a [u8], FixSequenceStoreError> {
let end = self
.offset
.checked_add(len)
.ok_or(FixSequenceStoreError::Truncated)?;
if end > self.bytes.len() {
return Err(FixSequenceStoreError::Truncated);
}
let value = &self.bytes[self.offset..end];
self.offset = end;
Ok(value)
}
fn read_u16(&mut self) -> Result<u16, FixSequenceStoreError> {
let bytes = self.read_exact(2)?;
Ok(u16::from_le_bytes([bytes[0], bytes[1]]))
}
fn read_u64(&mut self) -> Result<u64, FixSequenceStoreError> {
let bytes = self.read_exact(8)?;
Ok(u64::from_le_bytes([
bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],
]))
}
fn read_vec(&mut self) -> Result<Vec<u8>, FixSequenceStoreError> {
let len = usize::from(self.read_u16()?);
Ok(self.read_exact(len)?.to_vec())
}
const fn is_done(&self) -> bool {
self.offset == self.bytes.len()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct DurableResendRecord {
seq_no: u64,
kind: FixSentMessageKind,
raw: Vec<u8>,
encoded_bytes: u64,
checksum: u64,
}
fn encode_durable_resend_record(
out: &mut Vec<u8>,
seq_no: u64,
kind: FixSentMessageKind,
raw: &[u8],
previous_checksum: u64,
) -> Result<(), FixDurableResendStoreError> {
let raw_len =
u32::try_from(raw.len()).map_err(|_| FixDurableResendStoreError::FrameTooLarge)?;
let raw_hash = hash_bytes(raw);
let frame_checksum = durable_resend_frame_checksum(seq_no, kind, raw, previous_checksum);
out.clear();
out.reserve(DURABLE_RESEND_MAGIC.len() + 2 + 1 + 8 + 4 + 8 + 8 + 8 + raw.len());
out.extend_from_slice(DURABLE_RESEND_MAGIC);
put_snapshot_u16(out, DURABLE_RESEND_VERSION);
out.push(durable_resend_kind_to_byte(kind));
put_snapshot_u64(out, seq_no);
out.extend_from_slice(&raw_len.to_le_bytes());
put_snapshot_u64(out, raw_hash);
put_snapshot_u64(out, previous_checksum);
put_snapshot_u64(out, frame_checksum);
out.extend_from_slice(raw);
Ok(())
}
fn durable_resend_frame_checksum(
seq_no: u64,
kind: FixSentMessageKind,
raw: &[u8],
previous_checksum: u64,
) -> u64 {
let mut hash = FNV_OFFSET_BASIS;
hash = hash_u64(hash, u64::from(DURABLE_RESEND_VERSION));
hash ^= u64::from(durable_resend_kind_to_byte(kind));
hash = hash.wrapping_mul(FNV_PRIME);
hash = hash_u64(hash, seq_no);
hash = hash_u64(hash, usize_to_u64(raw.len()));
hash = hash_u64(hash, hash_bytes(raw));
hash = hash_u64(hash, previous_checksum);
hash_bytes_into(hash, raw)
}
fn durable_resend_kind_to_byte(kind: FixSentMessageKind) -> u8 {
match kind {
FixSentMessageKind::Application => 1,
FixSentMessageKind::Administrative => 2,
FixSentMessageKind::Reject => 3,
}
}
fn durable_resend_kind_from_byte(
kind: u8,
) -> Result<FixSentMessageKind, FixDurableResendStoreError> {
match kind {
1 => Ok(FixSentMessageKind::Application),
2 => Ok(FixSentMessageKind::Administrative),
3 => Ok(FixSentMessageKind::Reject),
_ => Err(FixDurableResendStoreError::InvalidKind(kind)),
}
}
fn inspect_durable_resend_path(
path: &Path,
) -> Result<FixDurableResendReplayReport, FixDurableResendStoreError> {
if !path.exists() {
return Ok(FixDurableResendReplayReport::default());
}
let records = read_durable_resend_records(path)?;
let mut report = FixDurableResendReplayReport::default();
for record in records {
report.records = report.records.saturating_add(1);
report.bytes = report.bytes.saturating_add(record.encoded_bytes);
report.first_seq_no.get_or_insert(record.seq_no);
report.last_seq_no = Some(record.seq_no);
report.checksum = record.checksum;
}
Ok(report)
}
fn read_durable_resend_records(
path: &Path,
) -> Result<Vec<DurableResendRecord>, FixDurableResendStoreError> {
if !path.exists() {
return Ok(Vec::new());
}
let mut file = File::open(path).map_err(durable_resend_io_error)?;
let mut bytes = Vec::new();
file.read_to_end(&mut bytes)
.map_err(durable_resend_io_error)?;
decode_durable_resend_records(&bytes)
}
fn decode_durable_resend_records(
bytes: &[u8],
) -> Result<Vec<DurableResendRecord>, FixDurableResendStoreError> {
let mut cursor = DurableResendCursor::new(bytes);
let mut records = Vec::new();
let mut previous_checksum = 0_u64;
let mut latest_seq_no = None;
while !cursor.is_done() {
let offset = cursor.offset;
if cursor.read_exact(DURABLE_RESEND_MAGIC.len())? != DURABLE_RESEND_MAGIC {
return Err(FixDurableResendStoreError::InvalidMagic);
}
let version = cursor.read_u16()?;
if version != DURABLE_RESEND_VERSION {
return Err(FixDurableResendStoreError::UnsupportedVersion(version));
}
let kind = durable_resend_kind_from_byte(cursor.read_u8()?)?;
let seq_no = cursor.read_u64()?;
if let Some(latest) = latest_seq_no {
if seq_no <= latest {
return Err(FixDurableResendStoreError::SequenceRegression {
latest,
received: seq_no,
});
}
}
let raw_len = usize::try_from(cursor.read_u32()?)
.map_err(|_| FixDurableResendStoreError::FrameTooLarge)?;
let expected_raw_hash = cursor.read_u64()?;
let expected_previous_checksum = cursor.read_u64()?;
if expected_previous_checksum != previous_checksum {
return Err(FixDurableResendStoreError::PreviousChecksumMismatch {
expected: expected_previous_checksum,
actual: previous_checksum,
});
}
let expected_frame_checksum = cursor.read_u64()?;
let raw = cursor.read_exact(raw_len)?.to_vec();
let actual_raw_hash = hash_bytes(&raw);
if expected_raw_hash != actual_raw_hash {
return Err(FixDurableResendStoreError::RawHashMismatch {
expected: expected_raw_hash,
actual: actual_raw_hash,
});
}
let actual_frame_checksum =
durable_resend_frame_checksum(seq_no, kind, &raw, previous_checksum);
if expected_frame_checksum != actual_frame_checksum {
return Err(FixDurableResendStoreError::FrameChecksumMismatch {
expected: expected_frame_checksum,
actual: actual_frame_checksum,
});
}
previous_checksum = actual_frame_checksum;
latest_seq_no = Some(seq_no);
records.push(DurableResendRecord {
seq_no,
kind,
raw,
encoded_bytes: usize_to_u64(cursor.offset.saturating_sub(offset)),
checksum: actual_frame_checksum,
});
}
Ok(records)
}
fn durable_resend_io_error(err: std::io::Error) -> FixDurableResendStoreError {
FixDurableResendStoreError::Io(err.to_string())
}
struct DurableResendCursor<'a> {
bytes: &'a [u8],
offset: usize,
}
impl<'a> DurableResendCursor<'a> {
const fn new(bytes: &'a [u8]) -> Self {
Self { bytes, offset: 0 }
}
fn read_exact(&mut self, len: usize) -> Result<&'a [u8], FixDurableResendStoreError> {
let end = self
.offset
.checked_add(len)
.ok_or(FixDurableResendStoreError::Truncated)?;
if end > self.bytes.len() {
return Err(FixDurableResendStoreError::Truncated);
}
let value = &self.bytes[self.offset..end];
self.offset = end;
Ok(value)
}
fn read_u8(&mut self) -> Result<u8, FixDurableResendStoreError> {
Ok(self.read_exact(1)?[0])
}
fn read_u16(&mut self) -> Result<u16, FixDurableResendStoreError> {
let bytes = self.read_exact(2)?;
Ok(u16::from_le_bytes([bytes[0], bytes[1]]))
}
fn read_u32(&mut self) -> Result<u32, FixDurableResendStoreError> {
let bytes = self.read_exact(4)?;
Ok(u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]))
}
fn read_u64(&mut self) -> Result<u64, FixDurableResendStoreError> {
let bytes = self.read_exact(8)?;
Ok(u64::from_le_bytes([
bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],
]))
}
const fn is_done(&self) -> bool {
self.offset == self.bytes.len()
}
}
fn update_transcript_hash(
mut hash: u64,
ordinal: u64,
timestamp_ns: u64,
direction: FixTranscriptDirection,
seq_no: Option<u64>,
msg_type: &[u8],
raw: &[u8],
) -> u64 {
hash = hash_u64(hash, ordinal);
hash = hash_u64(hash, timestamp_ns);
hash ^= u64::from(direction.as_byte());
hash = hash.wrapping_mul(FNV_PRIME);
hash = hash_u64(hash, seq_no.unwrap_or(0));
hash = hash_bytes_into(hash, msg_type);
hash_bytes_into(hash, raw)
}
pub fn debug_render(raw: &[u8]) -> String {
raw.iter()
.map(|b| if *b == SOH { '|' } else { *b as char })
.collect()
}
fn write_field(out: &mut Vec<u8>, tag: FixTag, value: &[u8]) {
write_u32(out, tag.0);
out.push(b'=');
out.extend_from_slice(value);
out.push(SOH);
}
fn write_replay_header(out: &mut Vec<u8>, sending_time: &[u8], orig_sending_time: &[u8]) {
write_field(out, FixTag::POSS_DUP_FLAG, b"Y");
write_field(out, FixTag::SENDING_TIME, sending_time);
write_field(out, FixTag::ORIG_SENDING_TIME, orig_sending_time);
}
fn write_checksum(out: &mut Vec<u8>, sum: u8) {
out.extend_from_slice(b"10=");
out.push(b'0' + (sum / 100));
out.push(b'0' + ((sum / 10) % 10));
out.push(b'0' + (sum % 10));
out.push(SOH);
}
fn patch_body_length(out: &mut [u8], body_len: usize) {
let Some(tag_start) = find_tag_start(out, FixTag::BODY_LENGTH) else {
return;
};
let value_start = tag_start + 2;
let mut digits = [b'0'; 10];
write_usize_padded(&mut digits, body_len);
out[value_start..value_start + digits.len()].copy_from_slice(&digits);
}
fn find_tag_start(raw: &[u8], tag: FixTag) -> Option<usize> {
let mut pos = 0usize;
while pos < raw.len() {
let end = raw[pos..]
.iter()
.position(|b| *b == SOH)
.map(|offset| pos + offset)
.unwrap_or(raw.len());
let field = &raw[pos..end];
if let Some(eq) = field.iter().position(|b| *b == b'=') {
if parse_u32(&field[..eq]).ok().map(FixTag) == Some(tag) {
return Some(pos);
}
}
pos = end.saturating_add(1);
}
None
}
fn validate_value(tag: FixTag, value: &[u8]) -> Result<(), FixEncodeError> {
if value.contains(&SOH) {
Err(FixEncodeError::ValueContainsSoh(tag))
} else {
Ok(())
}
}
fn parse_required_u64(
message: &FixMessageView<'_>,
tag: FixTag,
) -> Result<u64, FixRejectParseError> {
parse_u64(
message
.get(tag)
.ok_or(FixRejectParseError::MissingTag(tag))?,
)
.map_err(|()| FixRejectParseError::InvalidNumber(tag))
}
fn parse_optional_reject_u64(
message: &FixMessageView<'_>,
tag: FixTag,
) -> Result<Option<u64>, FixRejectParseError> {
if let Some(value) = message.get(tag) {
parse_u64(value)
.map(Some)
.map_err(|()| FixRejectParseError::InvalidNumber(tag))
} else {
Ok(None)
}
}
fn parse_optional_fix_tag(
message: &FixMessageView<'_>,
tag: FixTag,
) -> Result<Option<FixTag>, FixRejectParseError> {
if let Some(value) = message.get(tag) {
parse_u32(value)
.map(|value| Some(FixTag(value)))
.map_err(|()| FixRejectParseError::InvalidNumber(tag))
} else {
Ok(None)
}
}
fn push_gap_fill<'a>(
out: &mut Vec<FixResendAction<'a>>,
begin_seq_no: u64,
end_seq_no: u64,
gap_fill_messages: &mut u64,
gap_fill_sequences: &mut u64,
) {
if begin_seq_no == 0 || begin_seq_no > end_seq_no {
return;
}
out.push(FixResendAction::GapFill {
begin_seq_no,
end_seq_no,
});
*gap_fill_messages = gap_fill_messages.saturating_add(1);
*gap_fill_sequences = gap_fill_sequences
.saturating_add(end_seq_no.saturating_sub(begin_seq_no).saturating_add(1));
}
const fn clamp_seq_no(value: u64) -> u64 {
if value == 0 {
1
} else {
value
}
}
fn parse_checksum(value: &[u8]) -> Result<u8, FixParseError> {
if value.len() != 3 || !value.iter().all(u8::is_ascii_digit) {
return Err(FixParseError::InvalidChecksum);
}
Ok((value[0] - b'0') * 100 + (value[1] - b'0') * 10 + (value[2] - b'0'))
}
fn parse_u32(bytes: &[u8]) -> Result<u32, ()> {
let mut out = 0u32;
if bytes.is_empty() {
return Err(());
}
for byte in bytes {
if !byte.is_ascii_digit() {
return Err(());
}
out = out
.checked_mul(10)
.and_then(|v| v.checked_add(u32::from(*byte - b'0')))
.ok_or(())?;
}
Ok(out)
}
fn parse_u64(bytes: &[u8]) -> Result<u64, ()> {
let mut out = 0u64;
if bytes.is_empty() {
return Err(());
}
for byte in bytes {
if !byte.is_ascii_digit() {
return Err(());
}
out = out
.checked_mul(10)
.and_then(|v| v.checked_add(u64::from(*byte - b'0')))
.ok_or(())?;
}
Ok(out)
}
fn parse_usize(bytes: &[u8]) -> Result<usize, ()> {
let parsed = parse_u64(bytes)?;
usize::try_from(parsed).map_err(|_| ())
}
fn write_u32(out: &mut Vec<u8>, value: u32) {
let mut digits = [0u8; 10];
let len = write_u32_digits(&mut digits, value);
out.extend_from_slice(&digits[..len]);
}
fn write_usize_padded(out: &mut [u8; 10], value: usize) {
let mut digits = [0u8; 20];
let len = write_usize_digits(&mut digits, value);
let out_len = out.len();
let copied_len = len.min(out_len);
let start = out_len.saturating_sub(copied_len);
let digit_start = len.saturating_sub(out_len);
out[start..].copy_from_slice(&digits[digit_start..len]);
}
fn write_u32_digits(out: &mut [u8; 10], mut value: u32) -> usize {
if value == 0 {
out[0] = b'0';
return 1;
}
let mut tmp = [0u8; 10];
let mut len = 0usize;
while value != 0 {
tmp[len] = b'0' + (value % 10) as u8;
value /= 10;
len += 1;
}
for i in 0..len {
out[i] = tmp[len - 1 - i];
}
len
}
fn write_u64_digits(out: &mut [u8; 20], mut value: u64) -> usize {
if value == 0 {
out[0] = b'0';
return 1;
}
let mut tmp = [0u8; 20];
let mut len = 0usize;
while value != 0 {
tmp[len] = b'0' + (value % 10) as u8;
value /= 10;
len += 1;
}
for i in 0..len {
out[i] = tmp[len - 1 - i];
}
len
}
fn write_usize_digits(out: &mut [u8; 20], mut value: usize) -> usize {
if value == 0 {
out[0] = b'0';
return 1;
}
let mut tmp = [0u8; 20];
let mut len = 0usize;
while value != 0 {
tmp[len] = b'0' + (value % 10) as u8;
value /= 10;
len += 1;
}
for i in 0..len {
out[i] = tmp[len - 1 - i];
}
len
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn encodes_and_parses_heartbeat() {
let mut raw = Vec::new();
encode_message(
&mut raw,
b"FIX.4.4",
b"0",
&[(FixTag::MSG_SEQ_NUM, b"1".as_slice())],
)
.expect("encode");
let mut scratch = [FixFieldView::empty(); 16];
let message = parse_message(&raw, &mut scratch).expect("parse");
assert_eq!(
message.get(FixTag::BEGIN_STRING),
Some(b"FIX.4.4".as_slice())
);
assert_eq!(message.msg_type(), Some(b"0".as_slice()));
assert_eq!(message.msg_seq_num(), Some(1));
assert!(!message.poss_dup());
assert!(message.debug_render().contains("35=0|"));
}
#[test]
fn detects_body_length_mismatch() {
let raw = b"8=FIX.4.4\x019=1\x0135=0\x0134=1\x0110=222\x01";
let mut scratch = [FixFieldView::empty(); 16];
let err = parse_message(raw, &mut scratch).expect_err("body length mismatch");
assert!(matches!(err, FixParseError::BodyLengthMismatch { .. }));
}
#[test]
fn detects_checksum_mismatch() {
let mut raw = Vec::new();
encode_message(&mut raw, b"FIX.4.4", b"0", &[]).expect("encode");
let len = raw.len();
raw[len - 3] = b'9';
let mut scratch = [FixFieldView::empty(); 16];
let err = parse_message(&raw, &mut scratch).expect_err("checksum mismatch");
assert!(matches!(err, FixParseError::ChecksumMismatch { .. }));
}
#[test]
fn rejects_too_small_scratch() {
let mut raw = Vec::new();
encode_message(&mut raw, b"FIX.4.4", b"0", &[]).expect("encode");
let mut scratch = [FixFieldView::empty(); 2];
let err = parse_message(&raw, &mut scratch).expect_err("scratch too small");
assert!(matches!(err, FixParseError::ScratchTooSmall { .. }));
}
#[test]
fn rejects_reserved_encode_tags() {
let mut raw = Vec::new();
let err = encode_message(
&mut raw,
b"FIX.4.4",
b"D",
&[(FixTag::CHECK_SUM, b"001".as_slice())],
)
.expect_err("reserved tag");
assert_eq!(err, FixEncodeError::ReservedTag(FixTag::CHECK_SUM));
}
#[test]
fn rejects_soh_in_values() {
let mut raw = Vec::new();
let err = encode_message(&mut raw, b"FIX.4.4", b"D\x01", &[]).expect_err("soh");
assert_eq!(err, FixEncodeError::ValueContainsSoh(FixTag::MSG_TYPE));
}
#[test]
fn typed_encoder_decoder_round_trip() {
let mut encoder = FixEncoder::with_capacity(128);
let raw = encoder
.encode_typed(
FixVersion::Fix44,
FixMsgType::NEW_ORDER_SINGLE,
&[
(FixTag::MSG_SEQ_NUM, b"7".as_slice()),
(FixTag::CL_ORD_ID, b"ORD-1".as_slice()),
],
)
.expect("encode");
let decoder = FixDecoder::new();
let mut scratch = [FixFieldView::empty(); 16];
let message = decoder.parse(raw, &mut scratch).expect("parse");
assert_eq!(message.version(), Some(FixVersion::Fix44));
assert_eq!(message.typed_msg_type(), Some(FixMsgType::NEW_ORDER_SINGLE));
assert_eq!(message.msg_seq_num(), Some(7));
}
#[test]
fn repeating_group_round_trips_without_allocating_group_entries() {
static PARTY_FIELDS: &[FixTag] = &[FixTag(448), FixTag(447), FixTag(452)];
let definition = FixRepeatingGroupDefinition::new(FixTag(453), FixTag(448), PARTY_FIELDS);
let first = [
(FixTag(448), b"PARTY-A".as_slice()),
(FixTag(447), b"D".as_slice()),
(FixTag(452), b"1".as_slice()),
];
let second = [
(FixTag(448), b"PARTY-B".as_slice()),
(FixTag(447), b"D".as_slice()),
(FixTag(452), b"3".as_slice()),
];
let groups = [&first[..], &second[..]];
let mut raw = Vec::new();
encode_message_with_repeating_group(
&mut raw,
b"FIX.4.4",
b"D",
&[(FixTag::CL_ORD_ID, b"ORD-1".as_slice())],
definition,
&groups,
)
.expect("encode repeating group");
let mut fields = [FixFieldView::empty(); 32];
let message = parse_message(&raw, &mut fields).expect("parse");
let mut entries = [FixRepeatingGroupEntry::empty(); 2];
let view = message
.repeating_group(definition, &mut entries)
.expect("parse repeating group");
assert_eq!(view.len(), 2);
assert_eq!(
view.get(0).and_then(|entry| entry.get(FixTag(448))),
Some(b"PARTY-A".as_slice())
);
assert_eq!(view.iter().count(), 2);
assert_eq!(view.get(1).map(|entry| entry.fields().len()), Some(3));
}
#[test]
fn repeating_group_reports_scratch_and_shape_errors() {
static PARTY_FIELDS: &[FixTag] = &[FixTag(448), FixTag(447)];
let definition = FixRepeatingGroupDefinition::new(FixTag(453), FixTag(448), PARTY_FIELDS);
let mut raw = Vec::new();
encode_message(
&mut raw,
b"FIX.4.4",
b"D",
&[
(FixTag(453), b"1".as_slice()),
(FixTag(447), b"D".as_slice()),
],
)
.expect("encode malformed group");
let mut fields = [FixFieldView::empty(); 16];
let message = parse_message(&raw, &mut fields).expect("parse");
let mut no_entries: [FixRepeatingGroupEntry; 0] = [];
assert!(matches!(
message.repeating_group(definition, &mut no_entries),
Err(FixGroupError::ScratchTooSmall {
required: 1,
capacity: 0
})
));
let mut entries = [FixRepeatingGroupEntry::empty(); 1];
assert!(matches!(
message.repeating_group(definition, &mut entries),
Err(FixGroupError::MissingDelimiter {
index: 0,
tag: FixTag(448)
})
));
let mut raw = Vec::new();
encode_message(
&mut raw,
b"FIX.4.4",
b"D",
&[
(FixTag(453), b"1".as_slice()),
(FixTag(448), b"PARTY".as_slice()),
(FixTag(9999), b"ordinary".as_slice()),
(FixTag(447), b"D".as_slice()),
],
)
.expect("encode non-contiguous group");
let mut fields = [FixFieldView::empty(); 16];
let message = parse_message(&raw, &mut fields).expect("parse non-contiguous group");
let mut entries = [FixRepeatingGroupEntry::empty(); 1];
assert!(matches!(
message.repeating_group(definition, &mut entries),
Err(FixGroupError::UnexpectedField {
index: 6,
tag: FixTag(447)
})
));
}
#[test]
fn repeating_group_encoder_rejects_invalid_entries() {
static PARTY_FIELDS: &[FixTag] = &[FixTag(448), FixTag(447)];
let definition = FixRepeatingGroupDefinition::new(FixTag(453), FixTag(448), PARTY_FIELDS);
let mut raw = Vec::new();
let missing_delimiter = [(FixTag(447), b"D".as_slice())];
let groups = [&missing_delimiter[..]];
assert!(matches!(
encode_message_with_repeating_group(
&mut raw,
b"FIX.4.4",
b"D",
&[],
definition,
&groups,
),
Err(FixEncodeError::MissingRepeatingGroupDelimiter {
group: 0,
tag: FixTag(448)
})
));
let invalid = [
(FixTag(448), b"PARTY".as_slice()),
(FixTag(9999), b"unsupported".as_slice()),
];
let groups = [&invalid[..]];
assert!(matches!(
encode_message_with_repeating_group(
&mut raw,
b"FIX.4.4",
b"D",
&[],
definition,
&groups,
),
Err(FixEncodeError::InvalidRepeatingGroupField {
group: 0,
index: 1,
tag: FixTag(9999)
})
));
let duplicate_count = [(FixTag(453), b"caller-count".as_slice())];
let groups: [&[(FixTag, &[u8])]; 0] = [];
assert!(matches!(
encode_message_with_repeating_group(
&mut raw,
b"FIX.4.4",
b"D",
&duplicate_count,
definition,
&groups,
),
Err(FixEncodeError::DuplicateRepeatingGroupCountTag(FixTag(453)))
));
let repeated_delimiter = [
(FixTag(448), b"PARTY-A".as_slice()),
(FixTag(447), b"D".as_slice()),
(FixTag(448), b"PARTY-B".as_slice()),
];
let groups = [&repeated_delimiter[..]];
assert!(matches!(
encode_message_with_repeating_group(
&mut raw,
b"FIX.4.4",
b"D",
&[],
definition,
&groups,
),
Err(FixEncodeError::RepeatedRepeatingGroupDelimiter {
group: 0,
index: 2,
tag: FixTag(448)
})
));
}
#[test]
fn dictionary_validates_required_tags() {
static REQUIRED: &[FixTag] = &[FixTag::CL_ORD_ID, FixTag::SYMBOL, FixTag::SIDE];
static RULES: &[FixMessageRule<'static>] = &[FixMessageRule::new(
FixMsgType::NEW_ORDER_SINGLE,
REQUIRED,
&[],
)];
let dictionary = FixDictionary::new(FixVersion::Fix44, RULES);
let mut raw = Vec::new();
encode_message(
&mut raw,
b"FIX.4.4",
b"D",
&[
(FixTag::CL_ORD_ID, b"ORD-1".as_slice()),
(FixTag::SYMBOL, b"BTCUSDT".as_slice()),
],
)
.expect("encode");
let mut scratch = [FixFieldView::empty(); 16];
let message = parse_message(&raw, &mut scratch).expect("parse");
let err = dictionary
.validate(&message)
.expect_err("missing side should fail");
assert_eq!(
err,
FixProfileError::MissingRequiredTag {
msg_type: FixMsgType::NEW_ORDER_SINGLE,
tag: FixTag::SIDE,
}
);
}
#[test]
fn dictionary_rejects_disallowed_tags() {
static DISALLOWED: &[FixTag] = &[FixTag::TEXT];
static RULES: &[FixMessageRule<'static>] =
&[FixMessageRule::new(FixMsgType::HEARTBEAT, &[], DISALLOWED)];
let dictionary = FixDictionary::new(FixVersion::Fix44, RULES);
let mut raw = Vec::new();
encode_message(
&mut raw,
b"FIX.4.4",
b"0",
&[(FixTag::TEXT, b"no text here".as_slice())],
)
.expect("encode");
let mut scratch = [FixFieldView::empty(); 16];
let message = parse_message(&raw, &mut scratch).expect("parse");
let err = dictionary
.validate(&message)
.expect_err("disallowed text should fail");
assert_eq!(
err,
FixProfileError::DisallowedTag {
msg_type: FixMsgType::HEARTBEAT,
tag: FixTag::TEXT,
}
);
}
#[test]
fn dictionary_rejects_version_mismatch() {
static RULES: &[FixMessageRule<'static>] =
&[FixMessageRule::new(FixMsgType::HEARTBEAT, &[], &[])];
let dictionary = FixDictionary::new(FixVersion::Fix42, RULES);
let mut raw = Vec::new();
encode_message(&mut raw, b"FIX.4.4", b"0", &[]).expect("encode");
let mut scratch = [FixFieldView::empty(); 16];
let message = parse_message(&raw, &mut scratch).expect("parse");
assert_eq!(
dictionary.validate(&message),
Err(FixProfileError::VersionMismatch {
expected: FixVersion::Fix42,
actual: FixVersion::Fix44,
})
);
}
#[test]
fn sequence_tracker_accepts_expected_inbound() {
let mut tracker = FixSequenceTracker::new();
assert_eq!(
tracker.observe_inbound(1, false),
Ok(FixSequenceAction::Accept { seq_no: 1 })
);
assert_eq!(tracker.next_inbound(), 2);
}
#[test]
fn sequence_tracker_detects_gap_without_advancing() {
let mut tracker = FixSequenceTracker::new();
assert_eq!(
tracker.observe_inbound(3, false),
Ok(FixSequenceAction::Gap {
expected: 1,
received: 3,
resend: FixResendRange {
begin_seq_no: 1,
end_seq_no: 2,
},
})
);
assert_eq!(tracker.next_inbound(), 1);
}
#[test]
fn sequence_tracker_marks_poss_dup_low_sequence_duplicate() {
let mut tracker = FixSequenceTracker::from_next(5, 9);
assert_eq!(
tracker.observe_inbound(3, true),
Ok(FixSequenceAction::Duplicate {
seq_no: 3,
expected: 5,
})
);
assert_eq!(tracker.next_inbound(), 5);
}
#[test]
fn sequence_tracker_marks_unflagged_low_sequence_too_low() {
let mut tracker = FixSequenceTracker::from_next(5, 9);
assert_eq!(
tracker.observe_inbound(3, false),
Ok(FixSequenceAction::TooLow {
expected: 5,
received: 3,
})
);
}
#[test]
fn sequence_tracker_observes_parsed_message_sequence() {
let mut raw = Vec::new();
encode_message(
&mut raw,
b"FIX.4.4",
b"0",
&[(FixTag::MSG_SEQ_NUM, b"1".as_slice())],
)
.expect("encode");
let mut scratch = [FixFieldView::empty(); 16];
let message = parse_message(&raw, &mut scratch).expect("parse");
let mut tracker = FixSequenceTracker::new();
assert_eq!(
tracker.observe_message(&message),
Ok(FixSequenceAction::Accept { seq_no: 1 })
);
}
#[test]
fn sequence_tracker_assigns_outbound_monotonically() {
let mut tracker = FixSequenceTracker::from_next(1, 10);
assert_eq!(tracker.assign_outbound(), 10);
assert_eq!(tracker.assign_outbound(), 11);
assert_eq!(tracker.next_outbound(), 12);
}
#[test]
fn sequence_reset_advances_but_does_not_decrease() {
let mut tracker = FixSequenceTracker::from_next(10, 1);
tracker.apply_sequence_reset(15).expect("advance");
assert_eq!(tracker.next_inbound(), 15);
assert_eq!(
tracker.apply_sequence_reset(14),
Err(FixSequenceError::SequenceResetWouldDecrease {
current: 15,
requested: 14,
})
);
}
#[test]
fn session_id_rejects_soh() {
let err = FixSessionId::new(FixVersion::Fix44, b"CLIENT\x01", b"BROKER").expect_err("soh");
assert_eq!(
err,
FixEncodeError::ValueContainsSoh(FixTag::SENDER_COMP_ID)
);
}
#[test]
fn sequence_snapshot_round_trips_tracker_state() {
let session_id =
FixSessionId::with_qualifier(FixVersion::Fix44, b"CLIENT", b"BROKER", b"A")
.expect("session");
let tracker = FixSequenceTracker::from_next(12, 34);
let snapshot = tracker.snapshot(session_id, b"20260717").expect("snapshot");
assert_eq!(snapshot.session_id(), session_id);
assert_eq!(snapshot.trading_day(), b"20260717");
assert_eq!(snapshot.next_inbound(), 12);
assert_eq!(snapshot.next_outbound(), 34);
let restored = FixSequenceTracker::from_snapshot(&snapshot);
assert_eq!(restored.next_inbound(), 12);
assert_eq!(restored.next_outbound(), 34);
}
#[test]
fn sequence_snapshot_clamps_zero_counters() {
let session_id =
FixSessionId::new(FixVersion::Fix44, b"CLIENT", b"BROKER").expect("session");
let snapshot = FixSequenceSnapshot::new(session_id, 0, 0, b"20260717").expect("snapshot");
assert_eq!(snapshot.next_inbound(), 1);
assert_eq!(snapshot.next_outbound(), 1);
}
#[test]
fn file_sequence_snapshot_store_saves_and_loads_latest() {
let root = std::env::temp_dir().join(format!(
"orderflow-fix-sequence-store-{}",
std::process::id()
));
let _ = fs::remove_dir_all(&root);
let session_id =
FixSessionId::with_qualifier(FixVersion::Fix44, b"CLIENT", b"BROKER", b"PRIMARY")
.expect("session");
let snapshot = FixSequenceSnapshot::new(session_id, 42, 77, b"20260726").expect("snapshot");
let mut store = FileFixSequenceSnapshotStore::open(
FixSequenceStoreConfig::new(&root).with_sync_on_save(false),
)
.expect("store");
let manifest = store.save_snapshot(&snapshot).expect("save");
assert_eq!(manifest.next_inbound, 42);
assert_eq!(manifest.next_outbound, 77);
assert!(manifest.bytes > 0);
let loaded = store.load_latest().expect("load").expect("snapshot");
assert!(loaded.validate_checksum());
assert_eq!(loaded.session_id().version(), FixVersion::Fix44);
assert_eq!(loaded.session_id().sender_comp_id(), b"CLIENT");
assert_eq!(loaded.session_id().target_comp_id(), b"BROKER");
assert_eq!(loaded.session_id().qualifier(), b"PRIMARY");
assert_eq!(loaded.trading_day(), b"20260726");
let borrowed = loaded.as_borrowed().expect("borrowed");
let restored = FixSequenceTracker::from_snapshot(&borrowed);
assert_eq!(restored.next_inbound(), 42);
assert_eq!(restored.next_outbound(), 77);
let _ = fs::remove_dir_all(&root);
}
#[test]
fn file_sequence_snapshot_store_returns_none_when_empty() {
let root = std::env::temp_dir().join(format!(
"orderflow-fix-sequence-store-empty-{}",
std::process::id()
));
let _ = fs::remove_dir_all(&root);
let store = FileFixSequenceSnapshotStore::open(
FixSequenceStoreConfig::new(&root).with_sync_on_save(false),
)
.expect("store");
assert!(store.load_latest().expect("load").is_none());
let _ = fs::remove_dir_all(&root);
}
#[test]
fn file_sequence_snapshot_store_rejects_corrupt_checksum() {
let root = std::env::temp_dir().join(format!(
"orderflow-fix-sequence-store-corrupt-{}",
std::process::id()
));
let _ = fs::remove_dir_all(&root);
let session_id =
FixSessionId::new(FixVersion::Fix42, b"CLIENT", b"BROKER").expect("session");
let snapshot = FixSequenceSnapshot::new(session_id, 12, 21, b"20260726").expect("snapshot");
let mut store = FileFixSequenceSnapshotStore::open(
FixSequenceStoreConfig::new(&root).with_sync_on_save(false),
)
.expect("store");
store.save_snapshot(&snapshot).expect("save");
let path = store.snapshot_path();
let mut bytes = fs::read(&path).expect("read");
let last = bytes.last_mut().expect("byte");
*last ^= 0x01;
fs::write(&path, bytes).expect("write");
let err = store.load_latest().expect_err("checksum");
assert!(matches!(
err,
FixSequenceStoreError::ChecksumMismatch { .. }
));
let _ = fs::remove_dir_all(&root);
}
#[test]
fn sequence_tracker_resets_to_one() {
let mut tracker = FixSequenceTracker::from_next(99, 100);
tracker.reset_to_one();
assert_eq!(tracker.next_inbound(), 1);
assert_eq!(tracker.next_outbound(), 1);
}
#[test]
fn resend_store_plans_replay_and_gap_fills() {
let mut store = FixResendStore::new(FixResendStoreConfig::new(8, 1024));
store
.record_sent(1, FixSentMessageKind::Application, b"app-1")
.expect("seq 1");
store
.record_sent(2, FixSentMessageKind::Administrative, b"admin-2")
.expect("seq 2");
store
.record_sent(3, FixSentMessageKind::Reject, b"reject-3")
.expect("seq 3");
store
.record_sent(5, FixSentMessageKind::Application, b"app-5")
.expect("seq 5");
let mut actions = Vec::new();
let summary = store.plan_resend_range(
FixResendRange {
begin_seq_no: 1,
end_seq_no: 5,
},
&mut actions,
);
assert_eq!(summary.replay_messages(), 3);
assert_eq!(summary.gap_fill_messages(), 2);
assert_eq!(summary.gap_fill_sequences(), 2);
assert_eq!(
actions,
vec![
FixResendAction::Replay {
seq_no: 1,
raw: b"app-1"
},
FixResendAction::GapFill {
begin_seq_no: 2,
end_seq_no: 2
},
FixResendAction::Replay {
seq_no: 3,
raw: b"reject-3"
},
FixResendAction::GapFill {
begin_seq_no: 4,
end_seq_no: 4
},
FixResendAction::Replay {
seq_no: 5,
raw: b"app-5"
},
]
);
}
#[test]
fn resend_store_uses_newest_sequence_for_open_ended_range() {
let mut store = FixResendStore::new(FixResendStoreConfig::new(8, 1024));
store
.record_sent(1, FixSentMessageKind::Application, b"app-1")
.expect("seq 1");
store
.record_sent(2, FixSentMessageKind::Administrative, b"admin-2")
.expect("seq 2");
let mut actions = vec![FixResendAction::GapFill {
begin_seq_no: 99,
end_seq_no: 99,
}];
let summary = store.plan_resend_range(
FixResendRange {
begin_seq_no: 1,
end_seq_no: 0,
},
&mut actions,
);
assert_eq!(summary.replay_messages(), 1);
assert_eq!(summary.gap_fill_sequences(), 1);
assert_eq!(
actions,
vec![
FixResendAction::Replay {
seq_no: 1,
raw: b"app-1"
},
FixResendAction::GapFill {
begin_seq_no: 2,
end_seq_no: 2
},
]
);
}
#[test]
fn resend_store_eviction_turns_old_sequences_into_gap_fill() {
let mut store = FixResendStore::new(FixResendStoreConfig::new(2, 1024));
store
.record_sent(1, FixSentMessageKind::Application, b"app-1")
.expect("seq 1");
store
.record_sent(2, FixSentMessageKind::Application, b"app-2")
.expect("seq 2");
let retention = store
.record_sent(3, FixSentMessageKind::Application, b"app-3")
.expect("seq 3");
assert!(retention.retained());
assert_eq!(retention.evicted_messages(), 1);
let metrics = store.metrics();
assert_eq!(metrics.retained_messages(), 2);
assert_eq!(metrics.oldest_seq_no(), Some(2));
assert_eq!(metrics.newest_seq_no(), Some(3));
assert_eq!(metrics.evicted_messages(), 1);
let mut actions = Vec::new();
store.plan_resend_range(
FixResendRange {
begin_seq_no: 1,
end_seq_no: 3,
},
&mut actions,
);
assert_eq!(
actions,
vec![
FixResendAction::GapFill {
begin_seq_no: 1,
end_seq_no: 1
},
FixResendAction::Replay {
seq_no: 2,
raw: b"app-2"
},
FixResendAction::Replay {
seq_no: 3,
raw: b"app-3"
},
]
);
}
#[test]
fn resend_store_reports_disabled_or_oversized_drops() {
let mut disabled = FixResendStore::new(FixResendStoreConfig::new(0, 1024));
let retention = disabled
.record_sent(1, FixSentMessageKind::Application, b"app-1")
.expect("disabled retention");
assert!(!retention.retained());
assert_eq!(disabled.metrics().dropped_messages(), 1);
let mut bounded = FixResendStore::new(FixResendStoreConfig::new(4, 4));
let retention = bounded
.record_sent(1, FixSentMessageKind::Application, b"app-1")
.expect("oversized retention");
assert!(!retention.retained());
assert_eq!(bounded.metrics().dropped_bytes(), 5);
}
#[test]
fn resend_store_rejects_non_increasing_sequences() {
let mut store = FixResendStore::default();
store
.record_sent(10, FixSentMessageKind::Application, b"app-10")
.expect("seq 10");
let err = store
.record_sent(10, FixSentMessageKind::Application, b"app-10-again")
.expect_err("same sequence");
assert_eq!(
err,
FixResendStoreError::SequenceRegression {
latest: 10,
received: 10
}
);
}
#[test]
fn durable_resend_store_reopens_and_rebuilds_planner() {
let path = std::env::temp_dir().join(format!(
"orderflow-fix-durable-resend-{}.log",
std::process::id()
));
let _ = fs::remove_file(&path);
{
let mut durable = FileFixDurableResendStore::open(
FixDurableResendStoreConfig::new(&path).with_sync_on_record(false),
)
.expect("durable");
let first = durable
.record_sent(1, FixSentMessageKind::Application, b"app-1")
.expect("seq 1");
assert_eq!(first.offset, 0);
assert!(first.bytes > 0);
durable
.record_sent(2, FixSentMessageKind::Administrative, b"admin-2")
.expect("seq 2");
durable
.record_sent(3, FixSentMessageKind::Reject, b"reject-3")
.expect("seq 3");
}
let durable = FileFixDurableResendStore::open(
FixDurableResendStoreConfig::new(&path).with_sync_on_record(false),
)
.expect("reopen");
let mut store = FixResendStore::new(FixResendStoreConfig::new(8, 1024));
let report = durable.load_into(&mut store).expect("load");
assert_eq!(report.records, 3);
assert_eq!(report.first_seq_no, Some(1));
assert_eq!(report.last_seq_no, Some(3));
assert_eq!(report.retained_messages, 3);
let mut actions = Vec::new();
let summary = store.plan_resend_range(
FixResendRange {
begin_seq_no: 1,
end_seq_no: 3,
},
&mut actions,
);
assert_eq!(summary.replay_messages(), 2);
assert_eq!(summary.gap_fill_sequences(), 1);
assert_eq!(
actions,
vec![
FixResendAction::Replay {
seq_no: 1,
raw: b"app-1"
},
FixResendAction::GapFill {
begin_seq_no: 2,
end_seq_no: 2
},
FixResendAction::Replay {
seq_no: 3,
raw: b"reject-3"
},
]
);
let _ = fs::remove_file(&path);
}
#[test]
fn durable_resend_store_rejects_corrupt_existing_log() {
let path = std::env::temp_dir().join(format!(
"orderflow-fix-durable-resend-corrupt-{}.log",
std::process::id()
));
let _ = fs::remove_file(&path);
let mut durable = FileFixDurableResendStore::open(
FixDurableResendStoreConfig::new(&path).with_sync_on_record(false),
)
.expect("durable");
durable
.record_sent(1, FixSentMessageKind::Application, b"app-1")
.expect("seq 1");
drop(durable);
let mut bytes = fs::read(&path).expect("read");
let last = bytes.last_mut().expect("byte");
*last ^= 0x01;
fs::write(&path, bytes).expect("write");
let err = FileFixDurableResendStore::open(
FixDurableResendStoreConfig::new(&path).with_sync_on_record(false),
)
.expect_err("corrupt");
assert!(matches!(
err,
FixDurableResendStoreError::RawHashMismatch { .. }
| FixDurableResendStoreError::FrameChecksumMismatch { .. }
));
let _ = fs::remove_file(&path);
}
#[test]
fn durable_resend_store_rejects_sequence_regression() {
let path = std::env::temp_dir().join(format!(
"orderflow-fix-durable-resend-regression-{}.log",
std::process::id()
));
let _ = fs::remove_file(&path);
let mut durable = FileFixDurableResendStore::open(
FixDurableResendStoreConfig::new(&path).with_sync_on_record(false),
)
.expect("durable");
durable
.record_sent(7, FixSentMessageKind::Application, b"app-7")
.expect("seq 7");
let err = durable
.record_sent(7, FixSentMessageKind::Application, b"app-7-again")
.expect_err("same seq");
assert_eq!(
err,
FixDurableResendStoreError::SequenceRegression {
latest: 7,
received: 7
}
);
let _ = fs::remove_file(&path);
}
#[test]
fn encodes_poss_dup_replay_with_orig_sending_time() {
let header = FixSessionHeader::new(b"CLIENT", b"BROKER", 7, b"20260717-12:00:05.000");
let order = FixNewOrderSingle::new(
b"ORD-1",
b"BTCUSDT",
FixOrderSide::Buy,
b"20260717-12:00:05.000",
b"1.25",
FixOrdType::Limit,
)
.with_price(b"65000.5");
let mut original = Vec::new();
encode_new_order_single(&mut original, FixVersion::Fix44, header, order)
.expect("original order");
let mut scratch = [FixFieldView::empty(); 32];
let original_view = parse_message(&original, &mut scratch).expect("parse original");
let mut replay = Vec::new();
encode_poss_dup_replay(&mut replay, &original_view, b"20260717-12:00:06.000")
.expect("replay");
let mut replay_scratch = [FixFieldView::empty(); 40];
let replay_view = parse_message(&replay, &mut replay_scratch).expect("parse replay");
assert_eq!(replay_view.msg_seq_num(), Some(7));
assert_eq!(
replay_view.get(FixTag::POSS_DUP_FLAG),
Some(b"Y".as_slice())
);
assert_eq!(
replay_view.get(FixTag::SENDING_TIME),
Some(b"20260717-12:00:06.000".as_slice())
);
assert_eq!(
replay_view.get(FixTag::ORIG_SENDING_TIME),
Some(b"20260717-12:00:05.000".as_slice())
);
assert_eq!(
replay_view.get(FixTag::CL_ORD_ID),
Some(b"ORD-1".as_slice())
);
}
#[test]
fn poss_dup_replay_preserves_existing_orig_sending_time() {
let header = FixSessionHeader::new(b"CLIENT", b"BROKER", 7, b"20260717-12:00:05.000");
let mut original = Vec::new();
encode_heartbeat(&mut original, FixVersion::Fix44, header, None).expect("heartbeat");
let mut scratch = [FixFieldView::empty(); 16];
let original_view = parse_message(&original, &mut scratch).expect("parse original");
let mut first_replay = Vec::new();
encode_poss_dup_replay(&mut first_replay, &original_view, b"20260717-12:00:06.000")
.expect("first replay");
let mut first_scratch = [FixFieldView::empty(); 20];
let first_view = parse_message(&first_replay, &mut first_scratch).expect("parse first");
let mut second_replay = Vec::new();
encode_poss_dup_replay(&mut second_replay, &first_view, b"20260717-12:00:07.000")
.expect("second replay");
let mut second_scratch = [FixFieldView::empty(); 20];
let second_view = parse_message(&second_replay, &mut second_scratch).expect("parse second");
assert_eq!(
second_view.get(FixTag::SENDING_TIME),
Some(b"20260717-12:00:07.000".as_slice())
);
assert_eq!(
second_view.get(FixTag::ORIG_SENDING_TIME),
Some(b"20260717-12:00:05.000".as_slice())
);
}
#[test]
fn poss_dup_replay_requires_source_sending_time() {
let mut raw = Vec::new();
encode_message(
&mut raw,
b"FIX.4.4",
b"0",
&[
(FixTag::SENDER_COMP_ID, b"CLIENT".as_slice()),
(FixTag::TARGET_COMP_ID, b"BROKER".as_slice()),
(FixTag::MSG_SEQ_NUM, b"1".as_slice()),
],
)
.expect("source without sending time");
let mut scratch = [FixFieldView::empty(); 16];
let view = parse_message(&raw, &mut scratch).expect("parse source");
let err = encode_poss_dup_replay(&mut Vec::new(), &view, b"20260717-12:00:06.000")
.expect_err("missing sending time");
assert_eq!(
err,
FixEncodeError::MissingRequiredTag(FixTag::SENDING_TIME)
);
}
#[test]
fn parses_session_reject_view() {
let mut raw = Vec::new();
encode_message(
&mut raw,
b"FIX.4.4",
b"3",
&[
(FixTag::REF_SEQ_NUM, b"12".as_slice()),
(FixTag::REF_TAG_ID, b"55".as_slice()),
(FixTag::REF_MSG_TYPE, b"D".as_slice()),
(FixTag::SESSION_REJECT_REASON, b"1".as_slice()),
(FixTag::TEXT, b"missing symbol".as_slice()),
],
)
.expect("encode");
let mut scratch = [FixFieldView::empty(); 16];
let message = parse_message(&raw, &mut scratch).expect("parse");
let reject = parse_session_reject(&message).expect("reject");
assert_eq!(reject.ref_seq_num(), 12);
assert_eq!(reject.ref_tag_id(), Some(FixTag::SYMBOL));
assert_eq!(reject.ref_msg_type(), Some(b"D".as_slice()));
assert_eq!(reject.session_reject_reason(), Some(1));
assert_eq!(reject.text(), Some(b"missing symbol".as_slice()));
}
#[test]
fn session_reject_requires_ref_seq_num() {
let mut raw = Vec::new();
encode_message(&mut raw, b"FIX.4.4", b"3", &[]).expect("encode");
let mut scratch = [FixFieldView::empty(); 8];
let message = parse_message(&raw, &mut scratch).expect("parse");
assert_eq!(
parse_session_reject(&message),
Err(FixRejectParseError::MissingTag(FixTag::REF_SEQ_NUM))
);
}
#[test]
fn parses_business_message_reject_view() {
let mut raw = Vec::new();
encode_message(
&mut raw,
b"FIX.4.4",
b"j",
&[
(FixTag::REF_SEQ_NUM, b"21".as_slice()),
(FixTag::REF_MSG_TYPE, b"D".as_slice()),
(FixTag::BUSINESS_REJECT_REF_ID, b"ORD-1".as_slice()),
(FixTag::BUSINESS_REJECT_REASON, b"3".as_slice()),
(FixTag::TEXT, b"unsupported order".as_slice()),
],
)
.expect("encode");
let mut scratch = [FixFieldView::empty(); 16];
let message = parse_message(&raw, &mut scratch).expect("parse");
let reject = parse_business_message_reject(&message).expect("reject");
assert_eq!(reject.ref_seq_num(), Some(21));
assert_eq!(reject.ref_msg_type(), b"D".as_slice());
assert_eq!(reject.business_reject_ref_id(), Some(b"ORD-1".as_slice()));
assert_eq!(reject.business_reject_reason(), 3);
assert_eq!(reject.text(), Some(b"unsupported order".as_slice()));
}
#[test]
fn business_message_reject_validates_required_numeric_reason() {
let mut raw = Vec::new();
encode_message(
&mut raw,
b"FIX.4.4",
b"j",
&[
(FixTag::REF_MSG_TYPE, b"D".as_slice()),
(FixTag::BUSINESS_REJECT_REASON, b"bad".as_slice()),
],
)
.expect("encode");
let mut scratch = [FixFieldView::empty(); 16];
let message = parse_message(&raw, &mut scratch).expect("parse");
assert_eq!(
parse_business_message_reject(&message),
Err(FixRejectParseError::InvalidNumber(
FixTag::BUSINESS_REJECT_REASON
))
);
}
#[test]
fn encodes_logon_with_required_admin_fields() {
let header = FixSessionHeader::new(b"CLIENT", b"BROKER", 1, b"20260717-12:00:00.000");
let mut raw = Vec::new();
encode_logon(&mut raw, FixVersion::Fix44, header, 30, true).expect("logon");
let mut scratch = [FixFieldView::empty(); 32];
let message = parse_message(&raw, &mut scratch).expect("parse");
assert_eq!(message.typed_msg_type(), Some(FixMsgType::LOGON));
assert_eq!(
message.get(FixTag::SENDER_COMP_ID),
Some(b"CLIENT".as_slice())
);
assert_eq!(
message.get(FixTag::TARGET_COMP_ID),
Some(b"BROKER".as_slice())
);
assert_eq!(message.get(FixTag::ENCRYPT_METHOD), Some(b"0".as_slice()));
assert_eq!(message.get(FixTag::HEART_BT_INT), Some(b"30".as_slice()));
assert_eq!(
message.get(FixTag::RESET_SEQ_NUM_FLAG),
Some(b"Y".as_slice())
);
}
#[test]
fn encodes_heartbeat_with_test_request_id() {
let header = FixSessionHeader::new(b"CLIENT", b"BROKER", 2, b"20260717-12:00:01.000");
let mut raw = Vec::new();
encode_heartbeat(&mut raw, FixVersion::Fix44, header, Some(b"T1")).expect("heartbeat");
let mut scratch = [FixFieldView::empty(); 32];
let message = parse_message(&raw, &mut scratch).expect("parse");
assert_eq!(message.typed_msg_type(), Some(FixMsgType::HEARTBEAT));
assert_eq!(message.get(FixTag::TEST_REQ_ID), Some(b"T1".as_slice()));
}
#[test]
fn transcript_capture_records_parsed_messages() {
let header = FixSessionHeader::new(b"CLIENT", b"BROKER", 2, b"20260717-12:00:01.000");
let mut raw = Vec::new();
encode_heartbeat(&mut raw, FixVersion::Fix44, header, Some(b"T1")).expect("heartbeat");
let mut scratch = [FixFieldView::empty(); 32];
let message = parse_message(&raw, &mut scratch).expect("parse");
let mut capture = FixTranscriptCapture::new(FixTranscriptConfig::new(4, 1024, true));
let retention = capture
.record_message(
FixTranscriptDirection::Outbound,
1_784_275_200_000_000_000,
&message,
)
.expect("record");
assert!(retention.retained());
assert!(retention.raw_retained());
let metrics = capture.metrics();
assert_eq!(metrics.captured_records(), 1);
assert_eq!(metrics.retained_records(), 1);
assert_eq!(metrics.retained_raw_bytes(), raw.len() as u64);
assert_ne!(metrics.rolling_hash(), FNV_OFFSET_BASIS);
let record = capture.records().next().expect("record");
assert_eq!(record.ordinal(), 1);
assert_eq!(record.direction(), FixTranscriptDirection::Outbound);
assert_eq!(record.seq_no(), Some(2));
assert_eq!(record.msg_type(), FixMsgType::HEARTBEAT.as_bytes());
assert_eq!(record.raw_checksum(), checksum(&raw));
assert_eq!(record.raw(), raw.as_slice());
}
#[test]
fn transcript_capture_evicts_to_bounds() {
let mut capture = FixTranscriptCapture::new(FixTranscriptConfig::new(2, 64, true));
let first = capture
.record_frame(
FixTranscriptDirection::Inbound,
1,
Some(1),
b"0",
b"8=FIX.4.4\x0135=0\x01",
)
.expect("record first");
assert!(first.retained());
let second = capture
.record_frame(
FixTranscriptDirection::Outbound,
2,
Some(2),
b"1",
b"8=FIX.4.4\x0135=1\x01",
)
.expect("record second");
assert!(second.retained());
let third = capture
.record_frame(
FixTranscriptDirection::Inbound,
3,
Some(3),
b"2",
b"8=FIX.4.4\x0135=2\x01",
)
.expect("record third");
assert_eq!(third.evicted_records(), 1);
let metrics = capture.metrics();
assert_eq!(metrics.captured_records(), 3);
assert_eq!(metrics.retained_records(), 2);
assert_eq!(metrics.evicted_records(), 1);
assert_eq!(metrics.oldest_ordinal(), Some(2));
assert_eq!(metrics.newest_ordinal(), Some(3));
}
#[test]
fn transcript_capture_can_keep_metadata_without_raw() {
let mut capture = FixTranscriptCapture::new(FixTranscriptConfig::new(4, 8, true));
let retention = capture
.record_frame(
FixTranscriptDirection::Inbound,
1,
Some(1),
b"D",
b"this raw frame is intentionally too large",
)
.expect("record");
assert!(retention.retained());
assert!(!retention.raw_retained());
let metrics = capture.metrics();
assert_eq!(metrics.retained_records(), 1);
assert_eq!(metrics.retained_raw_bytes(), 0);
assert_eq!(
metrics.dropped_raw_bytes(),
"this raw frame is intentionally too large".len() as u64
);
let record = capture.records().next().expect("record");
assert_eq!(
record.raw_len(),
"this raw frame is intentionally too large".len()
);
assert!(!record.raw_retained());
assert!(record.raw().is_empty());
}
#[test]
fn transcript_capture_respects_disabled_record_retention() {
let mut capture = FixTranscriptCapture::new(FixTranscriptConfig::new(0, 1024, true));
let retention = capture
.record_frame(FixTranscriptDirection::Inbound, 1, None, b"0", b"raw")
.expect("record");
assert!(!retention.retained());
assert!(!retention.raw_retained());
let metrics = capture.metrics();
assert_eq!(metrics.captured_records(), 1);
assert_eq!(metrics.retained_records(), 0);
assert_eq!(metrics.dropped_records(), 1);
assert_eq!(metrics.dropped_raw_bytes(), 3);
assert_ne!(metrics.rolling_hash(), FNV_OFFSET_BASIS);
}
#[test]
fn transcript_message_type_is_bounded() {
let mut capture = FixTranscriptCapture::default();
assert_eq!(
capture.record_frame(
FixTranscriptDirection::Inbound,
1,
None,
b"TOO-LONG-MSG-TYPE",
b"raw",
),
Err(FixTranscriptError::MsgTypeTooLong {
capacity: 8,
actual: 17,
})
);
}
#[test]
fn encodes_resend_request_range() {
let header = FixSessionHeader::new(b"CLIENT", b"BROKER", 3, b"20260717-12:00:02.000");
let mut raw = Vec::new();
encode_resend_request(
&mut raw,
FixVersion::Fix44,
header,
FixResendRange {
begin_seq_no: 4,
end_seq_no: 9,
},
)
.expect("resend request");
let mut scratch = [FixFieldView::empty(); 32];
let message = parse_message(&raw, &mut scratch).expect("parse");
assert_eq!(message.typed_msg_type(), Some(FixMsgType::RESEND_REQUEST));
assert_eq!(message.begin_seq_no(), Some(4));
assert_eq!(message.end_seq_no(), Some(9));
}
#[test]
fn encodes_sequence_reset_gap_fill() {
let header = FixSessionHeader::new(b"CLIENT", b"BROKER", 4, b"20260717-12:00:03.000");
let mut raw = Vec::new();
encode_sequence_reset_gap_fill(&mut raw, FixVersion::Fix44, header, 12).expect("gap fill");
let mut scratch = [FixFieldView::empty(); 32];
let message = parse_message(&raw, &mut scratch).expect("parse");
assert_eq!(message.typed_msg_type(), Some(FixMsgType::SEQUENCE_RESET));
assert!(message.gap_fill());
assert_eq!(message.new_seq_no(), Some(12));
}
#[test]
fn logout_builder_rejects_soh_in_text() {
let header = FixSessionHeader::new(b"CLIENT", b"BROKER", 5, b"20260717-12:00:04.000");
let mut raw = Vec::new();
let err = encode_logout(
&mut raw,
FixVersion::Fix44,
header,
Some(b"bad\x01text".as_slice()),
)
.expect_err("soh should fail");
assert_eq!(err, FixEncodeError::ValueContainsSoh(FixTag::TEXT));
}
#[test]
fn encodes_new_order_single() {
let header = FixSessionHeader::new(b"CLIENT", b"BROKER", 6, b"20260717-12:00:05.000");
let request = FixNewOrderSingle::new(
b"ORD-1",
b"BTCUSDT",
FixOrderSide::Buy,
b"20260717-12:00:05.000",
b"1.25",
FixOrdType::Limit,
)
.with_account(b"ACC")
.with_price(b"65000.5")
.with_stop_px(b"64950")
.with_time_in_force(FixTimeInForce::Day);
let mut raw = Vec::new();
encode_new_order_single(&mut raw, FixVersion::Fix44, header, request).expect("new order");
let mut scratch = [FixFieldView::empty(); 32];
let message = parse_message(&raw, &mut scratch).expect("parse");
assert_eq!(message.typed_msg_type(), Some(FixMsgType::NEW_ORDER_SINGLE));
assert_eq!(message.get(FixTag::CL_ORD_ID), Some(b"ORD-1".as_slice()));
assert_eq!(message.get(FixTag::ACCOUNT), Some(b"ACC".as_slice()));
assert_eq!(message.get(FixTag::SYMBOL), Some(b"BTCUSDT".as_slice()));
assert_eq!(message.get(FixTag::SIDE), Some(b"1".as_slice()));
assert_eq!(message.get(FixTag::ORDER_QTY), Some(b"1.25".as_slice()));
assert_eq!(message.get(FixTag::PRICE), Some(b"65000.5".as_slice()));
assert_eq!(message.get(FixTag::STOP_PX), Some(b"64950".as_slice()));
}
#[test]
fn encodes_order_cancel_request() {
let header = FixSessionHeader::new(b"CLIENT", b"BROKER", 7, b"20260717-12:00:06.000");
let request = FixOrderCancelRequest::new(
b"ORD-1",
b"ORD-1-CXL",
b"BTCUSDT",
FixOrderSide::Buy,
b"20260717-12:00:06.000",
)
.with_account(b"ACC");
let mut raw = Vec::new();
encode_order_cancel_request(&mut raw, FixVersion::Fix44, header, request).expect("cancel");
let mut scratch = [FixFieldView::empty(); 32];
let message = parse_message(&raw, &mut scratch).expect("parse");
assert_eq!(
message.typed_msg_type(),
Some(FixMsgType::ORDER_CANCEL_REQUEST)
);
assert_eq!(
message.get(FixTag::ORIG_CL_ORD_ID),
Some(b"ORD-1".as_slice())
);
assert_eq!(
message.get(FixTag::CL_ORD_ID),
Some(b"ORD-1-CXL".as_slice())
);
assert_eq!(message.get(FixTag::ACCOUNT), Some(b"ACC".as_slice()));
}
#[test]
fn encodes_order_cancel_replace_request() {
let header = FixSessionHeader::new(b"CLIENT", b"BROKER", 8, b"20260717-12:00:07.000");
let request = FixOrderCancelReplaceRequest::new(
b"ORD-1",
b"ORD-2",
b"BTCUSDT",
FixOrderSide::Buy,
b"20260717-12:00:07.000",
b"2.00",
FixOrdType::Limit,
)
.with_account(b"ACC")
.with_price(b"65100")
.with_stop_px(b"65000")
.with_time_in_force(FixTimeInForce::ImmediateOrCancel);
let mut raw = Vec::new();
encode_order_cancel_replace_request(&mut raw, FixVersion::Fix44, header, request)
.expect("replace");
let mut scratch = [FixFieldView::empty(); 32];
let message = parse_message(&raw, &mut scratch).expect("parse");
assert_eq!(
message.typed_msg_type(),
Some(FixMsgType::ORDER_CANCEL_REPLACE_REQUEST)
);
assert_eq!(
message.get(FixTag::ORIG_CL_ORD_ID),
Some(b"ORD-1".as_slice())
);
assert_eq!(message.get(FixTag::CL_ORD_ID), Some(b"ORD-2".as_slice()));
assert_eq!(message.get(FixTag::ACCOUNT), Some(b"ACC".as_slice()));
assert_eq!(message.get(FixTag::ORDER_QTY), Some(b"2.00".as_slice()));
assert_eq!(message.get(FixTag::STOP_PX), Some(b"65000".as_slice()));
assert_eq!(message.get(FixTag::TIME_IN_FORCE), Some(b"3".as_slice()));
}
#[test]
fn encodes_order_status_request() {
let header = FixSessionHeader::new(b"CLIENT", b"BROKER", 10, b"20260717-12:00:08.000");
let request = FixOrderStatusRequest::new(b"ORD-1").with_order_id(b"VENUE-1");
let mut raw = Vec::new();
encode_order_status_request(&mut raw, FixVersion::Fix44, header, request)
.expect("status request");
let mut scratch = [FixFieldView::empty(); 16];
let message = parse_message(&raw, &mut scratch).expect("parse");
assert_eq!(
message.msg_type(),
Some(FixMsgType::ORDER_STATUS_REQUEST.as_bytes())
);
assert_eq!(message.get(FixTag::CL_ORD_ID), Some(b"ORD-1".as_slice()));
assert_eq!(message.get(FixTag::ORDER_ID), Some(b"VENUE-1".as_slice()));
}
#[test]
fn order_status_request_allows_minimal_required_shape() {
let header = FixSessionHeader::new(b"CLIENT", b"BROKER", 10, b"20260717-12:00:08.000");
let request = FixOrderStatusRequest::new(b"ORD-1");
let mut raw = Vec::new();
encode_order_status_request(&mut raw, FixVersion::Fix44, header, request)
.expect("status request");
let mut scratch = [FixFieldView::empty(); 16];
let message = parse_message(&raw, &mut scratch).expect("parse");
assert_eq!(message.get(FixTag::CL_ORD_ID), Some(b"ORD-1".as_slice()));
assert_eq!(message.get(FixTag::ORDER_ID), None);
}
#[test]
fn order_status_request_rejects_soh() {
let header = FixSessionHeader::new(b"CLIENT", b"BROKER", 10, b"20260717-12:00:08.000");
let request = FixOrderStatusRequest::new(b"ORD\x01");
let mut raw = Vec::new();
let err = encode_order_status_request(&mut raw, FixVersion::Fix44, header, request)
.expect_err("soh");
assert_eq!(err, FixEncodeError::ValueContainsSoh(FixTag::CL_ORD_ID));
}
#[test]
fn encodes_order_mass_cancel_request() {
let header = FixSessionHeader::new(b"CLIENT", b"BROKER", 11, b"20260717-12:00:09.000");
let request = FixOrderMassCancelRequest::new(
b"MASS-1",
FixMassCancelRequestType::Security,
b"20260717-12:00:09.000",
)
.with_secondary_cl_ord_id(b"ALT-1")
.with_trading_session_id(b"REG")
.with_trading_session_sub_id(b"AM")
.with_symbol(b"BTCUSDT")
.with_side(FixOrderSide::Buy)
.with_text(b"cancel symbol");
let mut raw = Vec::new();
encode_order_mass_cancel_request(&mut raw, FixVersion::Fix44, header, request)
.expect("mass cancel");
let mut scratch = [FixFieldView::empty(); 24];
let message = parse_message(&raw, &mut scratch).expect("parse");
assert_eq!(
message.msg_type(),
Some(FixMsgType::ORDER_MASS_CANCEL_REQUEST.as_bytes())
);
assert_eq!(message.get(FixTag::CL_ORD_ID), Some(b"MASS-1".as_slice()));
assert_eq!(
message.get(FixTag::MASS_CANCEL_REQUEST_TYPE),
Some(b"1".as_slice())
);
assert_eq!(
message.get(FixTag::TRANSACT_TIME),
Some(b"20260717-12:00:09.000".as_slice())
);
assert_eq!(message.get(FixTag::SYMBOL), Some(b"BTCUSDT".as_slice()));
assert_eq!(message.get(FixTag::SIDE), Some(b"1".as_slice()));
assert_eq!(message.get(FixTag::TEXT), Some(b"cancel symbol".as_slice()));
}
#[test]
fn order_mass_cancel_request_allows_minimal_required_shape() {
let header = FixSessionHeader::new(b"CLIENT", b"BROKER", 11, b"20260717-12:00:09.000");
let request = FixOrderMassCancelRequest::new(
b"MASS-1",
FixMassCancelRequestType::AllOrders,
b"20260717-12:00:09.000",
);
let mut raw = Vec::new();
encode_order_mass_cancel_request(&mut raw, FixVersion::Fix44, header, request)
.expect("mass cancel");
let mut scratch = [FixFieldView::empty(); 16];
let message = parse_message(&raw, &mut scratch).expect("parse");
assert_eq!(
message.get(FixTag::MASS_CANCEL_REQUEST_TYPE),
Some(b"7".as_slice())
);
assert_eq!(message.get(FixTag::SYMBOL), None);
}
#[test]
fn order_mass_cancel_request_rejects_soh() {
let header = FixSessionHeader::new(b"CLIENT", b"BROKER", 11, b"20260717-12:00:09.000");
let request = FixOrderMassCancelRequest::new(
b"MASS-1",
FixMassCancelRequestType::Security,
b"20260717-12:00:09.000",
)
.with_text(b"bad\x01text");
let mut raw = Vec::new();
let err = encode_order_mass_cancel_request(&mut raw, FixVersion::Fix44, header, request)
.expect_err("soh");
assert_eq!(err, FixEncodeError::ValueContainsSoh(FixTag::TEXT));
}
#[test]
fn encodes_order_mass_status_request() {
let header = FixSessionHeader::new(b"CLIENT", b"BROKER", 12, b"20260717-12:00:10.000");
let request = FixOrderMassStatusRequest::new(b"MS-1", FixMassStatusReqType::Security)
.with_account(b"ACC")
.with_acct_id_source(b"1")
.with_trading_session_id(b"REG")
.with_trading_session_sub_id(b"AM")
.with_symbol(b"BTCUSDT")
.with_side(FixOrderSide::Sell);
let mut raw = Vec::new();
encode_order_mass_status_request(&mut raw, FixVersion::Fix44, header, request)
.expect("mass status");
let mut scratch = [FixFieldView::empty(); 24];
let message = parse_message(&raw, &mut scratch).expect("parse");
assert_eq!(
message.msg_type(),
Some(FixMsgType::ORDER_MASS_STATUS_REQUEST.as_bytes())
);
assert_eq!(
message.get(FixTag::MASS_STATUS_REQ_ID),
Some(b"MS-1".as_slice())
);
assert_eq!(
message.get(FixTag::MASS_STATUS_REQ_TYPE),
Some(b"1".as_slice())
);
assert_eq!(message.get(FixTag::ACCOUNT), Some(b"ACC".as_slice()));
assert_eq!(message.get(FixTag::SYMBOL), Some(b"BTCUSDT".as_slice()));
assert_eq!(message.get(FixTag::SIDE), Some(b"2".as_slice()));
}
#[test]
fn order_mass_status_request_allows_minimal_required_shape() {
let header = FixSessionHeader::new(b"CLIENT", b"BROKER", 12, b"20260717-12:00:10.000");
let request = FixOrderMassStatusRequest::new(b"MS-1", FixMassStatusReqType::AllOrders);
let mut raw = Vec::new();
encode_order_mass_status_request(&mut raw, FixVersion::Fix44, header, request)
.expect("mass status");
let mut scratch = [FixFieldView::empty(); 16];
let message = parse_message(&raw, &mut scratch).expect("parse");
assert_eq!(
message.get(FixTag::MASS_STATUS_REQ_TYPE),
Some(b"7".as_slice())
);
assert_eq!(message.get(FixTag::ACCOUNT), None);
}
#[test]
fn order_mass_status_request_rejects_soh() {
let header = FixSessionHeader::new(b"CLIENT", b"BROKER", 12, b"20260717-12:00:10.000");
let request = FixOrderMassStatusRequest::new(b"MS\x01", FixMassStatusReqType::AllOrders);
let mut raw = Vec::new();
let err = encode_order_mass_status_request(&mut raw, FixVersion::Fix44, header, request)
.expect_err("soh");
assert_eq!(
err,
FixEncodeError::ValueContainsSoh(FixTag::MASS_STATUS_REQ_ID)
);
}
#[test]
fn order_builder_rejects_soh_in_symbol() {
let header = FixSessionHeader::new(b"CLIENT", b"BROKER", 9, b"20260717-12:00:08.000");
let request = FixNewOrderSingle::new(
b"ORD-1",
b"BTC\x01USDT",
FixOrderSide::Buy,
b"20260717-12:00:08.000",
b"1",
FixOrdType::Market,
);
let mut raw = Vec::new();
let err = encode_new_order_single(&mut raw, FixVersion::Fix44, header, request)
.expect_err("soh should fail");
assert_eq!(err, FixEncodeError::ValueContainsSoh(FixTag::SYMBOL));
}
}