Skip to main content

zulip_api/message/
response.rs

1//! Zulip API response types.
2
3mod base;
4mod error_code;
5mod result;
6
7pub use base::BaseResponse;
8pub use error_code::ErrorCode;
9pub use result::ApiResult;
10
11/// Represents the common functions for all response types.
12pub trait Response {
13    /// Gets the [Response] `msg` field.
14    fn msg(&self) -> &str;
15
16    /// Sets the [Response] `msg` field.
17    fn set_msg<'a, C: Into<std::borrow::Cow<'a, str>>>(&mut self, msg: C);
18
19    /// Builder function that sets the [Response] `msg` field.
20    fn with_msg<'a, C: Into<std::borrow::Cow<'a, str>>>(self, msg: C) -> Self;
21
22    /// Gets the [Response] `result` field.
23    fn result(&self) -> ApiResult;
24
25    /// Sets the [Response] `result` field.
26    fn set_result(&mut self, result: ApiResult);
27
28    /// Builder function that sets the [Response] `result` field.
29    fn with_result(self, result: ApiResult) -> Self;
30
31    /// Gets the [Response] `code` field.
32    fn code(&self) -> Option<ErrorCode>;
33
34    /// Sets the [Response] `code` field.
35    fn set_code(&mut self, code: ErrorCode);
36
37    /// Unsets the [Response] `code` field.
38    fn unset_code(&mut self) -> Option<ErrorCode>;
39
40    /// Builder function that sets the [Response] `code` field.
41    fn with_code(self, code: ErrorCode) -> Self;
42
43    /// Gets the [Response] ignored unsupported parameters list.
44    ///
45    /// An array of any parameters sent in the request that are not
46    /// supported by the endpoint.
47    ///
48    /// See [error handling](https://zulip.com//api/rest-error-handling#ignored-parameters) documentation
49    /// for details on this and its change history.
50    fn ignored_parameters_unsupported(&self) -> Option<&[String]>;
51
52    /// Sets the [Response] ignored unsupported parameters list.
53    ///
54    /// An array of any parameters sent in the request that are not
55    /// supported by the endpoint.
56    ///
57    /// See [error handling](https://zulip.com//api/rest-error-handling#ignored-parameters) documentation
58    /// for details on this and its change history.
59    fn set_ignored_parameters_unsupported<'a, C: Into<std::borrow::Cow<'a, [String]>>>(
60        &mut self,
61        val: C,
62    );
63
64    /// Unsets the [Response] ignored unsupported parameters list.
65    ///
66    /// An array of any parameters sent in the request that are not
67    /// supported by the endpoint.
68    ///
69    /// See [error handling](https://zulip.com//api/rest-error-handling#ignored-parameters) documentation
70    /// for details on this and its change history.
71    fn unset_ignored_parameters_unsupported(&mut self) -> Option<Vec<String>>;
72
73    /// Builder function that sets the [Response] ignored unsupported parameters list.
74    ///
75    /// An array of any parameters sent in the request that are not
76    /// supported by the endpoint.
77    ///
78    /// See [error handling](https://zulip.com//api/rest-error-handling#ignored-parameters) documentation
79    /// for details on this and its change history.
80    fn with_ignored_parameters_unsupported<'a, C: Into<std::borrow::Cow<'a, [String]>>>(
81        self,
82        val: C,
83    ) -> Self;
84}
85
86/// Implements the [Response] trait for a given type.
87#[macro_export]
88macro_rules! impl_response {
89    ($ty:ident) => {
90        impl $crate::response::Response for $ty {
91            fn msg(&self) -> &str {
92                &self.msg
93            }
94
95            fn set_msg<'a, C: Into<std::borrow::Cow<'a, str>>>(&mut self, msg: C) {
96                self.msg = C::into(msg).into();
97            }
98
99            fn with_msg<'a, C: Into<std::borrow::Cow<'a, str>>>(self, msg: C) -> Self {
100                Self {
101                    msg: C::into(msg).into(),
102                    ..self
103                }
104            }
105
106            fn result(&self) -> $crate::response::ApiResult {
107                self.result
108            }
109
110            fn set_result(&mut self, result: $crate::response::ApiResult) {
111                self.result = result;
112            }
113
114            fn with_result(self, result: $crate::response::ApiResult) -> Self {
115                Self { result, ..self }
116            }
117
118            fn code(&self) -> Option<$crate::response::ErrorCode> {
119                self.code
120            }
121
122            fn set_code(&mut self, code: $crate::response::ErrorCode) {
123                self.code = Some(code);
124            }
125
126            fn unset_code(&mut self) -> Option<$crate::response::ErrorCode> {
127                self.code.take()
128            }
129
130            fn with_code(self, code: $crate::response::ErrorCode) -> Self {
131                Self {
132                    code: Some(code),
133                    ..self
134                }
135            }
136
137            fn ignored_parameters_unsupported(&self) -> Option<&[String]> {
138                self.ignored_parameters_unsupported.as_deref()
139            }
140
141            fn set_ignored_parameters_unsupported<'a, C: Into<std::borrow::Cow<'a, [String]>>>(
142                &mut self,
143                val: C,
144            ) {
145                self.ignored_parameters_unsupported = Some(C::into(val).into());
146            }
147
148            fn unset_ignored_parameters_unsupported(&mut self) -> Option<Vec<String>> {
149                self.ignored_parameters_unsupported.take()
150            }
151
152            fn with_ignored_parameters_unsupported<'a, C: Into<std::borrow::Cow<'a, [String]>>>(
153                self,
154                val: C,
155            ) -> Self {
156                Self {
157                    ignored_parameters_unsupported: Some(C::into(val).into()),
158                    ..self
159                }
160            }
161        }
162    };
163
164    ($ty:ident, $field:ident) => {
165        impl $crate::response::Response for $ty {
166            fn msg(&self) -> &str {
167                &self.$field.msg()
168            }
169
170            fn set_msg<'a, C: Into<std::borrow::Cow<'a, str>>>(&mut self, msg: C) {
171                self.$field.set_msg(msg);
172            }
173
174            fn with_msg<'a, C: Into<std::borrow::Cow<'a, str>>>(self, msg: C) -> Self {
175                Self {
176                    $field: self.$field.with_msg(msg),
177                    ..self
178                }
179            }
180
181            fn result(&self) -> $crate::response::ApiResult {
182                self.$field.result()
183            }
184
185            fn set_result(&mut self, result: $crate::response::ApiResult) {
186                self.$field.set_result(result);
187            }
188
189            fn with_result(self, result: $crate::response::ApiResult) -> Self {
190                Self {
191                    $field: self.$field.with_result(result),
192                    ..self
193                }
194            }
195
196            fn code(&self) -> Option<$crate::response::ErrorCode> {
197                self.$field.code()
198            }
199
200            fn set_code(&mut self, code: $crate::response::ErrorCode) {
201                self.$field.set_code(code);
202            }
203
204            fn unset_code(&mut self) -> Option<$crate::response::ErrorCode> {
205                self.$field.unset_code()
206            }
207
208            fn with_code(self, code: $crate::response::ErrorCode) -> Self {
209                Self {
210                    $field: self.$field.with_code(code),
211                    ..self
212                }
213            }
214
215            fn ignored_parameters_unsupported(&self) -> Option<&[String]> {
216                self.$field.ignored_parameters_unsupported()
217            }
218
219            fn set_ignored_parameters_unsupported<'a, C: Into<std::borrow::Cow<'a, [String]>>>(
220                &mut self,
221                val: C,
222            ) {
223                self.$field.set_ignored_parameters_unsupported(val);
224            }
225
226            fn unset_ignored_parameters_unsupported(&mut self) -> Option<Vec<String>> {
227                self.$field.unset_ignored_parameters_unsupported()
228            }
229
230            fn with_ignored_parameters_unsupported<'a, C: Into<std::borrow::Cow<'a, [String]>>>(
231                self,
232                val: C,
233            ) -> Self {
234                Self {
235                    $field: self.$field.with_ignored_parameters_unsupported(val),
236                    ..self
237                }
238            }
239        }
240    };
241}