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 a group setting value.
///
/// Modified from `openapi-generator-cli` output:
///
/// ```shell,no_build,no_run
/// $ openapi-generator-cli generate -g rust -i ./zerver/openapi/zulip.yaml -o zulip-openapi --skip-validate-spec
/// ```
///
/// ```text
/// /*
/// * Zulip REST API
/// *
/// * Powerful open source group chat
/// *
/// * The version of the OpenAPI document: 1.0.0
/// *
/// * Generated by: https://openapi-generator.tech
/// */
/// ```
///
/// See [zerver/openapi/zulip.yaml](https://github.com/zulip/zulip/blob/main/zerver/openapi/zulip.yaml) for the original OpenAPI configuration.
///
/// Configuration slightly modified to work with the Rust generator of [`openapi-generator-cli`](https://openapi-generator.tech).
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GroupSetting {
    /// The ID of the [user group](/help/user-groups) with this permission.
    GroupId(u64),
    GroupIds(GroupSettingIds),
}

impl GroupSetting {
    /// Creates a new [GroupSetting].
    pub const fn new() -> Self {
        Self::GroupId(0)
    }
}

impl_default!(GroupSetting);
impl_display!(GroupSetting);

/// GroupSettingValueOneOf : An object with these fields:
///
/// Modified from `openapi-generator-cli` output:
///
/// ```shell,no_build,no_run
/// $ openapi-generator-cli generate -g rust -i ./zerver/openapi/zulip.yaml -o zulip-openapi --skip-validate-spec
/// ```
///
/// ```text
/// /*
/// * Zulip REST API
/// *
/// * Powerful open source group chat
/// *
/// * The version of the OpenAPI document: 1.0.0
/// *
/// * Generated by: https://openapi-generator.tech
/// */
/// ```
///
/// See [zerver/openapi/zulip.yaml](https://github.com/zulip/zulip/blob/main/zerver/openapi/zulip.yaml) for the original OpenAPI configuration.
///
/// Configuration slightly modified to work with the Rust generator of [`openapi-generator-cli`](https://openapi-generator.tech).
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct GroupSettingIds {
    #[serde(skip_serializing_if = "Option::is_none")]
    direct_members: Option<Vec<u64>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    direct_subgroups: Option<Vec<u64>>,
}

impl GroupSettingIds {
    /// Creates a new [GroupSettingIds].
    pub const fn new() -> Self {
        Self {
            direct_members: None,
            direct_subgroups: None,
        }
    }
}

field_access! {
    GroupSettingIds {
        /// The list of IDs of individual users in the collection of users with this permission.
        ///
        /// **Changes**: Prior to Zulip 10.0 (feature level 303), this list would include deactivated users who had the permission before being deactivated.
        direct_members: option_cow_deref { [u64], Vec<u64> },
        /// The list of IDs of the groups in the collection of users with this permission.
        direct_subgroups: option_cow_deref { [u64], Vec<u64> },
    }
}

impl_default!(GroupSettingIds);
impl_display!(GroupSettingIds);

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

    #[test]
    fn test_group_setting_ids() {
        let direct_members = [1u64];
        let direct_subgroups = [2u64];

        let ids = GroupSettingIds::new()
            .with_direct_members(direct_members.as_ref())
            .with_direct_subgroups(direct_subgroups.as_ref());
        let ids_json = ids.to_string();

        assert_eq!(serde_json::to_string(&ids).unwrap(), ids_json);
        assert_eq!(
            serde_json::from_str::<GroupSettingIds>(&ids_json).unwrap(),
            ids
        );

        assert_eq!(ids.direct_members(), Some(direct_members.as_ref()));
        assert_eq!(ids.direct_subgroups(), Some(direct_subgroups.as_ref()));
    }
}