Skip to main content

rocketmq_model/common/entity/
client_group.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 std::hash::Hash;
16use std::hash::Hasher;
17
18use cheetah_string::CheetahString;
19use serde::Deserialize;
20use serde::Serialize;
21
22#[derive(Debug, Clone, Serialize, Deserialize, Default, Eq)]
23pub struct ClientGroup {
24    #[serde(default)]
25    pub client_id: CheetahString,
26
27    #[serde(default)]
28    pub group: CheetahString,
29}
30
31impl ClientGroup {
32    #[must_use]
33    pub fn new() -> Self {
34        Self::default()
35    }
36
37    pub fn from_parts(client_id: CheetahString, group: CheetahString) -> Self {
38        Self { client_id, group }
39    }
40
41    #[must_use]
42    pub fn client_id(&self) -> &CheetahString {
43        &self.client_id
44    }
45
46    pub fn with_client_id(&mut self, client_id: CheetahString) -> &mut Self {
47        self.client_id = client_id;
48        self
49    }
50
51    #[must_use]
52    pub fn group(&self) -> &CheetahString {
53        &self.group
54    }
55
56    pub fn with_group(&mut self, group: CheetahString) -> &mut Self {
57        self.group = group;
58        self
59    }
60}
61
62impl PartialEq for ClientGroup {
63    fn eq(&self, other: &Self) -> bool {
64        self.client_id == other.client_id && self.group == other.group
65    }
66}
67
68impl Hash for ClientGroup {
69    fn hash<H: Hasher>(&self, state: &mut H) {
70        self.client_id.hash(state);
71        self.group.hash(state);
72    }
73}
74
75#[cfg(test)]
76mod tests {
77    use super::*;
78
79    #[test]
80    fn client_group_default() {
81        let cg = ClientGroup::default();
82        assert!(cg.client_id().is_empty());
83        assert!(cg.group().is_empty());
84    }
85
86    #[test]
87    fn client_group_from_parts() {
88        let cg = ClientGroup::from_parts("client".into(), "group".into());
89        assert_eq!(cg.client_id(), "client");
90        assert_eq!(cg.group(), "group");
91    }
92
93    #[test]
94    fn client_group_getters_and_setters() {
95        let mut cg = ClientGroup::new();
96        cg.with_client_id("client".into()).with_group("group".into());
97        assert_eq!(cg.client_id(), "client");
98        assert_eq!(cg.group(), "group");
99    }
100
101    #[test]
102    fn client_group_partial_eq_and_hash() {
103        let cg1 = ClientGroup::from_parts("client".into(), "group".into());
104        let cg2 = ClientGroup::from_parts("client".into(), "group".into());
105        let cg3 = ClientGroup::from_parts("client2".into(), "group".into());
106        assert_eq!(cg1, cg2);
107        assert_ne!(cg1, cg3);
108
109        use std::collections::hash_map::DefaultHasher;
110        let mut hasher1 = DefaultHasher::new();
111        cg1.hash(&mut hasher1);
112        let mut hasher2 = DefaultHasher::new();
113        cg2.hash(&mut hasher2);
114        assert_eq!(hasher1.finish(), hasher2.finish());
115    }
116
117    #[test]
118    fn client_group_serialization_and_deserialization() {
119        let cg = ClientGroup::from_parts("client".into(), "group".into());
120        let json = serde_json::to_string(&cg).unwrap();
121        let expected = r#"{"client_id":"client","group":"group"}"#;
122        assert_eq!(json, expected);
123        let decoded: ClientGroup = serde_json::from_str(&json).unwrap();
124        assert_eq!(decoded, cg);
125    }
126}