Skip to main content

rocketmq_protocol/code/
broker_request_code.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::str::FromStr;
16
17use rocketmq_error::Error;
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
20pub enum BrokerRequestCode {
21    RegisterBroker = 103,
22    BrokerHeartbeat = 904,
23    GetBrokerClusterInfo = 106,
24}
25
26impl BrokerRequestCode {
27    pub fn value_of(code: i32) -> Option<Self> {
28        match code {
29            103 => Some(BrokerRequestCode::RegisterBroker),
30            106 => Some(BrokerRequestCode::GetBrokerClusterInfo),
31            _ => None,
32        }
33    }
34
35    pub fn get_code(&self) -> i32 {
36        *self as i32
37    }
38
39    pub fn get_type_from_name(name: &str) -> Option<Self> {
40        match name.to_ascii_uppercase().as_str() {
41            "REGISTERBROKER" => Some(BrokerRequestCode::RegisterBroker),
42            "BROKERHEARTBEAT" => Some(BrokerRequestCode::BrokerHeartbeat),
43            "GETBROKERCLUSTERINFO" => Some(BrokerRequestCode::GetBrokerClusterInfo),
44            _ => None,
45        }
46    }
47}
48
49impl FromStr for BrokerRequestCode {
50    type Err = Error;
51
52    fn from_str(s: &str) -> Result<Self, Self::Err> {
53        match s.to_ascii_uppercase().as_str() {
54            "REGISTERBROKER" => Ok(BrokerRequestCode::RegisterBroker),
55            "BROKERHEARTBEAT" => Ok(BrokerRequestCode::BrokerHeartbeat),
56            "GETBROKERCLUSTERINFO" => Ok(BrokerRequestCode::GetBrokerClusterInfo),
57            _ => Err(crate::error::invalid_argument(format!(
58                "Parse from string error,Invalid BrokerRequestCode: {s}"
59            ))),
60        }
61    }
62}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67
68    #[test]
69    fn test_value_of() {
70        let code: i32 = 103;
71        assert_eq!(
72            BrokerRequestCode::value_of(code),
73            Some(BrokerRequestCode::RegisterBroker)
74        );
75
76        let code: i32 = 1;
77        assert_eq!(BrokerRequestCode::value_of(code), None);
78    }
79
80    #[test]
81    fn test_get_type_from_name() {
82        let name = "RegisterBroker";
83        assert_eq!(
84            BrokerRequestCode::get_type_from_name(name),
85            Some(BrokerRequestCode::RegisterBroker)
86        );
87
88        let name = "UNKNOW";
89        assert_eq!(BrokerRequestCode::get_type_from_name(name), None);
90    }
91}