zulip-api 0.0.8

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

use crate::response::BaseResponse;
use crate::{field_access, impl_default, impl_display, impl_response, Message};

/// Represents the response for the `Get Message` Zulip API endpoint.
///
/// See [get-message](https://zulip.com/api/get-message) for details.
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct Response {
    #[serde(flatten)]
    base: BaseResponse,
    #[serde(skip_serializing_if = "Option::is_none")]
    raw_content: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    message: Option<Message>,
}

impl Response {
    /// Creates a new [Response].
    pub const fn new() -> Self {
        Self {
            base: BaseResponse::new(),
            raw_content: None,
            message: None,
        }
    }
}

field_access! {
    Response {
        /// The raw Markdown content of the message.
        ///
        /// See the help center article on [message formatting](https://zulip.com/help/format-your-message-using-markdown) for details on Zulip-flavored Markdown.
        ///
        /// **Deprecated** and to be removed once no longer required for
        /// legacy clients. Modern clients should prefer passing
        /// `"apply_markdown": false` to request raw message content.
        raw_content: option_cow_deref { str, String },
    }
}

field_access! {
    Response {
        /// Object containing details of the message.
        message: option_cow_ref { Message },
    }
}

impl_default!(Response);
impl_display!(Response);
impl_response!(Response, base);

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

    #[test]
    fn test_response() {
        let raw_content = "test raw cnntent";
        let message = Message::new();

        let response = Response::new()
            .with_raw_content(raw_content)
            .with_message(&message);
        let response_json = response.to_string();

        assert_eq!(serde_json::to_string(&response).unwrap(), response_json);
        assert_eq!(
            serde_json::from_str::<Response>(&response_json).unwrap(),
            response
        );

        assert_eq!(response.raw_content(), Some(raw_content));
        assert_eq!(response.message(), Some(&message));
    }
}