Skip to main content

rocketmq_protocol/protocol/
request_type.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
17#[derive(Debug, PartialEq, Eq, Clone, Copy)]
18pub enum RequestType {
19    Stream,
20}
21
22impl Display for RequestType {
23    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24        match self {
25            RequestType::Stream => write!(f, "STREAM"),
26        }
27    }
28}
29
30impl RequestType {
31    pub fn value_of(code: u8) -> Option<Self> {
32        match code {
33            0 => Some(RequestType::Stream),
34            _ => None,
35        }
36    }
37
38    pub fn get_code(&self) -> u8 {
39        match self {
40            RequestType::Stream => 0,
41        }
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn value_of_maps_zero_to_stream() {
51        assert_eq!(RequestType::value_of(0), Some(RequestType::Stream));
52    }
53
54    #[test]
55    fn value_of_rejects_unknown_codes() {
56        assert_eq!(RequestType::value_of(1), None);
57        assert_eq!(RequestType::value_of(255), None);
58    }
59
60    #[test]
61    fn get_code_returns_zero_for_stream() {
62        assert_eq!(RequestType::Stream.get_code(), 0);
63    }
64
65    #[test]
66    fn display_prints_stream_for_stream_variant() {
67        assert_eq!(RequestType::Stream.to_string(), "STREAM");
68    }
69}