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 `Get Read Receipts` Zulip API endpoint.
///
/// See [get-read-receipts](https://zulip.com/api/get-read-receipts) for details.
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct Response {
    #[serde(flatten)]
    base: BaseResponse,
    #[serde(skip_serializing_if = "Option::is_none")]
    user_ids: Option<Vec<u64>>,
}

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

field_access! {
    Response {
        /// An array of IDs of users who have marked the target message as
        /// read and whose read status is available to the current user.
        ///
        /// The IDs of users who have disabled sending read receipts
        /// (`"send_read_receipts": false`) will never appear in the response,
        /// nor will the message's sender. Additionally, the IDs of any users
        /// who have been muted by the current user or who have muted the
        /// current user will not be included in the response.
        ///
        /// The current user's ID will appear if they have marked the target
        /// message as read.
        ///
        /// **Changes**: Prior to Zulip 6.0 (feature level 143), the IDs of
        /// users who have been muted by or have muted the current user were
        /// included in the response.
        user_ids: 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 user_ids = [1];

        let response = Response::new().with_user_ids(user_ids.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.user_ids(), Some(user_ids.as_ref()));
    }
}