Skip to main content

rocketmq_protocol/protocol/
request_source.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
15#[derive(Debug, PartialEq, Clone, Copy)]
16#[repr(i32)]
17pub enum RequestSource {
18    Unknown = -2,
19    SDK = -1,
20    ProxyForOrder = 0,
21    ProxyForBroadcast = 1,
22    ProxyForStream = 2,
23}
24
25impl RequestSource {
26    pub const SYSTEM_PROPERTY_KEY: &'static str = "rocketmq.requestSource";
27
28    pub fn get_value(&self) -> i32 {
29        match self {
30            RequestSource::SDK => -1,
31            RequestSource::ProxyForOrder => 0,
32            RequestSource::ProxyForBroadcast => 1,
33            RequestSource::ProxyForStream => 2,
34            _ => -2,
35        }
36    }
37
38    pub fn is_valid(value: Option<i32>) -> bool {
39        if let Some(v) = value {
40            (-1..3).contains(&v)
41        } else {
42            false
43        }
44    }
45
46    pub fn parse_integer(value: Option<i32>) -> RequestSource {
47        if let Some(v) = value {
48            if Self::is_valid(Some(v)) {
49                return Self::from_value(v);
50            }
51        }
52        RequestSource::Unknown
53    }
54
55    pub fn from_value(value: i32) -> RequestSource {
56        match value {
57            -1 => RequestSource::SDK,
58            0 => RequestSource::ProxyForOrder,
59            1 => RequestSource::ProxyForBroadcast,
60            2 => RequestSource::ProxyForStream,
61            _ => RequestSource::Unknown,
62        }
63    }
64}
65
66impl From<i32> for RequestSource {
67    fn from(value: i32) -> Self {
68        RequestSource::from_value(value)
69    }
70}
71
72#[cfg(test)]
73mod request_source_tests {
74    use super::*;
75
76    #[test]
77    fn parse_integer_returns_unknown_for_out_of_range_positive_values() {
78        assert_eq!(RequestSource::parse_integer(Some(3)), RequestSource::Unknown);
79    }
80
81    #[test]
82    fn parse_integer_returns_unknown_for_out_of_range_negative_values() {
83        assert_eq!(RequestSource::parse_integer(Some(-3)), RequestSource::Unknown);
84    }
85
86    #[test]
87    fn from_value_returns_correct_variant_for_known_values() {
88        assert_eq!(RequestSource::from_value(-1), RequestSource::SDK);
89        assert_eq!(RequestSource::from_value(0), RequestSource::ProxyForOrder);
90        assert_eq!(RequestSource::from_value(1), RequestSource::ProxyForBroadcast);
91        assert_eq!(RequestSource::from_value(2), RequestSource::ProxyForStream);
92    }
93
94    #[test]
95    fn from_value_returns_unknown_for_unknown_values() {
96        assert_eq!(RequestSource::from_value(4), RequestSource::Unknown);
97        assert_eq!(RequestSource::from_value(-3), RequestSource::Unknown);
98    }
99
100    #[test]
101    fn is_valid_identifies_only_known_values_as_valid() {
102        assert!(RequestSource::is_valid(Some(-1)));
103        assert!(RequestSource::is_valid(Some(0)));
104        assert!(RequestSource::is_valid(Some(1)));
105        assert!(RequestSource::is_valid(Some(2)));
106        assert!(!RequestSource::is_valid(Some(3)));
107        assert!(!RequestSource::is_valid(Some(-3)));
108    }
109
110    #[test]
111    fn get_value_returns_expected_integer_representation() {
112        assert_eq!(RequestSource::SDK.get_value(), -1);
113        assert_eq!(RequestSource::ProxyForOrder.get_value(), 0);
114        assert_eq!(RequestSource::ProxyForBroadcast.get_value(), 1);
115        assert_eq!(RequestSource::ProxyForStream.get_value(), 2);
116        assert_eq!(RequestSource::Unknown.get_value(), -2);
117    }
118}