Skip to content

Commit

Permalink
🏷️ Add trait FieldMap
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurlm committed Nov 14, 2023
1 parent 598126f commit 1d282fd
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
4 changes: 2 additions & 2 deletions quickfix/examples/fix_repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use std::{

use colored::Colorize;
use quickfix::{
Application, ApplicationCallback, ConnectionHandler, FileLogFactory, FileStoreFactory, Message,
SessionId, SessionSettings, SocketAcceptor, SocketInitiator,
Application, ApplicationCallback, ConnectionHandler, FieldMap, FileLogFactory,
FileStoreFactory, Message, SessionId, SessionSettings, SocketAcceptor, SocketInitiator,
};

fn main() {
Expand Down
6 changes: 6 additions & 0 deletions quickfix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ pub trait ConnectionHandler {
fn is_logged_on(&self) -> Result<bool, QuickFixError>;
fn is_stopped(&self) -> Result<bool, QuickFixError>;
}

pub trait FieldMap {
fn get_field(&self, tag: i32) -> Option<String>;
fn set_field(&mut self, tag: i32, value: &str) -> Result<(), QuickFixError>;
fn remove_field(&mut self, tag: i32) -> Result<(), QuickFixError>;
}
30 changes: 16 additions & 14 deletions quickfix/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use quickfix_ffi::{

use crate::{
utils::{ffi_code_to_result, read_buffer_to_string, read_checked_cstr},
QuickFixError,
FieldMap, QuickFixError,
};

pub struct Message(pub(crate) quickfix_ffi::FixMessage_t);
Expand All @@ -26,19 +26,6 @@ impl Message {
.ok_or(QuickFixError::InvalidFunctionReturn)
}

pub fn set_field(&mut self, tag: i32, value: &str) -> Result<(), QuickFixError> {
let ffi_value = CString::new(value)?;
ffi_code_to_result(unsafe { FixMessage_setField(self.0, tag, ffi_value.as_ptr()) })
}

pub fn get_field(&self, tag: i32) -> Option<String> {
unsafe { FixMessage_getField(self.0, tag) }.map(read_checked_cstr)
}

pub fn remove_field(&mut self, tag: i32) -> Result<(), QuickFixError> {
ffi_code_to_result(unsafe { FixMessage_removeField(self.0, tag) })
}

pub fn as_string(&self) -> Result<String, QuickFixError> {
self.as_string_with_len(4096 /* 1 page */)
}
Expand All @@ -53,6 +40,21 @@ impl Message {
}
}

impl FieldMap for Message {
fn get_field(&self, tag: i32) -> Option<String> {
unsafe { FixMessage_getField(self.0, tag) }.map(read_checked_cstr)
}

fn set_field(&mut self, tag: i32, value: &str) -> Result<(), QuickFixError> {
let ffi_value = CString::new(value)?;
ffi_code_to_result(unsafe { FixMessage_setField(self.0, tag, ffi_value.as_ptr()) })
}

fn remove_field(&mut self, tag: i32) -> Result<(), QuickFixError> {
ffi_code_to_result(unsafe { FixMessage_removeField(self.0, tag) })
}
}

impl fmt::Debug for Message {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Message").field(&self.as_string()).finish()
Expand Down
2 changes: 1 addition & 1 deletion quickfix/tests/test_messages.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use quickfix::Message;
use quickfix::{FieldMap, Message};

#[test]
fn test_read_empy_message() {
Expand Down

0 comments on commit 1d282fd

Please sign in to comment.