rocketmq_protocol/protocol/subscription/
simple_subscription_data.rs1use 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}