zulip-api 0.0.8

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

use crate::{field_access, impl_default, impl_display};

/// Represents the request 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 Request {
    #[serde(skip)]
    message_id: u64,
    #[serde(skip_serializing_if = "Option::is_none")]
    apply_markdown: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    allow_empty_topic_name: Option<bool>,
}

impl Request {
    /// Creates a new [Request].
    pub const fn new() -> Self {
        Self {
            message_id: 0,
            apply_markdown: None,
            allow_empty_topic_name: None,
        }
    }
}

field_access! {
    Request {
        /// The target message's ID.
        message_id: u64,
    }
}

field_access! {
    Request {
        /// If `true`, message content is returned in the rendered HTML
        /// format. If `false`, message content is returned in the raw
        /// [Zulip-flavored Markdown format](https://zulip.com/help/format-your-message-using-markdown) text that user entered.
        ///
        /// **Changes**: New in Zulip 5.0 (feature level 120).
        apply_markdown: option { bool },
        /// Whether the client supports processing the empty string as a topic in the
        /// topic name fields in the returned data, including in returned edit_history data.
        ///
        /// If `false`, the server will use the value of `realm_empty_topic_display_name`
        /// found in the [`POST /register`](https://zulip.com/api/register-queue) response instead of empty string
        /// to represent the empty string topic in its response.
        ///
        /// **Changes**: New in Zulip 10.0 (feature level 334). Previously, the empty string
        /// was not a valid topic.
        allow_empty_topic_name: option { bool },
    }
}

impl_default!(Request);
impl_display!(Request);

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

    #[test]
    fn test_request() {
        let message_id = 1;
        let apply_markdown = true;
        let allow_empty_topic_name = false;

        let mut request = Request::new()
            .with_message_id(message_id)
            .with_apply_markdown(apply_markdown)
            .with_allow_empty_topic_name(allow_empty_topic_name);
        let request_json = request.to_string();

        assert_eq!(request.message_id(), message_id);
        assert_eq!(request.apply_markdown(), Some(apply_markdown));
        assert_eq!(
            request.allow_empty_topic_name(),
            Some(allow_empty_topic_name)
        );

        assert_eq!(serde_json::to_string(&request).unwrap(), request_json);
        request.set_message_id(0);
        assert_eq!(
            serde_json::from_str::<Request>(&request_json).unwrap(),
            request
        );
    }
}