rocketmq_remoting/protocol/
request_source.rs

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17#[derive(Debug, PartialEq, Clone, Copy)]
18#[repr(i32)]
19pub enum RequestSource {
20    Unknown = -2,
21    SDK = -1,
22    ProxyForOrder = 0,
23    ProxyForBroadcast = 1,
24    ProxyForStream = 2,
25}
26
27impl RequestSource {
28    pub const SYSTEM_PROPERTY_KEY: &'static str = "rocketmq.requestSource";
29
30    pub fn get_value(&self) -> i32 {
31        match self {
32            RequestSource::SDK => -1,
33            RequestSource::ProxyForOrder => 0,
34            RequestSource::ProxyForBroadcast => 1,
35            RequestSource::ProxyForStream => 2,
36            _ => -2,
37        }
38    }
39
40    pub fn is_valid(value: Option<i32>) -> bool {
41        if let Some(v) = value {
42            (-1..3).contains(&v)
43        } else {
44            false
45        }
46    }
47
48    pub fn parse_integer(value: Option<i32>) -> RequestSource {
49        if let Some(v) = value {
50            if Self::is_valid(Some(v)) {
51                return Self::from_value(v);
52            }
53        }
54        RequestSource::Unknown
55    }
56
57    pub fn from_value(value: i32) -> RequestSource {
58        match value {
59            -1 => RequestSource::SDK,
60            0 => RequestSource::ProxyForOrder,
61            1 => RequestSource::ProxyForBroadcast,
62            2 => RequestSource::ProxyForStream,
63            _ => RequestSource::Unknown,
64        }
65    }
66}
67
68impl From<i32> for RequestSource {
69    fn from(value: i32) -> Self {
70        RequestSource::from_value(value)
71    }
72}
73
74#[cfg(test)]
75mod request_source_tests {
76    use super::*;
77
78    #[test]
79    fn parse_integer_returns_unknown_for_out_of_range_positive_values() {
80        assert_eq!(
81            RequestSource::parse_integer(Some(3)),
82            RequestSource::Unknown
83        );
84    }
85
86    #[test]
87    fn parse_integer_returns_unknown_for_out_of_range_negative_values() {
88        assert_eq!(
89            RequestSource::parse_integer(Some(-3)),
90            RequestSource::Unknown
91        );
92    }
93
94    #[test]
95    fn from_value_returns_correct_variant_for_known_values() {
96        assert_eq!(RequestSource::from_value(-1), RequestSource::SDK);
97        assert_eq!(RequestSource::from_value(0), RequestSource::ProxyForOrder);
98        assert_eq!(
99            RequestSource::from_value(1),
100            RequestSource::ProxyForBroadcast
101        );
102        assert_eq!(RequestSource::from_value(2), RequestSource::ProxyForStream);
103    }
104
105    #[test]
106    fn from_value_returns_unknown_for_unknown_values() {
107        assert_eq!(RequestSource::from_value(4), RequestSource::Unknown);
108        assert_eq!(RequestSource::from_value(-3), RequestSource::Unknown);
109    }
110
111    #[test]
112    fn is_valid_identifies_only_known_values_as_valid() {
113        assert!(RequestSource::is_valid(Some(-1)));
114        assert!(RequestSource::is_valid(Some(0)));
115        assert!(RequestSource::is_valid(Some(1)));
116        assert!(RequestSource::is_valid(Some(2)));
117        assert!(!RequestSource::is_valid(Some(3)));
118        assert!(!RequestSource::is_valid(Some(-3)));
119    }
120
121    #[test]
122    fn get_value_returns_expected_integer_representation() {
123        assert_eq!(RequestSource::SDK.get_value(), -1);
124        assert_eq!(RequestSource::ProxyForOrder.get_value(), 0);
125        assert_eq!(RequestSource::ProxyForBroadcast.get_value(), 1);
126        assert_eq!(RequestSource::ProxyForStream.get_value(), 2);
127        assert_eq!(RequestSource::Unknown.get_value(), -2);
128    }
129}