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

mod message_history;

pub use message_history::MessageHistory;

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

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

field_access! {
    Response {
        /// A chronologically sorted, oldest to newest, array
        /// of `snapshot` objects, each one with the values of
        /// the message after the edit.
        message_history: option_cow_deref { [MessageHistory], Vec<MessageHistory> },
    }
}

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

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

    #[test]
    fn test_response() {
        let message_history = [MessageHistory::new()];

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