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 response for the `Mark Topic as Read` Zulip API endpoint.
///
/// See [mark-topic-as-read](https://zulip.com/api/mark-topic-as-read) for details.
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct Request {
    stream_id: u64,
    topic_name: String,
}

impl Request {
    /// Creates a new [Request].
    pub const fn new() -> Self {
        Self {
            stream_id: 0,
            topic_name: String::new(),
        }
    }
}

field_access! {
    Request {
        /// The ID of the channel to access.
        stream_id: u64,
    }
}

field_access! {
    Request {
        /// The name of the topic whose messages should be marked as read.
        ///
        /// Note: When the value of `realm_empty_topic_display_name` found in
        /// the [POST /register](https://zulip.com/api/register-queue) response is used for this
        /// parameter, it is interpreted as an empty string.
        ///
        /// **Changes**: Before Zulip 10.0 (feature level 334), empty string
        /// was not a valid topic name for channel messages.
        topic_name: cow { str },
    }
}

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

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

    #[test]
    fn test_request() {
        let stream_id = 1;
        let topic_name = "test topic";

        let request = Request::new()
            .with_stream_id(stream_id)
            .with_topic_name(topic_name);
        let request_json = request.to_string();

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

        assert_eq!(request.stream_id(), stream_id);
        assert_eq!(request.topic_name(), topic_name);
    }
}