Skip to main content

rocketmq_remoting/protocol/header/
notify_broker_role_change_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 std::fmt::Display;
16
17use cheetah_string::CheetahString;
18use rocketmq_macros::RequestHeaderCodecV2;
19use serde::Deserialize;
20use serde::Serialize;
21
22#[derive(Serialize, Deserialize, Debug, RequestHeaderCodecV2)]
23#[serde(rename_all = "camelCase")]
24pub struct NotifyBrokerRoleChangedRequestHeader {
25    #[required]
26    pub master_address: Option<CheetahString>,
27
28    #[required]
29    pub master_epoch: Option<i32>,
30
31    #[required]
32    pub sync_state_set_epoch: Option<i32>,
33
34    #[required]
35    pub master_broker_id: Option<u64>,
36}
37
38impl Display for NotifyBrokerRoleChangedRequestHeader {
39    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40        write!(
41            f,
42            "(master_address={:?}, master_epoch={:?}, sync_state_set_epoch={:?}, master_broker_id={:?})",
43            self.master_address, self.master_epoch, self.sync_state_set_epoch, self.master_broker_id
44        )
45    }
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    #[test]
53    fn notify_broker_role_changed_request_header_display() {
54        let header = NotifyBrokerRoleChangedRequestHeader {
55            master_address: Some(CheetahString::from("addr")),
56            master_epoch: Some(1),
57            sync_state_set_epoch: Some(2),
58            master_broker_id: Some(3),
59        };
60        let display = format!("{}", header);
61        let expected = r#"(master_address=Some("addr"), master_epoch=Some(1), sync_state_set_epoch=Some(2), master_broker_id=Some(3))"#;
62        assert_eq!(display, expected);
63    }
64}