Skip to main content

zulip_api/message/get_message_history/
response.rs

1use serde::{Deserialize, Serialize};
2
3use crate::response::BaseResponse;
4use crate::{field_access, impl_default, impl_display, impl_response};
5
6mod message_history;
7
8pub use message_history::MessageHistory;
9
10/// Represents the response for the `Get Message History` Zulip API endpoint.
11///
12/// See [get-message-history](https://zulip.com/api/get-message-history) for details.
13#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
14pub struct Response {
15    #[serde(flatten)]
16    base: BaseResponse,
17    #[serde(skip_serializing_if = "Option::is_none")]
18    message_history: Option<Vec<MessageHistory>>,
19}
20
21impl Response {
22    /// Creates a new [Response].
23    pub const fn new() -> Self {
24        Self {
25            base: BaseResponse::new(),
26            message_history: None,
27        }
28    }
29}
30
31field_access! {
32    Response {
33        /// A chronologically sorted, oldest to newest, array
34        /// of `snapshot` objects, each one with the values of
35        /// the message after the edit.
36        message_history: option_cow_deref { [MessageHistory], Vec<MessageHistory> },
37    }
38}
39
40impl_default!(Response);
41impl_display!(Response);
42impl_response!(Response, base);
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn test_response() {
50        let message_history = [MessageHistory::new()];
51
52        let response = Response::new().with_message_history(message_history.as_ref());
53        let response_json = response.to_string();
54
55        assert_eq!(serde_json::to_string(&response).unwrap(), response_json);
56        assert_eq!(
57            serde_json::from_str::<Response>(&response_json).unwrap(),
58            response
59        );
60
61        assert_eq!(response.message_history(), Some(message_history.as_ref()));
62    }
63}