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};

/// Represents the response for the `Update Message Flags` Zulip API endpoint.
///
/// See [update-message-flags](https://zulip.com/api/update-message-flags) for details.
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct Response {
    #[serde(flatten)]
    base: BaseResponse,
    messages: Option<Vec<u64>>,
    ignored_because_not_subscribed_channels: Option<Vec<u64>>,
}

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

field_access! {
    Response {
        /// An array with the IDs of the modified messages.
        messages: option_cow_deref { [u64], Vec<u64> },
        /// Only present if the flag is `read` and the operation is `remove`.
        ///
        /// Zulip has an invariant that all unread messages must be in channels
        /// the user is subscribed to. This field will contain a list of the
        /// channels whose messages were skipped to mark as unread because the
        /// user is not subscribed to them.
        ///
        /// **Changes**: New in Zulip 10.0 (feature level 355).
        ignored_because_not_subscribed_channels: option_cow_deref { [u64], Vec<u64> },
    }
}

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

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

    #[test]
    fn test_response() {
        let messages = [1u64];
        let ignored_because_not_subscribed_channels = [2u64];

        let response = Response::new()
            .with_messages(messages.as_ref())
            .with_ignored_because_not_subscribed_channels(
                ignored_because_not_subscribed_channels.as_ref(),
            );
        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.messages(), Some(messages.as_ref()));
        assert_eq!(
            response.ignored_because_not_subscribed_channels(),
            Some(ignored_because_not_subscribed_channels.as_ref())
        );
    }
}