rocketmq_model/common/lite/
lite_subscription_dto.rs1use std::collections::HashSet;
16use std::fmt;
17
18use cheetah_string::CheetahString;
19use serde::Deserialize;
20use serde::Serialize;
21
22use super::LiteSubscriptionAction;
23use super::OffsetOption;
24
25#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
26#[serde(rename_all = "camelCase")]
27pub struct LiteSubscriptionDTO {
28 #[serde(default)]
29 pub action: LiteSubscriptionAction,
30
31 #[serde(default)]
32 pub client_id: CheetahString,
33
34 #[serde(default)]
35 pub group: CheetahString,
36
37 #[serde(default)]
38 pub topic: CheetahString,
39
40 #[serde(default)]
41 pub lite_topic_set: HashSet<CheetahString>,
42
43 #[serde(default)]
44 pub offset_option: Option<OffsetOption>,
45
46 #[serde(default)]
47 pub version: i64,
48}
49
50impl LiteSubscriptionDTO {
51 #[must_use]
52 #[inline]
53 pub fn new() -> Self {
54 Self {
55 action: LiteSubscriptionAction::default(),
56 client_id: CheetahString::new(),
57 group: CheetahString::new(),
58 topic: CheetahString::new(),
59 lite_topic_set: HashSet::new(),
60 offset_option: None,
61 version: 0,
62 }
63 }
64
65 #[must_use]
66 #[inline]
67 pub fn with_action(mut self, action: LiteSubscriptionAction) -> Self {
68 self.action = action;
69 self
70 }
71
72 #[must_use]
73 #[inline]
74 pub fn with_client_id(mut self, client_id: CheetahString) -> Self {
75 self.client_id = client_id;
76 self
77 }
78
79 #[must_use]
80 #[inline]
81 pub fn with_group(mut self, group: CheetahString) -> Self {
82 self.group = group;
83 self
84 }
85
86 #[must_use]
87 #[inline]
88 pub fn with_topic(mut self, topic: CheetahString) -> Self {
89 self.topic = topic;
90 self
91 }
92
93 #[must_use]
94 #[inline]
95 pub fn with_lite_topic_set(mut self, lite_topic_set: HashSet<CheetahString>) -> Self {
96 self.lite_topic_set = lite_topic_set;
97 self
98 }
99
100 #[must_use]
101 #[inline]
102 pub fn with_offset_option(mut self, offset_option: OffsetOption) -> Self {
103 self.offset_option = Some(offset_option);
104 self
105 }
106
107 #[must_use]
108 #[inline]
109 pub fn with_version(mut self, version: i64) -> Self {
110 self.version = version;
111 self
112 }
113
114 #[must_use]
115 #[inline]
116 pub const fn action(&self) -> LiteSubscriptionAction {
117 self.action
118 }
119
120 #[inline]
121 pub fn set_action(&mut self, action: LiteSubscriptionAction) {
122 self.action = action;
123 }
124
125 #[must_use]
126 #[inline]
127 pub fn client_id(&self) -> &CheetahString {
128 &self.client_id
129 }
130
131 #[inline]
132 pub fn set_client_id(&mut self, client_id: CheetahString) {
133 self.client_id = client_id;
134 }
135
136 #[must_use]
137 #[inline]
138 pub fn group(&self) -> &CheetahString {
139 &self.group
140 }
141
142 #[inline]
143 pub fn set_group(&mut self, group: CheetahString) {
144 self.group = group;
145 }
146
147 #[must_use]
148 #[inline]
149 pub fn topic(&self) -> &CheetahString {
150 &self.topic
151 }
152
153 #[inline]
154 pub fn set_topic(&mut self, topic: CheetahString) {
155 self.topic = topic;
156 }
157
158 #[must_use]
159 #[inline]
160 pub fn lite_topic_set(&self) -> &HashSet<CheetahString> {
161 &self.lite_topic_set
162 }
163
164 #[inline]
165 pub fn set_lite_topic_set(&mut self, lite_topic_set: HashSet<CheetahString>) {
166 self.lite_topic_set = lite_topic_set;
167 }
168
169 #[must_use]
170 #[inline]
171 pub fn offset_option(&self) -> Option<OffsetOption> {
172 self.offset_option
173 }
174
175 #[inline]
176 pub fn set_offset_option(&mut self, offset_option: OffsetOption) {
177 self.offset_option = Some(offset_option);
178 }
179
180 #[must_use]
181 #[inline]
182 pub const fn version(&self) -> i64 {
183 self.version
184 }
185
186 #[inline]
187 pub fn set_version(&mut self, version: i64) {
188 self.version = version;
189 }
190}
191
192impl Default for LiteSubscriptionDTO {
193 #[inline]
194 fn default() -> Self {
195 Self::new()
196 }
197}
198
199impl fmt::Display for LiteSubscriptionDTO {
200 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
201 write!(
202 f,
203 "LiteSubscriptionDTO {{ action: {}, client_id: {}, group: {}, topic: {}, lite_topic_set: {:?}, \
204 offset_option: {:?}, version: {} }}",
205 self.action, self.client_id, self.group, self.topic, self.lite_topic_set, self.offset_option, self.version
206 )
207 }
208}
209
210#[cfg(test)]
211mod tests {
212 use super::*;
213
214 #[test]
215 fn lite_subscription_dto_default() {
216 let dto = LiteSubscriptionDTO::default();
217 assert_eq!(dto.action(), LiteSubscriptionAction::default());
218 assert!(dto.client_id().is_empty());
219 assert!(dto.group().is_empty());
220 assert!(dto.topic().is_empty());
221 assert!(dto.lite_topic_set().is_empty());
222 assert!(dto.offset_option().is_none());
223 assert_eq!(dto.version(), 0);
224 }
225
226 #[test]
227 fn lite_subscription_dto_with_methods() {
228 let mut set = HashSet::new();
229 set.insert("lite_topic".into());
230 let offset = OffsetOption::offset(100);
231
232 let dto = LiteSubscriptionDTO::new()
233 .with_action(LiteSubscriptionAction::CompleteAdd)
234 .with_client_id("client".into())
235 .with_group("group".into())
236 .with_topic("topic".into())
237 .with_lite_topic_set(set.clone())
238 .with_offset_option(offset)
239 .with_version(1);
240
241 assert_eq!(dto.action(), LiteSubscriptionAction::CompleteAdd);
242 assert_eq!(dto.client_id(), "client");
243 assert_eq!(dto.group(), "group");
244 assert_eq!(dto.topic(), "topic");
245 assert_eq!(dto.lite_topic_set(), &set);
246 assert_eq!(dto.offset_option(), Some(offset));
247 assert_eq!(dto.version(), 1);
248 }
249
250 #[test]
251 fn lite_subscription_dto_setters() {
252 let mut dto = LiteSubscriptionDTO::new();
253 dto.set_action(LiteSubscriptionAction::CompleteRemove);
254 dto.set_client_id("client2".into());
255 dto.set_group("group2".into());
256 dto.set_topic("topic2".into());
257
258 let mut set = HashSet::new();
259 set.insert("topic1".into());
260 dto.set_lite_topic_set(set.clone());
261
262 let offset = OffsetOption::offset(200);
263 dto.set_offset_option(offset);
264 dto.set_version(2);
265
266 assert_eq!(dto.action(), LiteSubscriptionAction::CompleteRemove);
267 assert_eq!(dto.client_id(), "client2");
268 assert_eq!(dto.group(), "group2");
269 assert_eq!(dto.topic(), "topic2");
270 assert_eq!(dto.lite_topic_set(), &set);
271 assert_eq!(dto.offset_option(), Some(offset));
272 assert_eq!(dto.version(), 2);
273 }
274
275 #[test]
276 fn lite_subscription_dto_display() {
277 let dto = LiteSubscriptionDTO::new();
278 let display = format!("{}", dto);
279 let expected = "LiteSubscriptionDTO { action: PARTIAL_ADD, client_id: , group: , topic: , lite_topic_set: {}, \
280 offset_option: None, version: 0 }";
281 assert_eq!(display, expected);
282 }
283
284 #[test]
285 fn lite_subscription_dto_serde() {
286 let dto = LiteSubscriptionDTO::new().with_topic("topic".into());
287 let json = serde_json::to_string(&dto).unwrap();
288 let expected = r#"{"action":"PARTIAL_ADD","clientId":"","group":"","topic":"topic","liteTopicSet":[],"offsetOption":null,"version":0}"#;
289 assert_eq!(json, expected);
290 let decoded: LiteSubscriptionDTO = serde_json::from_str(&json).unwrap();
291 assert_eq!(dto, decoded);
292 }
293}