Skip to main content

zulip_api/message/render/
request.rs

1use serde::{Deserialize, Serialize};
2
3use crate::field_access;
4
5/// Represents the request to the `Render` Zulip API.
6#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
7pub struct Request {
8    content: String,
9}
10
11impl Request {
12    /// Creates a new [Request].
13    pub const fn new() -> Self {
14        Self {
15            content: String::new(),
16        }
17    }
18}
19
20field_access! {
21    Request {
22        /// The content of the message.
23        ///
24        /// Clients should use the `max_message_length` returned by the
25        /// [`POST /register`](https://zulip.com/api/register-queue) endpoint to determine
26        /// the maximum message size.
27        content: cow { str },
28    }
29}
30
31impl Default for Request {
32    fn default() -> Self {
33        Self::new()
34    }
35}
36
37impl std::fmt::Display for Request {
38    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39        serde_json::to_string(self)
40            .map_err(|_| std::fmt::Error)
41            .and_then(|s| write!(f, "{s}"))
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn test_request() {
51        let req = Request::new().with_content("test message");
52        let req_json = req.to_string();
53
54        assert_eq!(serde_json::to_string(&req).unwrap(), req_json);
55        assert_eq!(serde_json::from_str::<Request>(&req_json).unwrap(), req);
56    }
57}