Skip to main content

Crate fixer

Crate fixer 

Source
Expand description

§fixer

A full-featured FIX protocol engine for Rust. Supports FIX versions 4.0 through 5.0SP2 with spec-driven message validation, code-generated type-safe messages, and pluggable storage and logging backends.

§Quick Start

A FIX application has three parts: a configuration, an Application trait implementation, and an Acceptor (server) or Initiator (client).

use fixer::{
    acceptor::Acceptor,
    application::Application,
    errors::MessageRejectErrorResult,
    log::screen_log::ScreenLogFactory,
    message::Message,
    registry::send_to_target,
    session::session_id::SessionID,
    settings::Settings,
    store::MemoryStoreFactory,
};
use simple_error::SimpleResult;
use std::sync::Arc;
use tokio::io::BufReader;

struct MyApp;

impl Application for MyApp {
    fn on_create(&self, session_id: &Arc<SessionID>) {
        println!("Session created: {session_id}");
    }
    fn on_logon(&self, session_id: &Arc<SessionID>) {
        println!("Logon: {session_id}");
    }
    fn on_logout(&self, session_id: &Arc<SessionID>) {
        println!("Logout: {session_id}");
    }
    fn to_admin(&self, _msg: &mut Message, _session_id: &Arc<SessionID>) {}
    fn to_app(
        &self, _msg: &mut Message, _session_id: &Arc<SessionID>,
    ) -> SimpleResult<()> {
        Ok(())
    }
    fn from_admin(
        &self, _msg: &Message, _session_id: &Arc<SessionID>,
    ) -> MessageRejectErrorResult {
        Ok(())
    }
    fn from_app(
        &self, msg: &Message, session_id: &Arc<SessionID>,
    ) -> MessageRejectErrorResult {
        // Echo the message back (see examples/echo_server for a full version)
        println!("Received message from {session_id}");
        Ok(())
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let cfg = "\
[DEFAULT]
SocketAcceptPort=5001
SenderCompID=SERVER
TargetCompID=CLIENT
ResetOnLogon=Y

[SESSION]
BeginString=FIX.4.2
";
    let settings = Settings::parse(BufReader::new(cfg.as_bytes())).await?;
    let app: Arc<dyn Application> = Arc::new(MyApp);
    let store_factory = MemoryStoreFactory::new();
    let log_factory = ScreenLogFactory::new();

    let mut acceptor =
        Acceptor::new(app, store_factory, settings, log_factory).await?;
    acceptor.start().await?;

    tokio::signal::ctrl_c().await?;
    acceptor.stop().await;
    Ok(())
}

§Architecture

ConceptDescription
ApplicationYour code — callbacks for session events and messages
AcceptorListens for inbound FIX connections (server side)
InitiatorConnects to remote FIX servers (client side)
SettingsINI-style configuration with [DEFAULT] and [SESSION] sections
MessageA FIX message with Header, Body, and Trailer FieldMaps
SessionIDIdentifies a session by BeginString + SenderCompID + TargetCompID
MessageStoreFactoryEnumCreates message stores (memory, file, SQL, MongoDB)
LogFactoryEnumCreates loggers (screen, file, SQL, MongoDB, composite)

§Feature Flags

All features are opt-in. By default only the in-memory store and screen/file loggers are available.

FeatureDescription
sql_storeSQL-backed message store (SQLite, PostgreSQL, MySQL) via sqlx
sql_logSQL-backed logging via sqlx
mongo_storeMongoDB-backed message store via mongodb
mongo_logMongoDB-backed logging via mongodb

§Generated Types

The companion crate fixer-fix provides code-generated, type-safe FIX messages. See its documentation for usage with tag, field, enums, and per-version message modules (fix40 through fix50sp2).

§Examples

See the examples/ directory for working applications:

  • echo_server — Acceptor that echoes messages back to the sender
  • trade_client — Initiator that sends orders using generated types

Modules§

acceptor
application
config
connection
datadictionary
errors
field
field_map
fileutil
fix_boolean
fix_bytes
fix_decimal
fix_float
fix_int
fix_string
fix_utc_timestamp
initiator
log
message
message_router
msg_type
parser
registry
repeating_group
session
settings
store
tag
tag_value
tls
validation

Constants§

BEGIN_STRING_FIX40
BEGIN_STRING_FIX41
BEGIN_STRING_FIX42
BEGIN_STRING_FIX43
BEGIN_STRING_FIX44
BEGIN_STRING_FIXT11