1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use std::fmt::Display;

//TODO partial match on sender + target comp?
#[derive(Debug, Clone, Default, Hash, PartialEq, Eq, Ord, PartialOrd)]
pub struct SessionId {
    pub(crate) id: String,
    pub(crate) begin_string: String,
    pub(crate) sender_comp_id: String,
    pub(crate) sender_sub_id: String,
    pub(crate) sender_location_id: String,
    pub(crate) target_comp_id: String,
    pub(crate) target_sub_id: String,
    pub(crate) target_location_id: String,
    // pub(crate) session_qualifier: String,
    pub(crate) is_fixt: bool,
}

impl SessionId {
    pub fn new<A, B, C, D, E, F, G>(
        begin_string: A,
        sender_comp_id: B,
        sender_sub_id: C,
        sender_location_id: D,
        target_comp_id: E,
        target_sub_id: F,
        target_location_id: G,
        // session_qualifier: String,
    ) -> Self
    where
        A: Into<String>,
        B: Into<String>,
        C: Into<String>,
        D: Into<String>,
        E: Into<String>,
        F: Into<String>,
        G: Into<String>,
    {
        let begin_string = begin_string.into();
        let sender_comp_id = sender_comp_id.into();
        let sender_sub_id = sender_sub_id.into();
        let sender_location_id = sender_location_id.into();
        let target_comp_id = target_comp_id.into();
        let target_sub_id = target_sub_id.into();
        let target_location_id = target_location_id.into();
        let id = format!(
            "{}:{}{}{}->{}{}{}",
            begin_string,
            sender_comp_id,
            sender_sub_id,
            sender_location_id,
            target_comp_id,
            target_sub_id,
            target_location_id
        );
        let is_fixt = begin_string.starts_with("FIXT");
        SessionId {
            id,
            begin_string,
            sender_comp_id,
            sender_sub_id,
            sender_location_id,
            target_comp_id,
            target_sub_id,
            target_location_id,
            // session_qualifier,
            is_fixt,
        }
    }

    pub fn is_empty(&self) -> bool {
        self.sender_comp_id.len() + self.target_comp_id.len() == 0
    }

    pub fn id(&self) -> &str {
        self.id.as_ref()
    }

    pub fn begin_string(&self) -> &str {
        self.begin_string.as_ref()
    }

    pub fn sender_comp_id(&self) -> &str {
        self.sender_comp_id.as_ref()
    }

    pub fn sender_sub_id(&self) -> &str {
        self.sender_sub_id.as_ref()
    }

    pub fn sender_location_id(&self) -> &str {
        self.sender_location_id.as_ref()
    }

    pub fn target_comp_id(&self) -> &str {
        self.target_comp_id.as_ref()
    }

    pub fn target_sub_id(&self) -> &str {
        self.target_sub_id.as_ref()
    }

    pub fn target_location_id(&self) -> &str {
        self.target_location_id.as_ref()
    }

    pub fn is_fixt(&self) -> bool {
        self.is_fixt
    }


    pub fn prefix(&self) -> String {
        format!("{}-{}{}{}{}{}-{}{}{}{}{}",
            self.begin_string(),
            self.sender_comp_id(),
            if self.sender_sub_id().is_empty() { "" } else { "-" },
            self.sender_sub_id(),
            if self.sender_location_id().is_empty() { "" } else { "-" },
            self.sender_location_id(),
            self.target_comp_id(),
            if self.target_sub_id().is_empty() { "" } else { "-" },
            self.target_sub_id(),
            if self.target_location_id().is_empty() { "" } else { "-" },
            self.target_location_id(),
        )
    }
}

impl Display for SessionId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(format_args!("{}", self.id))
    }
}