zulip_api/message/matches_narrow/
response.rs1use 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#[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 pub const fn new() -> Self {
21 Self {
22 base: BaseResponse::new(),
23 messages: None,
24 }
25 }
26}
27
28field_access! {
29 Response {
30 messages: option_ref { HashMap<String, MatchNarrow> },
33 }
34}
35
36impl_default!(Response);
37impl_display!(Response);
38impl_response!(Response, base);
39
40#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
44pub struct MatchNarrow {
45 match_content: String,
46 match_subject: String,
47}
48
49impl MatchNarrow {
50 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 match_content: cow { str },
65 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}