Skip to main content

zulip_api/message/get_messages/
response.rs

1use serde::{Deserialize, Serialize};
2
3use crate::filter::Anchor;
4use crate::message::Message;
5use crate::response::BaseResponse;
6use crate::{field_access, impl_default, impl_display, impl_response};
7
8/// Represents the response for the `Get Messages` Zulip API endpoint.
9///
10/// See [get-messages](https://zulip.com/api/get-messages) for details.
11#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
12pub struct Response {
13    #[serde(flatten)]
14    base: BaseResponse,
15    #[serde(skip_serializing_if = "Option::is_none")]
16    anchor: Option<Anchor>,
17    #[serde(skip_serializing_if = "Option::is_none")]
18    found_newest: Option<bool>,
19    #[serde(skip_serializing_if = "Option::is_none")]
20    found_oldest: Option<bool>,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    found_anchor: Option<bool>,
23    #[serde(skip_serializing_if = "Option::is_none")]
24    history_limited: Option<bool>,
25    messages: Vec<Message>,
26}
27
28impl Response {
29    /// Creates a new [Response].
30    pub const fn new() -> Self {
31        Self {
32            base: BaseResponse::new(),
33            anchor: None,
34            found_newest: None,
35            found_oldest: None,
36            found_anchor: None,
37            history_limited: None,
38            messages: Vec::new(),
39        }
40    }
41}
42
43field_access! {
44    Response {
45        anchor: option { Anchor },
46        found_newest: option { bool },
47        found_oldest: option { bool },
48        found_anchor: option { bool },
49        history_limited: option { bool },
50    }
51}
52
53field_access! {
54    Response {
55        messages: cow { [Message] },
56    }
57}
58
59impl_default!(Response);
60impl_display!(Response);
61impl_response!(Response, base);
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    #[test]
68    fn test_response_minimal() {
69        let response = Response::new();
70        let response_json = response.to_string();
71
72        assert_eq!(serde_json::to_string(&response).unwrap(), response_json);
73        assert_eq!(
74            serde_json::from_str::<Response>(&response_json).unwrap(),
75            response
76        );
77    }
78
79    #[test]
80    fn test_response_full() {
81        let anchor = Anchor::Newest;
82        let found_newest = true;
83        let found_oldest = false;
84        let found_anchor = true;
85        let messages = [Message::new().with_content("test content").with_id(1)];
86
87        let response = Response::new()
88            .with_anchor(anchor)
89            .with_found_newest(found_newest)
90            .with_found_oldest(found_oldest)
91            .with_found_anchor(found_anchor)
92            .with_messages(&messages);
93
94        let response_json = response.to_string();
95
96        assert_eq!(response.anchor(), Some(anchor));
97        assert_eq!(response.found_newest(), Some(found_newest));
98        assert_eq!(response.found_oldest(), Some(found_oldest));
99        assert_eq!(response.found_anchor(), Some(found_anchor));
100        assert_eq!(response.messages(), messages.as_ref());
101
102        assert_eq!(serde_json::to_string(&response).unwrap(), response_json);
103        assert_eq!(
104            serde_json::from_str::<Response>(&response_json).unwrap(),
105            response
106        );
107    }
108}