Skip to main content

hotfix_store/
lib.rs

1//! Message store traits and implementations for the hotfix FIX engine.
2//!
3//! This crate provides the [`MessageStore`] trait and several implementations:
4//!
5//! - [`InMemoryMessageStore`]: An in-memory store for testing (loses state on restart)
6//! - [`FileStore`]: A file-based store for persistence
7//!
8//! # Features
9//!
10//! - `test-utils`: Enables the [`test_utils`] module with test harness for store implementations
11
12/// Error types for store operations.
13pub mod error;
14
15/// File-based message store for persistence.
16pub mod file;
17
18/// In-memory message store (non-persistent).
19pub mod in_memory;
20
21/// Test utilities for message store implementations.
22#[cfg(feature = "test-utils")]
23pub mod test_utils;
24
25pub use error::{BoxError, Result, StoreError};
26pub use file::FileStore;
27pub use in_memory::InMemoryMessageStore;
28
29use chrono::DateTime;
30
31/// A trait for storing and retrieving FIX messages and sequence numbers.
32///
33/// Message stores are responsible for:
34/// - Persisting outgoing messages for potential resend
35/// - Tracking sender and target sequence numbers
36/// - Storing session creation time
37///
38/// Implementations should be async-safe and handle concurrent access appropriately.
39#[async_trait::async_trait]
40pub trait MessageStore: Send + Sync {
41    /// Adds a message to the store with the given sequence number.
42    async fn add(&mut self, sequence_number: u64, message: &[u8]) -> Result<()>;
43
44    /// Retrieves messages in the given sequence number range (inclusive).
45    async fn get_slice(&self, begin: usize, end: usize) -> Result<Vec<Vec<u8>>>;
46
47    /// Returns the next sender sequence number (current + 1).
48    fn next_sender_seq_number(&self) -> u64;
49
50    /// Returns the next target sequence number (current + 1).
51    fn next_target_seq_number(&self) -> u64;
52
53    /// Increments the sender sequence number by 1.
54    async fn increment_sender_seq_number(&mut self) -> Result<()>;
55
56    /// Increments the target sequence number by 1.
57    async fn increment_target_seq_number(&mut self) -> Result<()>;
58
59    /// Sets the target sequence number to a specific value.
60    async fn set_target_seq_number(&mut self, seq_number: u64) -> Result<()>;
61
62    /// Resets the store, clearing all messages and resetting sequence numbers.
63    async fn reset(&mut self) -> Result<()>;
64
65    /// Returns the creation time of the current session.
66    fn creation_time(&self) -> DateTime<chrono::Utc>;
67}