Skip to main content

zulip_api/message/matches_narrow/
response.rs

1use std::collections::HashMap;
2
3use serde::{Deserialize, Serialize};
4
5use crate::response::BaseResponse;
6use crate::{field_access, impl_default, impl_display, impl_response};
7
8/// Represents the response for the `Matches Narrow` Zulip API endpoint.
9///
10/// See [check-messages-match-narrow](https://zulip.com/api/check-messages-match-narrow) for details.
11#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
12pub struct Response {
13    #[serde(flatten)]
14    base: BaseResponse,
15    messages: Option<HashMap<String, MatchNarrow>>,
16}
17
18impl Response {
19    /// Creates a new [Response].
20    pub const fn new() -> Self {
21        Self {
22            base: BaseResponse::new(),
23            messages: None,
24        }
25    }
26}
27
28field_access! {
29    Response {
30        /// A dictionary with a key for each queried message that matches the narrow,
31        /// with message IDs as keys and search rendering data as values.
32        messages: option_ref { HashMap<String, MatchNarrow> },
33    }
34}
35
36impl_default!(Response);
37impl_display!(Response);
38impl_response!(Response, base);
39
40/// Represents the match entry response for the `Matches Narrow` Zulip API endpoint.
41///
42/// See [check-messages-match-narrow](https://zulip.com/api/check-messages-match-narrow) for details.
43#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
44pub struct MatchNarrow {
45    match_content: String,
46    match_subject: String,
47}
48
49impl MatchNarrow {
50    /// Creates a new [MatchNarrow].
51    pub const fn new() -> Self {
52        Self {
53            match_content: String::new(),
54            match_subject: String::new(),
55        }
56    }
57}
58
59field_access! {
60    MatchNarrow {
61        /// HTML content of a queried message that matches the narrow. If the
62        /// narrow is a search narrow, `<span class="highlight">` elements
63        /// will be included, wrapping the matches for the search keywords.
64        match_content: cow { str },
65        /// HTML-escaped topic of a queried message that matches the narrow. If the
66        /// narrow is a search narrow, `<span class="highlight">` elements
67        /// will be included wrapping the matches for the search keywords.
68        match_subject: cow { str },
69    }
70}
71
72impl_default!(MatchNarrow);
73impl_display!(MatchNarrow);
74
75#[cfg(test)]
76mod tests {
77    use super::*;
78
79    #[test]
80    fn test_response() {
81        let msg_id = String::from("31");
82        let match_narrow = MatchNarrow::new()
83            .with_match_content(r#"<p><a href="http://foo.com" target="_blank" title="http://foo.com">http://foo.com</a></p>"#)
84            .with_match_subject("test_topic");
85        let messages = HashMap::from([(msg_id, match_narrow)]);
86
87        let response = Response::new().with_messages(messages.clone());
88        let response_json = response.to_string();
89
90        assert_eq!(serde_json::to_string(&response).unwrap(), response_json);
91        assert_eq!(
92            serde_json::from_str::<Response>(&response_json).unwrap(),
93            response
94        );
95
96        assert_eq!(response.messages(), Some(&messages));
97    }
98}