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 configuration for a group permission setting specifying the groups to which the setting can be set to and the default values for the setting.
///
/// **Changes**: Removed `allow_owners_group` field in Zulip 10.0 (feature level 326), as we now support anonymous user groups.
/// Previously, the `role:owners` system group was not offered when `allow_owners_group` was false.
///
/// Removed unnecessary `id_field_name` field in Zulip 10.0 (feature level 326).
/// Previously, this always had the value of `\"{setting_name}_id\"`;
/// it was an internal implementation detail of the server not intended to be included in the API.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct GroupPermissionSetting {
    #[serde(skip_serializing_if = "Option::is_none")]
    require_system_group: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    allow_internet_group: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    allow_nobody_group: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    allow_everyone_group: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    default_group_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    default_for_system_groups: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    allowed_system_groups: Option<Vec<String>>,
}

impl GroupPermissionSetting {
    /// Creates a new [GroupPermissionSetting].
    pub const fn new() -> Self {
        Self {
            require_system_group: None,
            allow_internet_group: None,
            allow_nobody_group: None,
            allow_everyone_group: None,
            default_group_name: None,
            default_for_system_groups: None,
            allowed_system_groups: None,
        }
    }
}

field_access! {
    GroupPermissionSetting {
        /// Whether the setting can only be set to a system user group.
        require_system_group: option { bool },
        /// Whether the setting can be set to `role:internet` system group.
        allow_internet_group: option { bool },
        /// Whether the setting can be set to `role:nobody` system group.
        allow_nobody_group: option { bool },
        /// Whether the setting can be set to `role:everyone` system group.
        ///
        /// If false, guest users cannot exercise this permission even if they are part of the [group-setting value](https://zulip.com/api/group-setting-values) for this setting.
        allow_everyone_group: option { bool },
    }
}

field_access! {
    GroupPermissionSetting {
        /// Name of the default system group for the setting.
        ///
        /// For some channel settings, this can also be `channel_creator`.
        ///
        /// In that case:
        ///
        /// - If the channel's [`creator_id`](https://zulip.com/api/get-stream-by-id#response) is not `null`, default for the setting is an anonymous group with the channel creator as the only member.
        /// - If the channel's [`creator_id`](https://zulip.com/api/get-stream-by-id#response) is `null`, default for the setting is `role:nobody` system group.
        ///
        /// **Changes**: In Zulip 12.0 (feature level 427), renamed `stream_creator_or_nobody` value to `channel_creator`.
        default_group_name: option_cow_deref { str, String },
        /// Name of the default group for the setting for system groups.  This is non-null only for group-level settings.
        default_for_system_groups: option_cow_deref { str, String },
        /// An array of names of system groups to which the setting can be set to.
        ///
        /// If the list is empty, the setting can be set to system groups based on the other boolean fields.
        ///
        /// **Changes**: New in Zulip 8.0 (feature level 225).
        allowed_system_groups: option_cow_deref { [String], Vec<String> },
    }
}

impl_default!(GroupPermissionSetting);
impl_display!(GroupPermissionSetting);

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

    #[test]
    fn test_group_setting() {
        let require_system_group = true;
        let allow_internet_group = false;
        let allow_nobody_group = true;
        let allow_everyone_group = false;
        let default_group_name = "test default group name";
        let default_for_system_groups = "test default system groups";
        let allowed_system_groups = ["test allowed system group".to_string()];

        let setting = GroupPermissionSetting::new()
            .with_require_system_group(require_system_group)
            .with_allow_internet_group(allow_internet_group)
            .with_allow_nobody_group(allow_nobody_group)
            .with_allow_everyone_group(allow_everyone_group)
            .with_default_group_name(default_group_name)
            .with_default_for_system_groups(default_for_system_groups)
            .with_allowed_system_groups(allowed_system_groups.as_ref());
        let setting_json = setting.to_string();

        assert_eq!(serde_json::to_string(&setting).unwrap(), setting_json);
        assert_eq!(
            serde_json::from_str::<GroupPermissionSetting>(&setting_json).unwrap(),
            setting
        );

        assert_eq!(setting.require_system_group(), Some(require_system_group));
        assert_eq!(setting.allow_internet_group(), Some(allow_internet_group));
        assert_eq!(setting.allow_nobody_group(), Some(allow_nobody_group));
        assert_eq!(setting.allow_everyone_group(), Some(allow_everyone_group));
        assert_eq!(setting.default_group_name(), Some(default_group_name));
        assert_eq!(
            setting.default_for_system_groups(),
            Some(default_for_system_groups)
        );
        assert_eq!(
            setting.allowed_system_groups(),
            Some(allowed_system_groups.as_ref())
        );
    }
}