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, Flag, Operation};

/// Represents the request for the `Update Message Flags for Narrow` Zulip API endpoint.
///
/// See [update-message-flags-for-narrow](https://zulip.com/api/update-message-flags-for-narrow) for details.
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct Request {
    anchor: Anchor,
    include_anchor: Option<bool>,
    num_before: u64,
    num_after: u64,
    narrow: Vec<Narrow>,
    op: Operation,
    flag: Flag,
}

impl Request {
    /// Creates a new [Request].
    pub const fn new() -> Self {
        Self {
            anchor: Anchor::new(),
            include_anchor: None,
            num_before: 0,
            num_after: 0,
            narrow: Vec::new(),
            op: Operation::new(),
            flag: Flag::new(),
        }
    }
}

field_access! {
    Request {
        /// Represents an anchor updating of flags.
        ///
        /// Either an integer message ID, or a special string values for when
        /// the client wants the server to compute the anchor to use.
        anchor: Anchor,
        /// Limit the number of messages preceding the anchor in the update range.
        ///
        /// The server may decrease this to bound transaction sizes.
        num_before: u64,
        /// Limit the number of messages following the anchor in the update range.
        ///
        /// The server may decrease this to bound transaction sizes.
        num_after: u64,
        /// Whether to `add` the flag or `remove` it.
        op: Operation,
        /// The flag that should be added/removed.
        ///
        /// See [available flags](https://zulip.com/api/update-message-flags#available-flags).
        flag: Flag,
    }
}

field_access! {
    Request {
        /// Whether a message with the specified ID matching the narrow
        /// should be included in the update range.
        include_anchor: option { bool },
    }
}

field_access! {
    Request {
        /// The narrow you want update flags within. See how to
        /// [construct a narrow](https://zulip.com/api/construct-narrow).
        ///
        /// Note that, when adding the `read` flag to messages, clients should
        /// consider including a narrow with the `is:unread` filter as an
        /// optimization. Including that filter takes advantage of the fact that
        /// the server has a database index for unread messages.
        ///
        /// **Changes**: See [changes section](https://zulip.com/api/construct-narrow#changes)
        /// of search/narrow filter documentation.
        narrow: cow { [Narrow] },
    }
}

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

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

    #[test]
    fn test_request() {
        let anchor = Anchor::new();
        let include_anchor = true;
        let num_before = 1;
        let num_after = 2;
        let narrow = [Narrow::new()];
        let op = Operation::new();
        let flag = Flag::new();

        let request = Request::new()
            .with_anchor(anchor)
            .with_include_anchor(include_anchor)
            .with_num_before(num_before)
            .with_num_after(num_after)
            .with_narrow(narrow.as_ref())
            .with_op(op)
            .with_flag(flag);
        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.anchor(), anchor);
        assert_eq!(request.include_anchor(), Some(include_anchor));
        assert_eq!(request.num_before(), num_before);
        assert_eq!(request.num_after(), num_after);
        assert_eq!(request.narrow(), narrow.as_ref());
        assert_eq!(request.op(), op);
        assert_eq!(request.flag(), flag);
    }
}