Skip to main content

rocketmq_protocol/protocol/subscription/
simple_subscription_data.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 serde::Deserialize;
16use serde::Serialize;
17
18#[derive(Debug, Clone, Default, Serialize, Deserialize, Hash, Eq, PartialEq)]
19#[serde(rename_all = "camelCase")]
20pub struct SimpleSubscriptionData {
21    topic: String,
22    expression_type: String,
23    expression: String,
24    version: u64,
25}
26
27impl SimpleSubscriptionData {
28    pub fn new(topic: String, expression_type: String, expression: String, version: u64) -> Self {
29        SimpleSubscriptionData {
30            topic,
31            expression_type,
32            expression,
33            version,
34        }
35    }
36
37    pub fn topic(&self) -> &str {
38        &self.topic
39    }
40
41    pub fn expression_type(&self) -> &str {
42        &self.expression_type
43    }
44
45    pub fn expression(&self) -> &str {
46        &self.expression
47    }
48
49    pub fn version(&self) -> u64 {
50        self.version
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn serde_preserves_subscription_data() {
60        let data = SimpleSubscriptionData::new("topic".to_string(), "TAG".to_string(), "*".to_string(), 123);
61        assert_eq!(
62            (data.topic(), data.expression_type(), data.expression(), data.version()),
63            ("topic", "TAG", "*", 123)
64        );
65
66        let json = serde_json::to_string(&data).unwrap();
67        assert_eq!(
68            json,
69            r#"{"topic":"topic","expressionType":"TAG","expression":"*","version":123}"#
70        );
71        assert_eq!(serde_json::from_str::<SimpleSubscriptionData>(&json).unwrap(), data);
72    }
73}