Skip to main content

rocketmq_protocol/protocol/body/
connection.rs

1// Copyright 2023 The RocketMQ Rust Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use cheetah_string::CheetahString;
16use serde::Deserialize;
17use serde::Serialize;
18
19use crate::protocol::LanguageCode;
20
21#[derive(Deserialize, Serialize, Debug, Clone, Eq, PartialEq, Hash, Default)]
22#[serde(rename_all = "camelCase")]
23pub struct Connection {
24    client_id: CheetahString,
25    client_addr: CheetahString,
26    language: LanguageCode,
27    version: i32,
28}
29
30impl Connection {
31    pub fn new() -> Self {
32        Connection {
33            client_id: CheetahString::default(),
34            client_addr: CheetahString::default(),
35            language: LanguageCode::default(),
36            version: 0,
37        }
38    }
39}
40
41impl Connection {
42    pub fn get_client_id(&self) -> CheetahString {
43        self.client_id.clone()
44    }
45
46    pub fn set_client_id(&mut self, client_id: CheetahString) {
47        self.client_id = client_id;
48    }
49
50    pub fn get_client_addr(&self) -> CheetahString {
51        self.client_addr.clone()
52    }
53
54    pub fn set_client_addr(&mut self, client_addr: CheetahString) {
55        self.client_addr = client_addr;
56    }
57
58    pub fn get_language(&self) -> LanguageCode {
59        self.language
60    }
61
62    pub fn set_language(&mut self, language: LanguageCode) {
63        self.language = language;
64    }
65
66    pub fn get_version(&self) -> i32 {
67        self.version
68    }
69
70    pub fn set_version(&mut self, version: i32) {
71        self.version = version;
72    }
73}
74
75#[cfg(test)]
76mod tests {
77    use super::*;
78
79    #[test]
80    fn accessors_and_serde_preserve_the_connection_contract() {
81        let mut conn = Connection::new();
82        conn.set_client_id(CheetahString::from("id"));
83        conn.set_client_addr(CheetahString::from("addr"));
84        conn.set_language(LanguageCode::RUST);
85        conn.set_version(100);
86
87        assert_eq!(conn.get_client_id(), "id");
88        assert_eq!(conn.get_client_addr(), "addr");
89        assert_eq!(conn.get_language(), LanguageCode::RUST);
90        assert_eq!(conn.get_version(), 100);
91
92        let value = serde_json::to_value(&conn).expect("serialize connection");
93        assert_eq!(
94            value,
95            serde_json::json!({
96                "clientId": "id",
97                "clientAddr": "addr",
98                "language": "RUST",
99                "version": 100
100            })
101        );
102
103        let deserialized: Connection = serde_json::from_value(value).expect("deserialize connection");
104        assert_eq!(conn, deserialized);
105    }
106}