Skip to main content

rocketmq_protocol/protocol/subscription/
group_forbidden.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::fmt;
16
17use cheetah_string::CheetahString;
18use serde::Deserialize;
19use serde::Serialize;
20
21#[derive(Debug, Eq, Clone, Default, Serialize, Deserialize)]
22#[serde(rename_all = "camelCase")]
23pub struct GroupForbidden {
24    topic: CheetahString,
25    group: CheetahString,
26    readable: Option<bool>,
27}
28
29impl GroupForbidden {
30    pub fn new(topic: CheetahString, group: CheetahString, readable: Option<bool>) -> Self {
31        Self { topic, group, readable }
32    }
33
34    #[inline]
35    pub fn topic(&self) -> &CheetahString {
36        &self.topic
37    }
38
39    #[inline]
40    pub fn set_topic(&mut self, topic: CheetahString) {
41        self.topic = topic;
42    }
43
44    #[inline]
45    pub fn group(&self) -> &CheetahString {
46        &self.group
47    }
48
49    #[inline]
50    pub fn set_group(&mut self, group: CheetahString) {
51        self.group = group;
52    }
53
54    #[inline]
55    pub fn readable(&self) -> Option<bool> {
56        self.readable
57    }
58
59    #[inline]
60    pub fn set_readable(&mut self, readable: Option<bool>) {
61        self.readable = readable;
62    }
63}
64
65impl fmt::Display for GroupForbidden {
66    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67        write!(
68            f,
69            "GroupForbidden [topic={}, group={}, readable={:?}]",
70            self.topic, self.group, self.readable
71        )
72    }
73}
74
75impl std::hash::Hash for GroupForbidden {
76    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
77        self.topic.hash(state);
78        self.group.hash(state);
79        self.readable.hash(state);
80    }
81}
82
83impl PartialEq for GroupForbidden {
84    fn eq(&self, other: &Self) -> bool {
85        self.topic == other.topic && self.group == other.group && self.readable == other.readable
86    }
87}
88
89#[cfg(test)]
90mod tests {
91    use std::collections::HashSet;
92
93    use cheetah_string::CheetahString;
94
95    use super::*;
96
97    #[test]
98    fn methods_serde_and_display_preserve_group_forbidden() {
99        let mut group_forbidden = GroupForbidden::new(
100            CheetahString::from("initialTopic"),
101            CheetahString::from("initialGroup"),
102            Some(false),
103        );
104        group_forbidden.set_topic(CheetahString::from("testTopic"));
105        group_forbidden.set_group(CheetahString::from("testGroup"));
106        group_forbidden.set_readable(Some(true));
107
108        assert_eq!(group_forbidden.topic(), &CheetahString::from("testTopic"));
109        assert_eq!(group_forbidden.group(), "testGroup");
110        assert_eq!(group_forbidden.readable(), Some(true));
111        assert_eq!(
112            group_forbidden.to_string(),
113            "GroupForbidden [topic=testTopic, group=testGroup, readable=Some(true)]"
114        );
115
116        let json = serde_json::to_string(&group_forbidden).unwrap();
117        assert_eq!(json, r#"{"topic":"testTopic","group":"testGroup","readable":true}"#);
118        let decoded: GroupForbidden = serde_json::from_str(&json).unwrap();
119        assert_eq!(decoded, group_forbidden);
120    }
121
122    #[test]
123    fn equality_and_hash_use_all_fields() {
124        let group_forbidden = GroupForbidden::new(
125            CheetahString::from("testTopic"),
126            CheetahString::from("testGroup"),
127            Some(true),
128        );
129        let equal = GroupForbidden::new(
130            CheetahString::from("testTopic"),
131            CheetahString::from("testGroup"),
132            Some(true),
133        );
134        assert_eq!(group_forbidden, equal);
135        assert!(HashSet::from([group_forbidden.clone()]).contains(&equal));
136
137        for different in [
138            GroupForbidden::new("differentTopic".into(), "testGroup".into(), Some(true)),
139            GroupForbidden::new("testTopic".into(), "differentGroup".into(), Some(true)),
140            GroupForbidden::new("testTopic".into(), "testGroup".into(), None),
141        ] {
142            assert_ne!(group_forbidden, different);
143        }
144    }
145}