Skip to main content

zulip_api/message/render/
response.rs

1use serde::{Deserialize, Serialize};
2
3use crate::response::BaseResponse;
4use crate::{field_access, impl_response};
5
6/// Represents the request to the `Render` Zulip API.
7#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
8pub struct Response {
9    #[serde(flatten)]
10    base: BaseResponse,
11    rendered: Option<String>,
12}
13
14impl Response {
15    /// Creates a new [Self].
16    pub const fn new() -> Self {
17        Self {
18            base: BaseResponse::new(),
19            rendered: None,
20        }
21    }
22}
23
24impl Default for Response {
25    fn default() -> Self {
26        Self::new()
27    }
28}
29
30impl_response!(Response, base);
31
32field_access! {
33    Response {
34        /// The rendered HTML.
35        rendered: option_cow_deref { str, String },
36    }
37}
38
39impl std::fmt::Display for Response {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        serde_json::to_string(self)
42            .map_err(|_| std::fmt::Error)
43            .and_then(|s| write!(f, "{s}"))
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn test_response() {
53        let response = Response::new().with_rendered("<p>test message</p>");
54        let response_json = response.to_string();
55
56        assert_eq!(serde_json::to_string(&response).unwrap(), response_json);
57        assert_eq!(
58            serde_json::from_str::<Response>(&response_json).unwrap(),
59            response
60        );
61    }
62}