|
1 | 1 | use hotfix::Message as HotfixMessage; |
2 | 2 | use hotfix::message::FixMessage; |
| 3 | +use hotfix_message::{Part, fix44}; |
3 | 4 |
|
| 5 | +/// Business messages used for testing. |
4 | 6 | #[derive(Debug, Clone)] |
5 | | -pub struct TestMessage; |
| 7 | +pub enum TestMessage { |
| 8 | + /// A minimal implementation of a valid execution report. |
| 9 | + MinimalExecutionReport { |
| 10 | + order_id: String, |
| 11 | + exec_id: String, |
| 12 | + exec_type: fix44::ExecType, |
| 13 | + ord_status: fix44::OrdStatus, |
| 14 | + side: fix44::Side, |
| 15 | + symbol: String, |
| 16 | + order_qty: f64, |
| 17 | + price: f64, |
| 18 | + }, |
| 19 | +} |
| 20 | + |
| 21 | +impl TestMessage { |
| 22 | + pub fn dummy_execution_report() -> Self { |
| 23 | + Self::MinimalExecutionReport { |
| 24 | + order_id: "123456789".to_string(), |
| 25 | + exec_id: "123456789".to_string(), |
| 26 | + exec_type: fix44::ExecType::New, |
| 27 | + ord_status: fix44::OrdStatus::New, |
| 28 | + side: fix44::Side::Buy, |
| 29 | + symbol: "ABC".to_string(), |
| 30 | + order_qty: 100.0, |
| 31 | + price: 100.0, |
| 32 | + } |
| 33 | + } |
| 34 | +} |
6 | 35 |
|
7 | 36 | impl FixMessage for TestMessage { |
8 | | - fn write(&self, _msg: &mut HotfixMessage) {} |
| 37 | + fn write(&self, msg: &mut HotfixMessage) { |
| 38 | + match self { |
| 39 | + TestMessage::MinimalExecutionReport { |
| 40 | + order_id, |
| 41 | + exec_id, |
| 42 | + exec_type, |
| 43 | + ord_status, |
| 44 | + side, |
| 45 | + symbol, |
| 46 | + order_qty, |
| 47 | + price, |
| 48 | + } => { |
| 49 | + msg.set(fix44::ORDER_ID, order_id.as_str()); |
| 50 | + msg.set(fix44::EXEC_ID, exec_id.as_str()); |
| 51 | + msg.set(fix44::EXEC_TYPE, *exec_type); |
| 52 | + msg.set(fix44::ORD_STATUS, *ord_status); |
| 53 | + msg.set(fix44::SIDE, *side); |
| 54 | + msg.set(fix44::SYMBOL, symbol.as_str()); |
| 55 | + msg.set(fix44::ORDER_QTY, *order_qty); |
| 56 | + msg.set(fix44::PRICE, *price); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
9 | 60 |
|
10 | 61 | fn message_type(&self) -> &str { |
11 | | - unimplemented!() |
| 62 | + match self { |
| 63 | + TestMessage::MinimalExecutionReport { .. } => "8", |
| 64 | + } |
12 | 65 | } |
13 | 66 |
|
14 | | - fn parse(_msg: &HotfixMessage) -> Self { |
15 | | - TestMessage |
| 67 | + fn parse(msg: &HotfixMessage) -> Self { |
| 68 | + let msg_type: &str = msg.get(fix44::MSG_TYPE).unwrap(); |
| 69 | + if msg_type != "8" { |
| 70 | + // not an execution report |
| 71 | + panic!("Invalid message type: {}", msg_type); |
| 72 | + } |
| 73 | + |
| 74 | + let order_id: &str = msg.get(fix44::ORDER_ID).unwrap(); |
| 75 | + let exec_id: &str = msg.get(fix44::EXEC_ID).unwrap(); |
| 76 | + let exec_type = msg.get(fix44::EXEC_TYPE).unwrap(); |
| 77 | + let ord_status = msg.get(fix44::ORD_STATUS).unwrap(); |
| 78 | + let side = msg.get(fix44::SIDE).unwrap(); |
| 79 | + let symbol: &str = msg.get(fix44::SYMBOL).unwrap(); |
| 80 | + let order_qty = msg.get(fix44::ORDER_QTY).unwrap(); |
| 81 | + let price = msg.get(fix44::PRICE).unwrap(); |
| 82 | + |
| 83 | + Self::MinimalExecutionReport { |
| 84 | + order_id: order_id.to_string(), |
| 85 | + exec_id: exec_id.to_string(), |
| 86 | + exec_type, |
| 87 | + ord_status, |
| 88 | + side, |
| 89 | + symbol: symbol.to_string(), |
| 90 | + order_qty, |
| 91 | + price, |
| 92 | + } |
16 | 93 | } |
17 | 94 | } |
0 commit comments