Skip to main content

dfx_base/
session_id.rs

1use std::fmt::Display;
2
3//TODO partial match on sender + target comp?
4#[derive(Debug, Clone, Default, Hash, PartialEq, Eq, Ord, PartialOrd)]
5pub struct SessionId {
6    pub(crate) id: String,
7    pub(crate) begin_string: String,
8    pub(crate) sender_comp_id: String,
9    pub(crate) sender_sub_id: String,
10    pub(crate) sender_location_id: String,
11    pub(crate) target_comp_id: String,
12    pub(crate) target_sub_id: String,
13    pub(crate) target_location_id: String,
14    // pub(crate) session_qualifier: String,
15    pub(crate) is_fixt: bool,
16}
17
18impl SessionId {
19    pub fn new<A, B, C, D, E, F, G>(
20        begin_string: A,
21        sender_comp_id: B,
22        sender_sub_id: C,
23        sender_location_id: D,
24        target_comp_id: E,
25        target_sub_id: F,
26        target_location_id: G,
27        // session_qualifier: String,
28    ) -> Self
29    where
30        A: Into<String>,
31        B: Into<String>,
32        C: Into<String>,
33        D: Into<String>,
34        E: Into<String>,
35        F: Into<String>,
36        G: Into<String>,
37    {
38        let begin_string = begin_string.into();
39        let sender_comp_id = sender_comp_id.into();
40        let sender_sub_id = sender_sub_id.into();
41        let sender_location_id = sender_location_id.into();
42        let target_comp_id = target_comp_id.into();
43        let target_sub_id = target_sub_id.into();
44        let target_location_id = target_location_id.into();
45        let id = format!(
46            "{}:{}{}{}->{}{}{}",
47            begin_string,
48            sender_comp_id,
49            sender_sub_id,
50            sender_location_id,
51            target_comp_id,
52            target_sub_id,
53            target_location_id
54        );
55        let is_fixt = begin_string.starts_with("FIXT");
56        SessionId {
57            id,
58            begin_string,
59            sender_comp_id,
60            sender_sub_id,
61            sender_location_id,
62            target_comp_id,
63            target_sub_id,
64            target_location_id,
65            // session_qualifier,
66            is_fixt,
67        }
68    }
69
70    pub fn is_empty(&self) -> bool {
71        self.sender_comp_id.len() + self.target_comp_id.len() == 0
72    }
73
74    pub fn id(&self) -> &str {
75        self.id.as_ref()
76    }
77
78    pub fn begin_string(&self) -> &str {
79        self.begin_string.as_ref()
80    }
81
82    pub fn sender_comp_id(&self) -> &str {
83        self.sender_comp_id.as_ref()
84    }
85
86    pub fn sender_sub_id(&self) -> &str {
87        self.sender_sub_id.as_ref()
88    }
89
90    pub fn sender_location_id(&self) -> &str {
91        self.sender_location_id.as_ref()
92    }
93
94    pub fn target_comp_id(&self) -> &str {
95        self.target_comp_id.as_ref()
96    }
97
98    pub fn target_sub_id(&self) -> &str {
99        self.target_sub_id.as_ref()
100    }
101
102    pub fn target_location_id(&self) -> &str {
103        self.target_location_id.as_ref()
104    }
105
106    pub fn is_fixt(&self) -> bool {
107        self.is_fixt
108    }
109
110
111    pub fn prefix(&self) -> String {
112        format!("{}-{}{}{}{}{}-{}{}{}{}{}",
113            self.begin_string(),
114            self.sender_comp_id(),
115            if self.sender_sub_id().is_empty() { "" } else { "-" },
116            self.sender_sub_id(),
117            if self.sender_location_id().is_empty() { "" } else { "-" },
118            self.sender_location_id(),
119            self.target_comp_id(),
120            if self.target_sub_id().is_empty() { "" } else { "-" },
121            self.target_sub_id(),
122            if self.target_location_id().is_empty() { "" } else { "-" },
123            self.target_location_id(),
124        )
125    }
126}
127
128impl Display for SessionId {
129    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
130        f.write_fmt(format_args!("{}", self.id))
131    }
132}