Skip to main content

rocketmq_protocol/protocol/body/
check_rocksdb_cqwrite_progress_response_body.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 cheetah_string::CheetahString;
16use serde::Deserialize;
17use serde::Serialize;
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
20#[repr(i32)]
21pub enum CheckStatus {
22    CheckOk = 0,
23    CheckNotOk = 1,
24    CheckInProgress = 2,
25    CheckError = 3,
26}
27
28impl From<i32> for CheckStatus {
29    fn from(value: i32) -> Self {
30        match value {
31            0 => CheckStatus::CheckOk,
32            1 => CheckStatus::CheckNotOk,
33            2 => CheckStatus::CheckInProgress,
34            3 => CheckStatus::CheckError,
35            _ => CheckStatus::CheckError,
36        }
37    }
38}
39
40/// Result of checking RocksDB consume queue write progress.
41/// Corresponds to Java's `CheckRocksdbCqWriteResult`.
42#[derive(Serialize, Deserialize, Debug, Default, Clone)]
43#[serde(rename_all = "camelCase")]
44pub struct CheckRocksdbCqWriteResult {
45    pub check_result: Option<CheetahString>,
46    pub check_status: i32,
47}
48
49impl CheckRocksdbCqWriteResult {
50    pub fn get_check_status(&self) -> CheckStatus {
51        CheckStatus::from(self.check_status)
52    }
53}
54
55#[cfg(test)]
56mod tests {
57    use serde_json;
58
59    use super::*;
60
61    #[test]
62    fn serialize_check_rocksdb_cq_write_result() {
63        let result = CheckRocksdbCqWriteResult {
64            check_result: Some(CheetahString::from("check doing")),
65            check_status: 2,
66        };
67        let serialized = serde_json::to_string(&result).unwrap();
68        assert!(serialized.contains("\"checkResult\":\"check doing\""));
69        assert!(serialized.contains("\"checkStatus\":2"));
70    }
71
72    #[test]
73    fn deserialize_check_rocksdb_cq_write_result() {
74        let json = r#"{"checkResult":"all ok","checkStatus":0}"#;
75        let result: CheckRocksdbCqWriteResult = serde_json::from_str(json).unwrap();
76        assert_eq!(result.check_result, Some(CheetahString::from("all ok")));
77        assert_eq!(result.check_status, 0);
78        assert_eq!(result.get_check_status(), CheckStatus::CheckOk);
79    }
80
81    #[test]
82    fn check_status_from_i32() {
83        assert_eq!(CheckStatus::from(0), CheckStatus::CheckOk);
84        assert_eq!(CheckStatus::from(1), CheckStatus::CheckNotOk);
85        assert_eq!(CheckStatus::from(2), CheckStatus::CheckInProgress);
86        assert_eq!(CheckStatus::from(3), CheckStatus::CheckError);
87        assert_eq!(CheckStatus::from(99), CheckStatus::CheckError);
88    }
89}