Skip to main content

zaun_community_protocol/command/object/component/
kinds.rs

1//! The fields of each component kind (§8.14). Every kind has an optional `id`, unique in its
2//! message, which the host assigns when absent.
3
4use serde::{Deserialize, Serialize};
5
6use super::Component;
7use crate::model::ChannelKind;
8
9/// Type 1.
10#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
11pub struct ActionRow {
12    #[serde(default, skip_serializing_if = "Option::is_none")]
13    /// Unique in the message.
14    pub id: Option<u32>,
15    /// 1–5 buttons, or one select.
16    pub components: Vec<Component>,
17}
18
19/// Button `style`: 1 primary, 2 secondary, 3 success, 4 danger, 5 link.
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
21#[serde(try_from = "u8", into = "u8")]
22pub enum ButtonStyle {
23    /// 1.
24    Primary,
25    /// 2.
26    Secondary,
27    /// 3.
28    Success,
29    /// 4.
30    Danger,
31    /// 5: opens `url` after the reader confirms; sends no interaction.
32    Link,
33}
34
35impl TryFrom<u8> for ButtonStyle {
36    type Error = String;
37    fn try_from(v: u8) -> Result<Self, String> {
38        Ok(match v {
39            1 => Self::Primary,
40            2 => Self::Secondary,
41            3 => Self::Success,
42            4 => Self::Danger,
43            5 => Self::Link,
44            _ => return Err(format!("unsupported button style {v}")),
45        })
46    }
47}
48
49impl From<ButtonStyle> for u8 {
50    fn from(s: ButtonStyle) -> Self {
51        s as Self + 1
52    }
53}
54
55/// Type 2.
56#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
57pub struct Button {
58    #[serde(default, skip_serializing_if = "Option::is_none")]
59    /// Unique in the message.
60    pub id: Option<u32>,
61    /// The look, and whether it is a link.
62    pub style: ButtonStyle,
63    #[serde(default, skip_serializing_if = "Option::is_none")]
64    /// ≤ 80 scalars.
65    pub label: Option<String>,
66    #[serde(default, skip_serializing_if = "Option::is_none")]
67    /// A Unicode emoji.
68    pub emoji: Option<String>,
69    #[serde(default, skip_serializing_if = "Option::is_none")]
70    /// Styles 1–4.
71    pub custom_id: Option<String>,
72    #[serde(default, skip_serializing_if = "Option::is_none")]
73    /// Style 5: an `https://` link.
74    pub url: Option<String>,
75    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
76    /// Shown but not clickable.
77    pub disabled: bool,
78}
79
80/// One option of a string select, radio group or checkbox group.
81#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
82pub struct SelectOption {
83    /// ≤ 100 scalars.
84    pub label: String,
85    /// ≤ 100 scalars; what the bot receives.
86    pub value: String,
87    #[serde(default, skip_serializing_if = "Option::is_none")]
88    /// ≤ 100 scalars.
89    pub description: Option<String>,
90    #[serde(default, skip_serializing_if = "Option::is_none")]
91    /// A Unicode emoji.
92    pub emoji: Option<String>,
93    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
94    /// Selected initially.
95    pub default: bool,
96}
97
98/// Type 3.
99#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
100pub struct StringSelect {
101    #[serde(default, skip_serializing_if = "Option::is_none")]
102    /// Unique in the message.
103    pub id: Option<u32>,
104    /// 1–100 bytes.
105    pub custom_id: String,
106    /// 1–25.
107    pub options: Vec<SelectOption>,
108    #[serde(default, skip_serializing_if = "Option::is_none")]
109    /// ≤ 150 scalars.
110    pub placeholder: Option<String>,
111    #[serde(default, skip_serializing_if = "Option::is_none")]
112    /// 0–25, default 1.
113    pub min_values: Option<u32>,
114    #[serde(default, skip_serializing_if = "Option::is_none")]
115    /// ≤ 25, default 1.
116    pub max_values: Option<u32>,
117    #[serde(default, skip_serializing_if = "Option::is_none")]
118    /// Modal only; default true.
119    pub required: Option<bool>,
120    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
121    /// Not in a modal.
122    pub disabled: bool,
123}
124
125/// `default_values` entry kind of an entity select.
126#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
127#[serde(rename_all = "snake_case")]
128pub enum DefaultValueKind {
129    /// A member id.
130    User,
131    /// A role id.
132    Role,
133    /// A channel id.
134    Channel,
135}
136
137/// A preselected entity of a user, role, mentionable or channel select.
138#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
139pub struct DefaultValue {
140    /// The id, base64url.
141    pub id: String,
142    /// What the id names.
143    #[serde(rename = "type")]
144    pub kind: DefaultValueKind,
145}
146
147/// Types 5–8.
148#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
149pub struct EntitySelect {
150    #[serde(default, skip_serializing_if = "Option::is_none")]
151    /// Unique in the message.
152    pub id: Option<u32>,
153    /// 1–100 bytes.
154    pub custom_id: String,
155    #[serde(default, skip_serializing_if = "Option::is_none")]
156    /// ≤ 150 scalars.
157    pub placeholder: Option<String>,
158    #[serde(default, skip_serializing_if = "Option::is_none")]
159    /// 0–25, default 1.
160    pub min_values: Option<u32>,
161    #[serde(default, skip_serializing_if = "Option::is_none")]
162    /// ≤ 25, default 1.
163    pub max_values: Option<u32>,
164    #[serde(default, skip_serializing_if = "Vec::is_empty")]
165    /// Preselected entities, ≤ `max_values`.
166    pub default_values: Vec<DefaultValue>,
167    #[serde(default, skip_serializing_if = "Vec::is_empty")]
168    /// Type 8 only: the channel kinds offered.
169    pub channel_types: Vec<ChannelKind>,
170    #[serde(default, skip_serializing_if = "Option::is_none")]
171    /// Modal only; default true.
172    pub required: Option<bool>,
173    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
174    /// Not in a modal.
175    pub disabled: bool,
176}
177
178/// Text input `style`: 1 short, 2 paragraph.
179#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
180#[serde(try_from = "u8", into = "u8")]
181pub enum TextInputStyle {
182    /// 1: one line.
183    Short,
184    /// 2: several lines.
185    Paragraph,
186}
187
188impl TryFrom<u8> for TextInputStyle {
189    type Error = String;
190    fn try_from(v: u8) -> Result<Self, String> {
191        match v {
192            1 => Ok(Self::Short),
193            2 => Ok(Self::Paragraph),
194            _ => Err(format!("unsupported text input style {v}")),
195        }
196    }
197}
198
199impl From<TextInputStyle> for u8 {
200    fn from(s: TextInputStyle) -> Self {
201        s as Self + 1
202    }
203}
204
205/// Type 4.
206#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
207pub struct TextInput {
208    #[serde(default, skip_serializing_if = "Option::is_none")]
209    /// Unique in the modal.
210    pub id: Option<u32>,
211    /// 1–100 bytes.
212    pub custom_id: String,
213    /// Short or paragraph.
214    pub style: TextInputStyle,
215    #[serde(default, skip_serializing_if = "Option::is_none")]
216    /// 0–4 000.
217    pub min_length: Option<u32>,
218    #[serde(default, skip_serializing_if = "Option::is_none")]
219    /// 1–4 000.
220    pub max_length: Option<u32>,
221    #[serde(default, skip_serializing_if = "Option::is_none")]
222    /// Default true.
223    pub required: Option<bool>,
224    #[serde(default, skip_serializing_if = "Option::is_none")]
225    /// Prefilled text, ≤ 4 000 scalars.
226    pub value: Option<String>,
227    #[serde(default, skip_serializing_if = "Option::is_none")]
228    /// ≤ 100 scalars.
229    pub placeholder: Option<String>,
230}
231
232mod layout;
233
234pub use layout::{
235    Checkbox, CheckboxGroup, Container, FileComponent, FileUpload, Label, MediaGallery, MediaItem,
236    RadioGroup, Section, Separator, TextDisplay, Thumbnail,
237};