djs-model 0.1.0-alpha.1

Discord API v10 types — users, guilds, channels, messages, interactions and gateway events.
Documentation
//! Layout components: action rows, sections, containers, separators and labels.

use compact_str::CompactString;
use serde::Serialize;

use super::Component;
use crate::macros::int_enum;

/// An [action row](https://docs.discord.com/developers/components/reference#action-row): up to
/// 5 [buttons](super::Button) or a single select menu, at the top level of a message or in a
/// [container](Container).
///
/// In modals, action rows holding text inputs are **deprecated** in favor of [`Label`].
///
/// discord.js counterpart: `ActionRow` / `ActionRowBuilder`.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[non_exhaustive]
pub struct ActionRow {
    /// The component's numeric identifier (generated by Discord when not set).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<i32>,
    /// The buttons or the select menu.
    pub components: Vec<Component>,
}

impl ActionRow {
    /// Creates an action row.
    ///
    /// discord.js counterpart: `ActionRowBuilder` (`@discordjs/builders`).
    #[must_use]
    pub const fn new(components: Vec<Component>) -> Self {
        Self {
            id: None,
            components,
        }
    }
}

/// A [section](https://docs.discord.com/developers/components/reference#section): 1-3
/// [text displays](super::TextDisplay) next to an accessory ([button](super::Button) or
/// [thumbnail](super::Thumbnail)). Messages only.
///
/// discord.js counterpart: `SectionComponent` / `SectionBuilder`.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[non_exhaustive]
pub struct Section {
    /// The component's numeric identifier (generated by Discord when not set).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<i32>,
    /// The text displays (1-3).
    pub components: Vec<Component>,
    /// The button or thumbnail next to the text.
    pub accessory: Box<Component>,
}

impl Section {
    /// Creates a section.
    ///
    /// discord.js counterpart: `SectionBuilder` (`@discordjs/builders`).
    #[must_use]
    pub fn new(components: Vec<Component>, accessory: Component) -> Self {
        Self {
            id: None,
            components,
            accessory: Box::new(accessory),
        }
    }
}

/// A [container](https://docs.discord.com/developers/components/reference#container): a
/// visually grouped box of components with an optional accent color, like an embed. Messages
/// only.
///
/// discord.js counterpart: `ContainerComponent` / `ContainerBuilder`.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[non_exhaustive]
pub struct Container {
    /// The component's numeric identifier (generated by Discord when not set).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<i32>,
    /// The action rows, text displays, sections, media galleries, separators and files inside.
    pub components: Vec<Component>,
    /// The accent color on the left, as an RGB integer.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub accent_color: Option<u32>,
    /// Whether the container is blurred until clicked.
    #[serde(skip_serializing_if = "core::ops::Not::not")]
    pub spoiler: bool,
}

impl Container {
    /// Creates a container without accent color.
    ///
    /// discord.js counterpart: `ContainerBuilder` (`@discordjs/builders`).
    #[must_use]
    pub const fn new(components: Vec<Component>) -> Self {
        Self {
            id: None,
            components,
            accent_color: None,
            spoiler: false,
        }
    }
}

/// A [separator](https://docs.discord.com/developers/components/reference#separator): vertical
/// space between components, with an optional line. Messages only.
///
/// discord.js counterpart: `SeparatorComponent` / `SeparatorBuilder`.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[non_exhaustive]
pub struct Separator {
    /// The component's numeric identifier (generated by Discord when not set).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<i32>,
    /// Whether a line is drawn (default `true`).
    pub divider: bool,
    /// The amount of space (Discord's default: [`Small`](SeparatorSpacing::Small)).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub spacing: Option<SeparatorSpacing>,
}

impl Separator {
    /// Creates a separator with a line and the default spacing.
    ///
    /// discord.js counterpart: `SeparatorBuilder` (`@discordjs/builders`).
    #[must_use]
    pub const fn new() -> Self {
        Self {
            id: None,
            divider: true,
            spacing: None,
        }
    }
}

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

int_enum! {
    /// The [space a separator adds](https://docs.discord.com/developers/components/reference#separator-separator-structure).
    ///
    /// discord.js counterpart: `SeparatorSpacingSize`.
    #[doc(alias = "SeparatorSpacingSize")]
    pub enum SeparatorSpacing: u8 {
        /// Small padding.
        Small = 1,
        /// Large padding.
        Large = 2,
    }
}

/// A [label](https://docs.discord.com/developers/components/reference#label): a title and
/// description for one input in a modal (text input, select menu, file upload, radio group,
/// checkbox group or checkbox).
///
/// discord.js counterpart: `LabelComponent` / `LabelBuilder`.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[non_exhaustive]
pub struct Label {
    /// The component's numeric identifier (generated by Discord when not set).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<i32>,
    /// The label text (max 45 characters).
    pub label: CompactString,
    /// An additional description (max 100 characters).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<CompactString>,
    /// The labelled input.
    pub component: Box<Component>,
}

impl Label {
    /// Creates a label for `component`.
    ///
    /// discord.js counterpart: `LabelBuilder` (`@discordjs/builders`).
    #[must_use]
    pub fn new(label: impl Into<CompactString>, component: Component) -> Self {
        Self {
            id: None,
            label: label.into(),
            description: None,
            component: Box::new(component),
        }
    }
}