zulip-api 0.0.8

Client library for interacting with the Zulip API
Documentation
use serde::{Deserialize, Serialize};

use crate::{field_access, BotService, BotType};

/// Represents a Zulip bot.
///
/// 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 Bot {
    #[serde(skip_serializing_if = "Option::is_none")]
    user_id: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    full_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    api_key: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    default_sending_stream: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    default_events_register_stream: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    default_all_public_streams: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    avatar_url: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    owner_id: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    services: Option<Vec<BotService>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    email: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    bot_type: Option<BotType>,
    #[serde(skip_serializing_if = "Option::is_none")]
    is_active: Option<bool>,
}

impl Bot {
    /// Creates a new [Bot].
    pub const fn new() -> Bot {
        Bot {
            user_id: None,
            full_name: None,
            api_key: None,
            default_sending_stream: None,
            default_events_register_stream: None,
            default_all_public_streams: None,
            avatar_url: None,
            owner_id: None,
            services: None,
            email: None,
            bot_type: None,
            is_active: None,
        }
    }
}

field_access! {
    Bot {
        /// The user ID of the bot.
        user_id: option { u64 },
        /// Whether the bot can send messages to all channels by default.
        default_all_public_streams: option { bool },
        /// The user ID of the bot's owner.
        ///
        /// If `null`, the bot has no owner.
        owner_id: option { u64 },
        /// An integer describing the type of bot.
        bot_type: option { BotType },
        /// A boolean describing whether the user account has been deactivated.
        is_active: option { bool },
    }
}

field_access! {
    Bot {
        /// The full name of the bot.
        full_name: option_cow_deref { str, String },
        /// The API key of the bot which it uses to make API requests.
        api_key: option_cow_deref { str, String },
        /// The default sending channel of the bot. If `null`, the bot doesn't have a default sending channel.
        default_sending_stream: option_cow_deref { str, String },
        /// The default channel for which the bot receives events/register data.
        /// If `null`, the bot doesn't have such a default channel.
        default_events_register_stream: option_cow_deref { str, String },
        /// The URL of the bot's avatar.
        avatar_url: option_cow_deref { str, String },
        /// An array containing extra configuration fields only relevant for
        /// outgoing webhook bots and embedded bots. This is always a single-element
        /// array.
        ///
        /// We consider this part of the Zulip API to be unstable; it is used only
        /// for UI elements for administering bots and is likely to change.
        services: option_cow_deref { [BotService], Vec<BotService> },
        /// The email of the bot.
        email: option_cow_deref { str, String },
    }
}

impl Default for Bot {
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Display for Bot {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        serde_json::to_string(self)
            .map_err(|_| std::fmt::Error)
            .and_then(|s| write!(f, "{s}"))
    }
}

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

    #[test]
    fn test_bot() {
        let user_id = 1;
        let full_name = "test_full_name";
        let api_key = "test_api_key";
        let default_sending_stream = "test_default_sending_stream";
        let default_events_register_stream = "test_default_events_register_stream";
        let default_all_public_streams = true;
        let avatar_url = "https://test.avatar.url";
        let owner_id = 2;
        let services = [BotService::new()];
        let email = "test@example.com";
        let bot_type = BotType::new();
        let is_active = true;

        let bot = Bot::new()
            .with_user_id(user_id)
            .with_full_name(full_name)
            .with_api_key(api_key)
            .with_default_sending_stream(default_sending_stream)
            .with_default_events_register_stream(default_events_register_stream)
            .with_default_all_public_streams(default_all_public_streams)
            .with_avatar_url(avatar_url)
            .with_owner_id(owner_id)
            .with_services(services.as_ref())
            .with_email(email)
            .with_bot_type(bot_type)
            .with_is_active(is_active);
        let bot_json = bot.to_string();

        assert_eq!(serde_json::to_string(&bot).unwrap(), bot_json);
        assert_eq!(serde_json::from_str::<Bot>(&bot_json).unwrap(), bot);

        assert_eq!(bot.user_id(), Some(user_id));
        assert_eq!(bot.full_name(), Some(full_name));
        assert_eq!(bot.api_key(), Some(api_key));
        assert_eq!(bot.default_sending_stream(), Some(default_sending_stream));
        assert_eq!(
            bot.default_events_register_stream(),
            Some(default_events_register_stream)
        );
        assert_eq!(
            bot.default_all_public_streams(),
            Some(default_all_public_streams)
        );
        assert_eq!(bot.avatar_url(), Some(avatar_url));
        assert_eq!(bot.owner_id(), Some(owner_id));
        assert_eq!(bot.services(), Some(services.as_ref()));
        assert_eq!(bot.email(), Some(email));
        assert_eq!(bot.bot_type(), Some(bot_type));
        assert_eq!(bot.is_active(), Some(is_active));
    }
}