easyfix_session/
session_id.rs1use core::fmt;
2
3use easyfix_messages::{
4 fields::{FixStr, FixString},
5 messages::{FixtMessage, Header},
6};
7use serde::Deserialize;
8
9#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq)]
10pub struct SessionId {
11 begin_string: FixString,
12 sender_comp_id: FixString,
13 target_comp_id: FixString,
14 session_qualifier: String,
15}
16
17impl fmt::Display for SessionId {
18 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19 if self.session_qualifier.is_empty() {
20 write!(
21 f,
22 "{}: {} -> {}",
23 self.begin_string, self.sender_comp_id, self.target_comp_id
24 )
25 } else {
26 write!(
27 f,
28 "{}: {} -> {} ({})",
29 self.begin_string, self.sender_comp_id, self.target_comp_id, self.session_qualifier
30 )
31 }
32 }
33}
34
35impl SessionId {
36 pub fn new(
37 begin_string: FixString,
38 sender_comp_id: FixString,
39 target_comp_id: FixString,
40 ) -> SessionId {
41 SessionId {
42 begin_string,
43 sender_comp_id,
44 target_comp_id,
45 session_qualifier: String::new(),
46 }
47 }
48
49 pub fn with_session_qualifier(
50 begin_string: FixString,
51 sender_comp_id: FixString,
52 target_comp_id: FixString,
53 session_qualifier: String,
54 ) -> SessionId {
55 SessionId {
56 begin_string,
57 sender_comp_id,
58 target_comp_id,
59 session_qualifier,
60 }
61 }
62
63 pub fn from_input_msg(msg: &FixtMessage) -> SessionId {
64 SessionId::from_input_header(&msg.header)
65 }
66
67 pub fn from_input_header(header: &Header) -> SessionId {
68 SessionId::new(
69 header.begin_string.clone(),
70 header.target_comp_id.clone(),
71 header.sender_comp_id.clone(),
72 )
73 }
74
75 pub fn from_output_msg(msg: &FixtMessage) -> SessionId {
76 SessionId::from_output_header(&msg.header)
77 }
78
79 pub fn from_output_header(header: &Header) -> SessionId {
80 SessionId::new(
81 header.begin_string.clone(),
82 header.sender_comp_id.clone(),
83 header.target_comp_id.clone(),
84 )
85 }
86
87 pub fn reverse_route(mut self) -> SessionId {
88 std::mem::swap(&mut self.sender_comp_id, &mut self.target_comp_id);
89 self
90 }
91
92 pub fn begin_string(&self) -> &FixStr {
93 &self.begin_string
94 }
95
96 pub fn sender_comp_id(&self) -> &FixStr {
97 &self.sender_comp_id
98 }
99
100 pub fn target_comp_id(&self) -> &FixStr {
101 &self.target_comp_id
102 }
103
104 pub fn session_qualifier(&self) -> &str {
105 &self.session_qualifier
106 }
107
108 pub fn is_fixt(&self) -> bool {
109 self.begin_string.as_utf8().starts_with("FIXT")
110 }
111}