Skip to main content

quickfix_tokio/
session_id.rs

1//! Session identity: BeginString + CompIDs (+ optional sub/location IDs and
2//! qualifier), matching the reference engines' `SessionID`.
3
4use std::fmt;
5
6#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
7pub struct SessionId {
8    pub begin_string: String,
9    pub sender_comp_id: String,
10    pub sender_sub_id: String,
11    pub sender_location_id: String,
12    pub target_comp_id: String,
13    pub target_sub_id: String,
14    pub target_location_id: String,
15    pub qualifier: String,
16}
17
18impl SessionId {
19    pub fn new(begin_string: &str, sender_comp_id: &str, target_comp_id: &str) -> Self {
20        Self {
21            begin_string: begin_string.to_owned(),
22            sender_comp_id: sender_comp_id.to_owned(),
23            target_comp_id: target_comp_id.to_owned(),
24            ..Default::default()
25        }
26    }
27
28    pub fn is_fixt(&self) -> bool {
29        self.begin_string == "FIXT.1.1"
30    }
31
32    /// The identity an acceptor derives from an inbound Logon: the peer's
33    /// SenderCompID is our TargetCompID and vice versa.
34    pub fn reversed(&self) -> Self {
35        Self {
36            begin_string: self.begin_string.clone(),
37            sender_comp_id: self.target_comp_id.clone(),
38            sender_sub_id: self.target_sub_id.clone(),
39            sender_location_id: self.target_location_id.clone(),
40            target_comp_id: self.sender_comp_id.clone(),
41            target_sub_id: self.sender_sub_id.clone(),
42            target_location_id: self.sender_location_id.clone(),
43            qualifier: self.qualifier.clone(),
44        }
45    }
46
47    /// Filename prefix for file-based stores/logs:
48    /// `BeginString-Sender-Target[-Qualifier]`.
49    pub fn file_prefix(&self) -> String {
50        let mut s = format!(
51            "{}-{}-{}",
52            self.begin_string, self.sender_comp_id, self.target_comp_id
53        );
54        if !self.qualifier.is_empty() {
55            s.push('-');
56            s.push_str(&self.qualifier);
57        }
58        s
59    }
60}
61
62impl fmt::Display for SessionId {
63    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64        write!(f, "{}:{}", self.begin_string, self.sender_comp_id)?;
65        if !self.sender_sub_id.is_empty() {
66            write!(f, "/{}", self.sender_sub_id)?;
67        }
68        if !self.sender_location_id.is_empty() {
69            write!(f, "/{}", self.sender_location_id)?;
70        }
71        write!(f, "->{}", self.target_comp_id)?;
72        if !self.target_sub_id.is_empty() {
73            write!(f, "/{}", self.target_sub_id)?;
74        }
75        if !self.target_location_id.is_empty() {
76            write!(f, "/{}", self.target_location_id)?;
77        }
78        if !self.qualifier.is_empty() {
79            write!(f, ":{}", self.qualifier)?;
80        }
81        Ok(())
82    }
83}