zulip-api 0.0.8

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

use crate::filter::{Anchor, Narrow};
use crate::{field_access, impl_default, impl_display};

/// Represents the request for the `Get Messages` Zulip API endpoint.
///
/// See [get-messages](https://zulip.com/api/get-messages) for details.
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct Request {
    #[serde(skip_serializing_if = "Option::is_none")]
    anchor: Option<Anchor>,
    #[serde(skip_serializing_if = "Option::is_none")]
    include_anchor: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    num_before: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    num_after: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    narrow: Option<Narrow>,
    #[serde(skip_serializing_if = "Option::is_none")]
    client_gravatar: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    apply_markdown: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    message_ids: Option<Vec<u64>>,
    #[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 {
            anchor: None,
            include_anchor: None,
            num_before: None,
            num_after: None,
            narrow: None,
            client_gravatar: None,
            apply_markdown: None,
            message_ids: None,
            allow_empty_topic_name: None,
        }
    }
}

field_access! {
    Request {
        anchor: option { Anchor },
        include_anchor: option { bool },
        num_before: option { u64 },
        num_after: option { u64 },
        client_gravatar: option { bool },
        apply_markdown: option { bool },
        allow_empty_topic_name: option { bool },
    }
}

field_access! {
    Request {
        narrow: option_ref { &Narrow, Narrow },
    }
}

field_access! {
    Request {
        message_ids: option_cow_deref { [u64], Vec<u64> },
    }
}

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

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

    #[test]
    fn test_request_minimal() {
        let req = Request::new();
        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);
    }

    #[test]
    fn test_request_full() {
        let anchor = Anchor::Newest;
        let include_anchor = true;
        let num_before = 1;
        let num_after = 2;
        let narrow = Narrow::new();
        let client_gravatar = true;
        let apply_markdown = true;
        let message_ids = vec![3u64];
        let allow_empty_topic_name = true;

        let req = Request::new()
            .with_anchor(anchor)
            .with_include_anchor(include_anchor)
            .with_num_before(num_before)
            .with_num_after(num_after)
            .with_narrow(narrow.clone())
            .with_client_gravatar(client_gravatar)
            .with_apply_markdown(apply_markdown)
            .with_message_ids(message_ids.clone())
            .with_allow_empty_topic_name(allow_empty_topic_name);

        let req_json = req.to_string();

        assert_eq!(req.anchor(), Some(anchor));
        assert_eq!(req.include_anchor(), Some(include_anchor));
        assert_eq!(req.num_before(), Some(num_before));
        assert_eq!(req.num_after(), Some(num_after));
        assert_eq!(req.narrow(), Some(&narrow));
        assert_eq!(req.client_gravatar(), Some(client_gravatar));
        assert_eq!(req.apply_markdown(), Some(apply_markdown));
        assert_eq!(req.message_ids(), Some(message_ids.as_ref()));
        assert_eq!(req.allow_empty_topic_name(), Some(allow_empty_topic_name));

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