Skip to main content

rocketmq_protocol/protocol/header/namesrv/
topic_operation_header.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 cheetah_string::CheetahString;
16use rocketmq_macros::RequestHeaderCodec;
17use serde::Deserialize;
18use serde::Serialize;
19
20use crate::rpc::rpc_request_header::RpcRequestHeader;
21
22#[derive(Debug, Clone, Deserialize, Serialize, Default, RequestHeaderCodec)]
23#[header(
24    type_id = "rocketmq_protocol::protocol::header::namesrv::topic_operation_header::DeleteTopicFromNamesrvRequestHeader",
25    java_class = "org.apache.rocketmq.remoting.protocol.header.namesrv.DeleteTopicFromNamesrvRequestHeader",
26    fast
27)]
28#[serde(rename_all = "camelCase")]
29pub struct DeleteTopicFromNamesrvRequestHeader {
30    #[header(required)]
31    pub topic: CheetahString,
32    pub cluster_name: Option<CheetahString>,
33    #[serde(flatten)]
34    #[header(flatten, presence = "always")]
35    pub topic_request: Option<TopicRequestHeader>,
36}
37
38impl DeleteTopicFromNamesrvRequestHeader {
39    pub fn new(topic: impl Into<CheetahString>, cluster_name: Option<impl Into<CheetahString>>) -> Self {
40        Self {
41            topic: topic.into(),
42            cluster_name: cluster_name.map(|s| s.into()),
43            topic_request: None,
44        }
45    }
46}
47
48#[derive(Debug, Clone, Deserialize, Serialize, Default, RequestHeaderCodec)]
49#[header(
50    type_id = "rocketmq_protocol::protocol::header::namesrv::topic_operation_header::RegisterTopicRequestHeader",
51    java_class = "org.apache.rocketmq.remoting.protocol.header.namesrv.RegisterTopicRequestHeader"
52)]
53#[serde(rename_all = "camelCase")]
54pub struct RegisterTopicRequestHeader {
55    #[header(required)]
56    pub topic: CheetahString,
57    #[serde(flatten)]
58    #[header(flatten, presence = "always")]
59    pub topic_request: Option<TopicRequestHeader>,
60}
61
62impl RegisterTopicRequestHeader {
63    pub fn new(topic: impl Into<CheetahString>) -> Self {
64        Self {
65            topic: topic.into(),
66            topic_request: None,
67        }
68    }
69}
70
71#[derive(Debug, Clone, Deserialize, Serialize, Default, RequestHeaderCodec)]
72#[header(
73    type_id = "rocketmq_protocol::protocol::header::namesrv::topic_operation_header::GetTopicsByClusterRequestHeader",
74    java_class = "org.apache.rocketmq.remoting.protocol.header.GetTopicsByClusterRequestHeader"
75)]
76pub struct GetTopicsByClusterRequestHeader {
77    #[header(required)]
78    pub cluster: CheetahString,
79}
80
81impl GetTopicsByClusterRequestHeader {
82    pub fn new(cluster: impl Into<CheetahString>) -> Self {
83        Self {
84            cluster: cluster.into(),
85        }
86    }
87}
88
89#[derive(Debug, Clone, Deserialize, Serialize, Default, RequestHeaderCodec)]
90#[header(
91    type_id = "rocketmq_protocol::protocol::header::namesrv::topic_operation_header::TopicRequestHeader",
92    java_class = "org.apache.rocketmq.remoting.rpc.TopicRequestHeader"
93)]
94#[serde(rename_all = "camelCase")]
95pub struct TopicRequestHeader {
96    pub lo: Option<bool>,
97    #[serde(flatten)]
98    #[header(flatten, presence = "always")]
99    pub rpc: Option<RpcRequestHeader>,
100}
101
102#[cfg(test)]
103mod tests {
104    use cheetah_string::CheetahString;
105
106    use super::*;
107
108    #[test]
109    fn delete_topic_from_namesrv_request_header_serialization() {
110        let header = DeleteTopicFromNamesrvRequestHeader::new("topic1", Some("cluster1"));
111        let serialized = serde_json::to_string(&header).unwrap();
112        assert_eq!(serialized, r#"{"topic":"topic1","clusterName":"cluster1"}"#);
113    }
114
115    #[test]
116    fn delete_topic_from_namesrv_request_header_deserialization() {
117        let json = r#"{"topic":"topic1","clusterName":"cluster1"}"#;
118        let deserialized: DeleteTopicFromNamesrvRequestHeader = serde_json::from_str(json).unwrap();
119        assert_eq!(deserialized.topic, CheetahString::from("topic1"));
120        assert_eq!(deserialized.cluster_name, Some(CheetahString::from("cluster1")));
121    }
122
123    #[test]
124    fn register_topic_request_header_serialization() {
125        let header = RegisterTopicRequestHeader::new("topic1");
126        let serialized = serde_json::to_string(&header).unwrap();
127        assert_eq!(serialized, r#"{"topic":"topic1"}"#);
128    }
129
130    #[test]
131    fn get_topics_by_cluster_request_header_serialization() {
132        let header = GetTopicsByClusterRequestHeader::new("cluster1");
133        let serialized = serde_json::to_string(&header).unwrap();
134        assert_eq!(serialized, r#"{"cluster":"cluster1"}"#);
135    }
136
137    #[test]
138    fn get_topics_by_cluster_request_header_deserialization() {
139        let json = r#"{"cluster":"cluster1"}"#;
140        let deserialized: GetTopicsByClusterRequestHeader = serde_json::from_str(json).unwrap();
141        assert_eq!(deserialized.cluster, CheetahString::from("cluster1"));
142    }
143}