Skip to main content

rocketmq_protocol/protocol/header/
get_max_offset_request_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::protocol::header::message_operation_header::TopicRequestHeaderTrait;
21use crate::rpc::topic_request_header::TopicRequestHeader;
22
23const fn default_committed() -> bool {
24    true
25}
26
27#[derive(Debug, Clone, Serialize, Deserialize, RequestHeaderCodec)]
28#[header(
29    type_id = "rocketmq_protocol::protocol::header::get_max_offset_request_header::GetMaxOffsetRequestHeader",
30    java_class = "org.apache.rocketmq.remoting.protocol.header.GetMaxOffsetRequestHeader"
31)]
32#[serde(rename_all = "camelCase")]
33pub struct GetMaxOffsetRequestHeader {
34    #[header(required)]
35    pub topic: CheetahString,
36
37    #[header(required)]
38    pub queue_id: i32,
39
40    #[serde(default = "default_committed")]
41    #[header(default_with = "default_committed", default_semantic = "literal:true")]
42    pub committed: bool,
43
44    #[serde(flatten)]
45    #[header(flatten, presence = "always")]
46    pub topic_request_header: Option<TopicRequestHeader>,
47}
48
49impl TopicRequestHeaderTrait for GetMaxOffsetRequestHeader {
50    fn set_lo(&mut self, lo: Option<bool>) {
51        if let Some(header) = self.topic_request_header.as_mut() {
52            header.lo = lo;
53        }
54    }
55
56    fn lo(&self) -> Option<bool> {
57        self.topic_request_header.as_ref().and_then(|h| h.lo)
58    }
59
60    fn set_topic(&mut self, topic: CheetahString) {
61        self.topic = topic;
62    }
63
64    fn topic(&self) -> &CheetahString {
65        &self.topic
66    }
67
68    fn broker_name(&self) -> Option<&CheetahString> {
69        self.topic_request_header
70            .as_ref()
71            .and_then(|h| h.rpc_request_header.as_ref())
72            .and_then(|h| h.broker_name.as_ref())
73    }
74
75    fn set_broker_name(&mut self, broker_name: CheetahString) {
76        if let Some(header) = self.topic_request_header.as_mut() {
77            if let Some(rpc_header) = header.rpc_request_header.as_mut() {
78                rpc_header.broker_name = Some(broker_name);
79            }
80        }
81    }
82
83    fn namespace(&self) -> Option<&str> {
84        self.topic_request_header
85            .as_ref()
86            .and_then(|h| h.rpc_request_header.as_ref())
87            .and_then(|r| r.namespace.as_deref())
88    }
89
90    fn set_namespace(&mut self, namespace: CheetahString) {
91        if let Some(header) = self.topic_request_header.as_mut() {
92            if let Some(rpc_header) = header.rpc_request_header.as_mut() {
93                rpc_header.namespace = Some(namespace);
94            }
95        }
96    }
97
98    fn namespaced(&self) -> Option<bool> {
99        self.topic_request_header
100            .as_ref()
101            .and_then(|h| h.rpc_request_header.as_ref())
102            .and_then(|r| r.namespaced)
103    }
104
105    fn set_namespaced(&mut self, namespaced: bool) {
106        if let Some(header) = self.topic_request_header.as_mut() {
107            if let Some(rpc_header) = header.rpc_request_header.as_mut() {
108                rpc_header.namespaced = Some(namespaced);
109            }
110        }
111    }
112
113    fn oneway(&self) -> Option<bool> {
114        self.topic_request_header
115            .as_ref()
116            .and_then(|h| h.rpc_request_header.as_ref())
117            .and_then(|r| r.oneway)
118    }
119
120    fn set_oneway(&mut self, oneway: bool) {
121        if let Some(header) = self.topic_request_header.as_mut() {
122            if let Some(rpc_header) = header.rpc_request_header.as_mut() {
123                rpc_header.oneway = Some(oneway);
124            }
125        }
126    }
127
128    fn queue_id(&self) -> i32 {
129        self.queue_id
130    }
131
132    fn set_queue_id(&mut self, queue_id: i32) {
133        self.queue_id = queue_id;
134    }
135}
136
137#[cfg(test)]
138mod tests {
139    use super::*;
140
141    #[test]
142    fn serde_defaults_committed_to_java_default() {
143        let header: GetMaxOffsetRequestHeader = serde_json::from_str(r#"{"topic":"topic-a","queueId":1}"#).unwrap();
144
145        assert!(header.committed);
146    }
147
148    #[test]
149    fn nested_setters_are_noops_without_an_rpc_envelope() {
150        let mut header = GetMaxOffsetRequestHeader {
151            topic: CheetahString::from("topic-a"),
152            queue_id: 1,
153            committed: true,
154            topic_request_header: None,
155        };
156
157        header.set_lo(Some(true));
158        header.set_broker_name(CheetahString::from("broker-a"));
159        header.set_namespace(CheetahString::from("namespace-a"));
160        header.set_namespaced(true);
161        header.set_oneway(true);
162
163        assert_eq!(header.lo(), None);
164        assert_eq!(header.broker_name(), None);
165        assert_eq!(header.namespace(), None);
166        assert_eq!(header.namespaced(), None);
167        assert_eq!(header.oneway(), None);
168    }
169}