Skip to main content

rocketmq_client_rust/common/
admin_tool_result.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 crate::common::admin_tools_result_code_enum::AdminToolsResultCodeEnum;
16
17pub struct AdminToolResult<T> {
18    success: bool,
19    code: i32,
20    error_msg: String,
21    data: Option<T>,
22}
23
24#[allow(dead_code)]
25impl<T> AdminToolResult<T> {
26    pub fn new(success: bool, code: i32, error_msg: String, data: Option<T>) -> Self {
27        Self {
28            success,
29            code,
30            error_msg,
31            data,
32        }
33    }
34
35    pub fn success(data: T) -> Self {
36        Self::new(
37            true,
38            AdminToolsResultCodeEnum::Success.get_code(),
39            "success".to_string(),
40            Some(data),
41        )
42    }
43
44    pub fn failure(error_code_enum: AdminToolsResultCodeEnum, error_msg: String) -> Self {
45        Self::new(false, error_code_enum.get_code(), error_msg, None)
46    }
47
48    pub fn failure_with_data(error_code_enum: AdminToolsResultCodeEnum, error_msg: String, data: T) -> Self {
49        Self::new(false, error_code_enum.get_code(), error_msg, Some(data))
50    }
51
52    #[inline]
53    pub fn is_success(&self) -> bool {
54        self.success
55    }
56
57    #[inline]
58    pub fn set_success(&mut self, success: bool) {
59        self.success = success;
60    }
61
62    #[inline]
63    pub fn get_code(&self) -> i32 {
64        self.code
65    }
66
67    #[inline]
68    pub fn set_code(&mut self, code: i32) {
69        self.code = code;
70    }
71
72    #[inline]
73    pub fn get_error_msg(&self) -> &str {
74        &self.error_msg
75    }
76
77    #[inline]
78    pub fn set_error_msg(&mut self, error_msg: String) {
79        self.error_msg = error_msg;
80    }
81
82    #[inline]
83    pub fn get_data(&self) -> Option<&T> {
84        self.data.as_ref()
85    }
86
87    #[inline]
88    pub fn set_data(&mut self, data: T) {
89        self.data = Some(data);
90    }
91}
92
93#[cfg(test)]
94mod tests {
95    use super::*;
96    use crate::common::admin_tools_result_code_enum::AdminToolsResultCodeEnum;
97
98    #[test]
99    fn new_initializes_correctly() {
100        let result = AdminToolResult::new(true, 200, "success".to_string(), Some(42));
101        assert!(result.is_success());
102        assert_eq!(result.get_code(), 200);
103        assert_eq!(result.get_error_msg(), "success");
104        assert_eq!(result.get_data(), Some(&42));
105    }
106
107    #[test]
108    fn success_initializes_correctly() {
109        let result = AdminToolResult::success(42);
110        assert!(result.is_success());
111        assert_eq!(result.get_code(), 200);
112        assert_eq!(result.get_error_msg(), "success");
113        assert_eq!(result.get_data(), Some(&42));
114    }
115
116    #[test]
117    fn failure_initializes_correctly() {
118        let result = AdminToolResult::<i32>::failure(AdminToolsResultCodeEnum::RemotingError, "error".to_string());
119        assert!(!result.is_success());
120        assert_eq!(result.get_code(), -1001);
121        assert_eq!(result.get_error_msg(), "error");
122        assert!(result.get_data().is_none());
123    }
124
125    #[test]
126    fn failure_with_data_initializes_correctly() {
127        let result =
128            AdminToolResult::failure_with_data(AdminToolsResultCodeEnum::RemotingError, "error".to_string(), 42);
129        assert!(!result.is_success());
130        assert_eq!(result.get_code(), -1001);
131        assert_eq!(result.get_error_msg(), "error");
132        assert_eq!(result.get_data(), Some(&42));
133    }
134
135    #[test]
136    fn set_success_updates_success() {
137        let mut result = AdminToolResult::new(false, 200, "success".to_string(), Some(42));
138        result.set_success(true);
139        assert!(result.is_success());
140    }
141
142    #[test]
143    fn set_code_updates_code() {
144        let mut result = AdminToolResult::new(true, 200, "success".to_string(), Some(42));
145        result.set_code(-1001);
146        assert_eq!(result.get_code(), -1001);
147    }
148
149    #[test]
150    fn set_error_msg_updates_error_msg() {
151        let mut result = AdminToolResult::new(true, 200, "success".to_string(), Some(42));
152        result.set_error_msg("new error".to_string());
153        assert_eq!(result.get_error_msg(), "new error");
154    }
155
156    #[test]
157    fn set_data_updates_data() {
158        let mut result = AdminToolResult::new(true, 200, "success".to_string(), Some(42));
159        result.set_data(43);
160        assert_eq!(result.get_data(), Some(&43));
161    }
162}