Skip to main content

zulip_api/message/get_message/
response.rs

1use serde::{Deserialize, Serialize};
2
3use crate::response::BaseResponse;
4use crate::{field_access, impl_default, impl_display, impl_response, Message};
5
6/// Represents the response for the `Get Message` Zulip API endpoint.
7///
8/// See [get-message](https://zulip.com/api/get-message) for details.
9#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
10pub struct Response {
11    #[serde(flatten)]
12    base: BaseResponse,
13    #[serde(skip_serializing_if = "Option::is_none")]
14    raw_content: Option<String>,
15    #[serde(skip_serializing_if = "Option::is_none")]
16    message: Option<Message>,
17}
18
19impl Response {
20    /// Creates a new [Response].
21    pub const fn new() -> Self {
22        Self {
23            base: BaseResponse::new(),
24            raw_content: None,
25            message: None,
26        }
27    }
28}
29
30field_access! {
31    Response {
32        /// The raw Markdown content of the message.
33        ///
34        /// See the help center article on [message formatting](https://zulip.com/help/format-your-message-using-markdown) for details on Zulip-flavored Markdown.
35        ///
36        /// **Deprecated** and to be removed once no longer required for
37        /// legacy clients. Modern clients should prefer passing
38        /// `"apply_markdown": false` to request raw message content.
39        raw_content: option_cow_deref { str, String },
40    }
41}
42
43field_access! {
44    Response {
45        /// Object containing details of the message.
46        message: option_cow_ref { Message },
47    }
48}
49
50impl_default!(Response);
51impl_display!(Response);
52impl_response!(Response, base);
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_response() {
60        let raw_content = "test raw cnntent";
61        let message = Message::new();
62
63        let response = Response::new()
64            .with_raw_content(raw_content)
65            .with_message(&message);
66        let response_json = response.to_string();
67
68        assert_eq!(serde_json::to_string(&response).unwrap(), response_json);
69        assert_eq!(
70            serde_json::from_str::<Response>(&response_json).unwrap(),
71            response
72        );
73
74        assert_eq!(response.raw_content(), Some(raw_content));
75        assert_eq!(response.message(), Some(&message));
76    }
77}