Skip to main content

quickfix/
message_store_factory.rs

1use quickfix_ffi::{
2    FixFileMessageStoreFactory_new, FixMemoryMessageStoreFactory_new,
3    FixMessageStoreFactory_delete, FixMessageStoreFactory_t, FixNullMessageStoreFactory_new,
4};
5
6use crate::{QuickFixError, SessionSettings};
7
8#[cfg(feature = "build-with-mysql")]
9pub mod mysql;
10
11#[cfg(feature = "build-with-postgres")]
12pub mod postgres;
13
14///  Object can be converted as a foreign object representing a `MessageStore`.
15pub trait FfiMessageStoreFactory {
16    /// Get a representation of the message store as a FFI pointer.
17    fn as_ffi_ptr(&self) -> FixMessageStoreFactory_t;
18}
19
20/// File based implementation of `MessageStore`.
21#[derive(Debug)]
22pub struct FileMessageStoreFactory(FixMessageStoreFactory_t);
23
24impl FileMessageStoreFactory {
25    /// Try to create new struct from settings.
26    pub fn try_new(settings: &SessionSettings) -> Result<Self, QuickFixError> {
27        unsafe { FixFileMessageStoreFactory_new(settings.0) }
28            .map(Self)
29            .ok_or_else(QuickFixError::from_last_error)
30    }
31}
32
33impl FfiMessageStoreFactory for FileMessageStoreFactory {
34    fn as_ffi_ptr(&self) -> FixMessageStoreFactory_t {
35        self.0
36    }
37}
38
39impl Drop for FileMessageStoreFactory {
40    fn drop(&mut self) {
41        unsafe { FixMessageStoreFactory_delete(self.0) }
42    }
43}
44
45/// In memory implementation of `MessageStore`.
46#[derive(Debug)]
47pub struct MemoryMessageStoreFactory(FixMessageStoreFactory_t);
48
49impl MemoryMessageStoreFactory {
50    /// Create new struct.
51    pub fn new() -> Self {
52        Self::default()
53    }
54}
55
56impl FfiMessageStoreFactory for MemoryMessageStoreFactory {
57    fn as_ffi_ptr(&self) -> FixMessageStoreFactory_t {
58        self.0
59    }
60}
61
62impl Default for MemoryMessageStoreFactory {
63    fn default() -> Self {
64        unsafe { FixMemoryMessageStoreFactory_new() }
65            .map(Self)
66            .expect("Fail to allocate MemoryMessageStore")
67    }
68}
69
70impl Drop for MemoryMessageStoreFactory {
71    fn drop(&mut self) {
72        unsafe { FixMessageStoreFactory_delete(self.0) }
73    }
74}
75
76/// Null implementation of MessageStore.
77///
78/// Will not actually store messages. Useful for admin-only or market data-only applications.
79#[derive(Debug)]
80pub struct NullMessageStoreFactory(FixMessageStoreFactory_t);
81
82impl NullMessageStoreFactory {
83    /// Create new struct.
84    pub fn new() -> Self {
85        Self::default()
86    }
87}
88
89impl FfiMessageStoreFactory for NullMessageStoreFactory {
90    fn as_ffi_ptr(&self) -> FixMessageStoreFactory_t {
91        self.0
92    }
93}
94
95impl Default for NullMessageStoreFactory {
96    fn default() -> Self {
97        unsafe { FixNullMessageStoreFactory_new() }
98            .map(Self)
99            .expect("Fail to allocate NullMessageStore")
100    }
101}
102
103impl Drop for NullMessageStoreFactory {
104    fn drop(&mut self) {
105        unsafe { FixMessageStoreFactory_delete(self.0) }
106    }
107}