Skip to main content

zaun_community_protocol/command/object/
component.rs

1//! Message and modal components (specification §8.14), in Discord's shapes and numeric
2//! `type` values.
3//!
4//! On the wire a component is a JSON object whose `type` number selects its kind, as on
5//! Discord. Pictures and files name attachments of the same message, never URLs, so no
6//! client fetches anything remote to show a component.
7
8mod check;
9mod find;
10mod kinds;
11mod serde_impl;
12
13pub use check::{check_message_components, check_modal, component_attachments};
14pub use find::{find_interactive, modal_inputs};
15pub use kinds::{
16    ActionRow, Button, ButtonStyle, Checkbox, CheckboxGroup, Container, DefaultValue,
17    DefaultValueKind, EntitySelect, FileComponent, FileUpload, Label, MediaGallery, MediaItem,
18    RadioGroup, Section, SelectOption, Separator, StringSelect, TextDisplay, TextInput,
19    TextInputStyle, Thumbnail,
20};
21
22/// One component (§8.14). The variant is the wire `type` number.
23#[derive(Debug, Clone, PartialEq, Eq)]
24pub enum Component {
25    /// 1: a row of up to five buttons or one select.
26    ActionRow(ActionRow),
27    /// 2.
28    Button(Button),
29    /// 3.
30    StringSelect(StringSelect),
31    /// 4: modal only, inside a label.
32    TextInput(TextInput),
33    /// 5.
34    UserSelect(EntitySelect),
35    /// 6.
36    RoleSelect(EntitySelect),
37    /// 7: members and roles.
38    MentionableSelect(EntitySelect),
39    /// 8.
40    ChannelSelect(EntitySelect),
41    /// 9: text with a button or thumbnail beside it (V2).
42    Section(Section),
43    /// 10: markdown text (V2, modal).
44    TextDisplay(TextDisplay),
45    /// 11: a small picture, only as a section's accessory.
46    Thumbnail(Thumbnail),
47    /// 12: up to ten pictures (V2).
48    MediaGallery(MediaGallery),
49    /// 13: one attached file (V2).
50    File(FileComponent),
51    /// 14: space or a line between components (V2).
52    Separator(Separator),
53    /// 17: a box around other components (V2).
54    Container(Container),
55    /// 18: a modal field's label around one input.
56    Label(Label),
57    /// 19: files the invoker uploads in a modal.
58    FileUpload(FileUpload),
59    /// 21: one choice among options, in a modal.
60    RadioGroup(RadioGroup),
61    /// 22: several choices, in a modal.
62    CheckboxGroup(CheckboxGroup),
63    /// 23: one yes/no box, in a modal.
64    Checkbox(Checkbox),
65}
66
67impl Component {
68    /// The wire `type` number.
69    #[must_use]
70    pub const fn kind(&self) -> u8 {
71        match self {
72            Self::ActionRow(_) => 1,
73            Self::Button(_) => 2,
74            Self::StringSelect(_) => 3,
75            Self::TextInput(_) => 4,
76            Self::UserSelect(_) => 5,
77            Self::RoleSelect(_) => 6,
78            Self::MentionableSelect(_) => 7,
79            Self::ChannelSelect(_) => 8,
80            Self::Section(_) => 9,
81            Self::TextDisplay(_) => 10,
82            Self::Thumbnail(_) => 11,
83            Self::MediaGallery(_) => 12,
84            Self::File(_) => 13,
85            Self::Separator(_) => 14,
86            Self::Container(_) => 17,
87            Self::Label(_) => 18,
88            Self::FileUpload(_) => 19,
89            Self::RadioGroup(_) => 21,
90            Self::CheckboxGroup(_) => 22,
91            Self::Checkbox(_) => 23,
92        }
93    }
94
95    /// The component's `custom_id`, for the kinds that send interactions or carry input.
96    #[must_use]
97    pub fn custom_id(&self) -> Option<&str> {
98        match self {
99            Self::Button(b) => b.custom_id.as_deref(),
100            Self::StringSelect(s) => Some(&s.custom_id),
101            Self::TextInput(t) => Some(&t.custom_id),
102            Self::UserSelect(s)
103            | Self::RoleSelect(s)
104            | Self::MentionableSelect(s)
105            | Self::ChannelSelect(s) => Some(&s.custom_id),
106            Self::FileUpload(f) => Some(&f.custom_id),
107            Self::RadioGroup(r) => Some(&r.custom_id),
108            Self::CheckboxGroup(c) => Some(&c.custom_id),
109            Self::Checkbox(c) => Some(&c.custom_id),
110            _ => None,
111        }
112    }
113
114    /// The nested components, in order (children, then a section's accessory, then a
115    /// label's input).
116    #[must_use]
117    pub fn children(&self) -> Vec<&Self> {
118        match self {
119            Self::ActionRow(r) => r.components.iter().collect(),
120            Self::Container(c) => c.components.iter().collect(),
121            Self::Section(s) => s
122                .components
123                .iter()
124                .chain(std::iter::once(s.accessory.as_ref()))
125                .collect(),
126            Self::Label(l) => vec![l.component.as_ref()],
127            _ => Vec::new(),
128        }
129    }
130}