zulip-api 0.0.8

Client library for interacting with the Zulip API
Documentation
use serde::{Deserialize, Serialize};

use crate::field_access;

/// Represents the request to the `Render` Zulip API.
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct Request {
    content: String,
}

impl Request {
    /// Creates a new [Request].
    pub const fn new() -> Self {
        Self {
            content: String::new(),
        }
    }
}

field_access! {
    Request {
        /// The content of the message.
        ///
        /// Clients should use the `max_message_length` returned by the
        /// [`POST /register`](https://zulip.com/api/register-queue) endpoint to determine
        /// the maximum message size.
        content: cow { str },
    }
}

impl Default for Request {
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Display for Request {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        serde_json::to_string(self)
            .map_err(|_| std::fmt::Error)
            .and_then(|s| write!(f, "{s}"))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_request() {
        let req = Request::new().with_content("test message");
        let req_json = req.to_string();

        assert_eq!(serde_json::to_string(&req).unwrap(), req_json);
        assert_eq!(serde_json::from_str::<Request>(&req_json).unwrap(), req);
    }
}