zulip_api/message/send_message/
response.rs1use serde::{Deserialize, Serialize};
2
3use crate::response::BaseResponse;
4use crate::{field_access, impl_default, impl_display, impl_response, VisibilityPolicy};
5
6#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
8pub struct Response {
9 #[serde(flatten)]
10 base: BaseResponse,
11 #[serde(skip_serializing_if = "Option::is_none")]
12 id: Option<u64>,
13 #[serde(skip_serializing_if = "Option::is_none")]
14 automatic_new_visibility_policy: Option<VisibilityPolicy>,
15}
16
17impl_response!(Response, base);
18
19field_access! {
20 Response {
21 id: option { u64 },
23 automatic_new_visibility_policy: option { VisibilityPolicy },
46 }
47}
48
49impl Response {
50 pub const fn new() -> Self {
52 Self {
53 base: BaseResponse::new(),
54 id: None,
55 automatic_new_visibility_policy: None,
56 }
57 }
58}
59
60impl_default!(Response);
61impl_display!(Response);
62
63#[cfg(test)]
64mod tests {
65 use super::*;
66 use crate::response::{ApiResult, ErrorCode, Response as _};
67
68 #[test]
69 fn test_success_message_response() {
70 let msg = "test response";
71 let msg_json = serde_json::to_string(&msg).unwrap();
72
73 let result = ApiResult::Success;
74 let result_json = serde_json::to_string(&result).unwrap();
75
76 let id = 42;
77 let id_json = serde_json::to_string(&id).unwrap();
78
79 let response = Response::new()
80 .with_msg(msg)
81 .with_result(result)
82 .with_id(id);
83
84 let response_json =
85 format!(r#"{{"msg":{msg_json},"result":{result_json},"id":{id_json}}}"#);
86
87 assert_eq!(serde_json::to_string(&response).unwrap(), response_json);
88 assert_eq!(
89 serde_json::from_str::<Response>(&response_json).unwrap(),
90 response
91 );
92
93 assert_eq!(response.msg(), msg);
94 assert_eq!(response.result(), result);
95 assert_eq!(response.code(), None);
96 assert_eq!(response.id(), Some(id));
97 }
98
99 #[test]
100 fn test_error_message_response() {
101 let msg = "test response";
102 let msg_json = serde_json::to_string(&msg).unwrap();
103
104 let result = ApiResult::Error;
105 let result_json = serde_json::to_string(&result).unwrap();
106
107 let code = ErrorCode::BadRequest;
108 let code_json = serde_json::to_string(&code).unwrap();
109
110 let id = 42;
111 let id_json = serde_json::to_string(&id).unwrap();
112
113 let response = Response::new()
114 .with_msg(msg)
115 .with_result(result)
116 .with_code(code)
117 .with_id(id);
118
119 let response_json = format!(
120 r#"{{"msg":{msg_json},"result":{result_json},"code":{code_json},"id":{id_json}}}"#
121 );
122
123 assert_eq!(serde_json::to_string(&response).unwrap(), response_json);
124 assert_eq!(
125 serde_json::from_str::<Response>(&response_json).unwrap(),
126 response
127 );
128
129 assert_eq!(response.msg(), msg);
130 assert_eq!(response.result(), result);
131 assert_eq!(response.code(), Some(code));
132 assert_eq!(response.id(), Some(id));
133 }
134}