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