1mod content;
27mod interactive;
28mod layout;
29
30use compact_str::CompactString;
31use serde::{Deserialize, Deserializer, Serialize, Serializer};
32
33pub use self::{
34 content::{
35 ContentScanMetadata, ContentScanMetadataFlags, File, LoadingState, MediaGallery,
36 MediaGalleryItem, TextDisplay, Thumbnail, UnfurledMediaItem, UnfurledMediaItemFlags,
37 },
38 interactive::{
39 AutoPopulatedSelect, Button, ButtonStyle, ChannelSelect, Checkbox, CheckboxGroup,
40 FileUpload, GroupOption, InteractiveButton, LinkButton, PremiumButton, RadioGroup,
41 SelectDefaultValue, SelectDefaultValueType, SelectOption, StringSelect, TextInput,
42 TextInputStyle, UnknownButton,
43 },
44 layout::{ActionRow, Container, Label, Section, Separator, SeparatorSpacing},
45};
46use crate::{channel::ChannelType, emoji::PartialEmoji, id::SkuId, macros::int_enum};
47
48#[derive(Clone, Debug, PartialEq, Eq)]
53#[non_exhaustive]
54pub enum Component {
55 ActionRow(ActionRow),
57 Button(Button),
59 StringSelect(StringSelect),
61 TextInput(TextInput),
63 UserSelect(AutoPopulatedSelect),
65 RoleSelect(AutoPopulatedSelect),
67 MentionableSelect(AutoPopulatedSelect),
69 ChannelSelect(ChannelSelect),
71 Section(Section),
73 TextDisplay(TextDisplay),
75 Thumbnail(Thumbnail),
77 MediaGallery(MediaGallery),
79 File(File),
81 Separator(Separator),
83 Container(Container),
85 Label(Label),
87 FileUpload(FileUpload),
89 RadioGroup(RadioGroup),
91 CheckboxGroup(CheckboxGroup),
93 Checkbox(Checkbox),
95 Unknown(UnknownComponent),
97}
98
99#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize)]
104#[non_exhaustive]
105pub struct UnknownComponent {
106 #[serde(skip)]
108 pub kind: ComponentType,
109 #[serde(skip_serializing_if = "Option::is_none")]
111 pub id: Option<i32>,
112}
113
114int_enum! {
115 pub enum ComponentType: u8 {
119 ActionRow = 1,
121 Button = 2,
123 StringSelect = 3,
125 TextInput = 4,
127 UserSelect = 5,
129 RoleSelect = 6,
131 MentionableSelect = 7,
133 ChannelSelect = 8,
135 Section = 9,
137 TextDisplay = 10,
139 Thumbnail = 11,
141 MediaGallery = 12,
143 File = 13,
145 Separator = 14,
147 ContentInventoryEntry = 16,
152 Container = 17,
154 Label = 18,
156 FileUpload = 19,
158 RadioGroup = 21,
160 CheckboxGroup = 22,
162 Checkbox = 23,
164 }
165}
166
167impl Component {
168 #[must_use]
172 pub const fn kind(&self) -> ComponentType {
173 match self {
174 Self::ActionRow(_) => ComponentType::ActionRow,
175 Self::Button(_) => ComponentType::Button,
176 Self::StringSelect(_) => ComponentType::StringSelect,
177 Self::TextInput(_) => ComponentType::TextInput,
178 Self::UserSelect(_) => ComponentType::UserSelect,
179 Self::RoleSelect(_) => ComponentType::RoleSelect,
180 Self::MentionableSelect(_) => ComponentType::MentionableSelect,
181 Self::ChannelSelect(_) => ComponentType::ChannelSelect,
182 Self::Section(_) => ComponentType::Section,
183 Self::TextDisplay(_) => ComponentType::TextDisplay,
184 Self::Thumbnail(_) => ComponentType::Thumbnail,
185 Self::MediaGallery(_) => ComponentType::MediaGallery,
186 Self::File(_) => ComponentType::File,
187 Self::Separator(_) => ComponentType::Separator,
188 Self::Container(_) => ComponentType::Container,
189 Self::Label(_) => ComponentType::Label,
190 Self::FileUpload(_) => ComponentType::FileUpload,
191 Self::RadioGroup(_) => ComponentType::RadioGroup,
192 Self::CheckboxGroup(_) => ComponentType::CheckboxGroup,
193 Self::Checkbox(_) => ComponentType::Checkbox,
194 Self::Unknown(component) => component.kind,
195 }
196 }
197
198 #[must_use]
202 pub const fn id(&self) -> Option<i32> {
203 match self {
204 Self::ActionRow(component) => component.id,
205 Self::Button(component) => component.id(),
206 Self::StringSelect(component) => component.id,
207 Self::TextInput(component) => component.id,
208 Self::UserSelect(component)
209 | Self::RoleSelect(component)
210 | Self::MentionableSelect(component) => component.id,
211 Self::ChannelSelect(component) => component.id,
212 Self::Section(component) => component.id,
213 Self::TextDisplay(component) => component.id,
214 Self::Thumbnail(component) => component.id,
215 Self::MediaGallery(component) => component.id,
216 Self::File(component) => component.id,
217 Self::Separator(component) => component.id,
218 Self::Container(component) => component.id,
219 Self::Label(component) => component.id,
220 Self::FileUpload(component) => component.id,
221 Self::RadioGroup(component) => component.id,
222 Self::CheckboxGroup(component) => component.id,
223 Self::Checkbox(component) => component.id,
224 Self::Unknown(component) => component.id,
225 }
226 }
227
228 #[must_use]
232 pub fn custom_id(&self) -> Option<&str> {
233 match self {
234 Self::Button(component) => component.custom_id(),
235 Self::StringSelect(component) => Some(&component.custom_id),
236 Self::TextInput(component) => Some(&component.custom_id),
237 Self::UserSelect(component)
238 | Self::RoleSelect(component)
239 | Self::MentionableSelect(component) => Some(&component.custom_id),
240 Self::ChannelSelect(component) => Some(&component.custom_id),
241 Self::FileUpload(component) => Some(&component.custom_id),
242 Self::RadioGroup(component) => Some(&component.custom_id),
243 Self::CheckboxGroup(component) => Some(&component.custom_id),
244 Self::Checkbox(component) => Some(&component.custom_id),
245 Self::ActionRow(_)
246 | Self::Section(_)
247 | Self::TextDisplay(_)
248 | Self::Thumbnail(_)
249 | Self::MediaGallery(_)
250 | Self::File(_)
251 | Self::Separator(_)
252 | Self::Container(_)
253 | Self::Label(_)
254 | Self::Unknown(_) => None,
255 }
256 }
257
258 #[must_use]
263 pub fn children(&self) -> &[Self] {
264 match self {
265 Self::ActionRow(component) => &component.components,
266 Self::Section(component) => &component.components,
267 Self::Container(component) => &component.components,
268 _ => &[],
269 }
270 }
271}
272
273macro_rules! from_struct {
274 ($($ty:ident => $variant:ident),* $(,)?) => {
275 $(
276 impl From<$ty> for Component {
277 fn from(component: $ty) -> Self {
278 Self::$variant(component)
279 }
280 }
281 )*
282 };
283}
284
285from_struct! {
286 ActionRow => ActionRow,
287 Button => Button,
288 StringSelect => StringSelect,
289 TextInput => TextInput,
290 ChannelSelect => ChannelSelect,
291 Section => Section,
292 TextDisplay => TextDisplay,
293 Thumbnail => Thumbnail,
294 MediaGallery => MediaGallery,
295 File => File,
296 Separator => Separator,
297 Container => Container,
298 Label => Label,
299 FileUpload => FileUpload,
300 RadioGroup => RadioGroup,
301 CheckboxGroup => CheckboxGroup,
302 Checkbox => Checkbox,
303}
304
305macro_rules! from_button {
306 ($($ty:ident => $variant:ident),* $(,)?) => {
307 $(
308 impl From<$ty> for Button {
309 fn from(button: $ty) -> Self {
310 Self::$variant(button)
311 }
312 }
313
314 impl From<$ty> for Component {
315 fn from(button: $ty) -> Self {
316 Self::Button(Button::$variant(button))
317 }
318 }
319 )*
320 };
321}
322
323from_button! {
324 InteractiveButton => Interactive,
325 LinkButton => Link,
326 PremiumButton => Premium,
327}
328
329#[derive(Deserialize)]
332struct RawComponent {
333 #[serde(rename = "type")]
334 kind: ComponentType,
335 #[serde(default)]
336 id: Option<i32>,
337 #[serde(default)]
338 custom_id: Option<CompactString>,
339 #[serde(default)]
340 components: Vec<Component>,
341 #[serde(default)]
342 component: Option<Box<Component>>,
343 #[serde(default)]
344 accessory: Option<Box<Component>>,
345 #[serde(default)]
346 style: Option<u8>,
347 #[serde(default)]
348 label: Option<CompactString>,
349 #[serde(default)]
350 emoji: Option<PartialEmoji>,
351 #[serde(default)]
352 url: Option<CompactString>,
353 #[serde(default)]
354 sku_id: Option<SkuId>,
355 #[serde(default)]
356 disabled: bool,
357 #[serde(default)]
358 options: Vec<SelectOption>,
359 #[serde(default)]
360 placeholder: Option<CompactString>,
361 #[serde(default)]
362 min_values: Option<u8>,
363 #[serde(default)]
364 max_values: Option<u8>,
365 #[serde(default)]
366 required: Option<bool>,
367 #[serde(default)]
368 min_length: Option<u16>,
369 #[serde(default)]
370 max_length: Option<u16>,
371 #[serde(default)]
372 value: Option<CompactString>,
373 #[serde(default, deserialize_with = "crate::serde_util::null_as_default")]
374 default_values: Vec<SelectDefaultValue>,
375 #[serde(default, deserialize_with = "crate::serde_util::null_as_default")]
376 channel_types: Vec<ChannelType>,
377 #[serde(default)]
378 content: Option<CompactString>,
379 #[serde(default)]
380 media: Option<Box<UnfurledMediaItem>>,
381 #[serde(default)]
382 description: Option<CompactString>,
383 #[serde(default)]
384 spoiler: bool,
385 #[serde(default)]
386 items: Vec<MediaGalleryItem>,
387 #[serde(default)]
388 file: Option<Box<UnfurledMediaItem>>,
389 #[serde(default)]
390 name: Option<CompactString>,
391 #[serde(default)]
392 size: Option<u64>,
393 #[serde(default)]
394 divider: Option<bool>,
395 #[serde(default)]
396 spacing: Option<SeparatorSpacing>,
397 #[serde(default)]
398 accent_color: Option<u32>,
399 #[serde(default, deserialize_with = "crate::serde_util::null_as_default")]
400 file_types: Vec<CompactString>,
401 #[serde(default)]
402 default: bool,
403}
404
405impl RawComponent {
406 fn required<T, E: serde::de::Error>(value: Option<T>, field: &'static str) -> Result<T, E> {
407 value.ok_or_else(|| E::missing_field(field))
408 }
409
410 fn into_component<E: serde::de::Error>(self) -> Result<Component, E> {
411 let id = self.id;
412 Ok(match self.kind {
413 ComponentType::ActionRow => Component::ActionRow(ActionRow {
414 id,
415 components: self.components,
416 }),
417 ComponentType::Button => Component::Button(self.into_button()?),
418 ComponentType::StringSelect => Component::StringSelect(StringSelect {
419 id,
420 custom_id: Self::required(self.custom_id, "custom_id")?,
421 options: self.options,
422 placeholder: self.placeholder,
423 min_values: self.min_values,
424 max_values: self.max_values,
425 required: self.required.unwrap_or(true),
426 disabled: self.disabled,
427 }),
428 ComponentType::TextInput => Component::TextInput(TextInput {
429 id,
430 custom_id: Self::required(self.custom_id, "custom_id")?,
431 style: TextInputStyle::from(Self::required(self.style, "style")?),
432 label: self.label,
433 min_length: self.min_length,
434 max_length: self.max_length,
435 required: self.required.unwrap_or(true),
436 value: self.value,
437 placeholder: self.placeholder,
438 }),
439 kind @ (ComponentType::UserSelect
440 | ComponentType::RoleSelect
441 | ComponentType::MentionableSelect) => {
442 let select = AutoPopulatedSelect {
443 id,
444 custom_id: Self::required(self.custom_id, "custom_id")?,
445 placeholder: self.placeholder,
446 default_values: self.default_values,
447 min_values: self.min_values,
448 max_values: self.max_values,
449 required: self.required.unwrap_or(true),
450 disabled: self.disabled,
451 };
452 match kind {
453 ComponentType::UserSelect => Component::UserSelect(select),
454 ComponentType::RoleSelect => Component::RoleSelect(select),
455 _ => Component::MentionableSelect(select),
456 }
457 }
458 ComponentType::ChannelSelect => Component::ChannelSelect(ChannelSelect {
459 id,
460 custom_id: Self::required(self.custom_id, "custom_id")?,
461 channel_types: self.channel_types,
462 placeholder: self.placeholder,
463 default_values: self.default_values,
464 min_values: self.min_values,
465 max_values: self.max_values,
466 required: self.required.unwrap_or(true),
467 disabled: self.disabled,
468 }),
469 kind => self.into_v2_component(kind)?,
470 })
471 }
472
473 fn into_v2_component<E: serde::de::Error>(self, kind: ComponentType) -> Result<Component, E> {
475 let id = self.id;
476 Ok(match kind {
477 ComponentType::Section => Component::Section(Section {
478 id,
479 components: self.components,
480 accessory: Self::required(self.accessory, "accessory")?,
481 }),
482 ComponentType::TextDisplay => Component::TextDisplay(TextDisplay {
483 id,
484 content: Self::required(self.content, "content")?,
485 }),
486 ComponentType::Thumbnail => Component::Thumbnail(Thumbnail {
487 id,
488 media: Self::required(self.media, "media")?,
489 description: self.description,
490 spoiler: self.spoiler,
491 }),
492 ComponentType::MediaGallery => Component::MediaGallery(MediaGallery {
493 id,
494 items: self.items,
495 }),
496 ComponentType::File => Component::File(File {
497 id,
498 file: Self::required(self.file, "file")?,
499 spoiler: self.spoiler,
500 name: self.name,
501 size: self.size,
502 }),
503 ComponentType::Separator => Component::Separator(Separator {
504 id,
505 divider: self.divider.unwrap_or(true),
506 spacing: self.spacing,
507 }),
508 ComponentType::Container => Component::Container(Container {
509 id,
510 components: self.components,
511 accent_color: self.accent_color,
512 spoiler: self.spoiler,
513 }),
514 ComponentType::Label => Component::Label(Label {
515 id,
516 label: Self::required(self.label, "label")?,
517 description: self.description,
518 component: Self::required(self.component, "component")?,
519 }),
520 ComponentType::FileUpload => Component::FileUpload(FileUpload {
521 id,
522 custom_id: Self::required(self.custom_id, "custom_id")?,
523 min_values: self.min_values,
524 max_values: self.max_values,
525 required: self.required.unwrap_or(true),
526 file_types: self.file_types,
527 }),
528 ComponentType::RadioGroup => Component::RadioGroup(RadioGroup {
529 id,
530 custom_id: Self::required(self.custom_id, "custom_id")?,
531 options: self.options.into_iter().map(GroupOption::from).collect(),
532 required: self.required.unwrap_or(true),
533 }),
534 ComponentType::CheckboxGroup => Component::CheckboxGroup(CheckboxGroup {
535 id,
536 custom_id: Self::required(self.custom_id, "custom_id")?,
537 options: self.options.into_iter().map(GroupOption::from).collect(),
538 min_values: self.min_values,
539 max_values: self.max_values,
540 required: self.required.unwrap_or(true),
541 }),
542 ComponentType::Checkbox => Component::Checkbox(Checkbox {
543 id,
544 custom_id: Self::required(self.custom_id, "custom_id")?,
545 default: self.default,
546 }),
547 kind => Component::Unknown(UnknownComponent { kind, id }),
548 })
549 }
550
551 fn into_button<E: serde::de::Error>(self) -> Result<Button, E> {
552 let id = self.id;
553 let style = ButtonStyle::from(Self::required(self.style, "style")?);
554 Ok(match style {
555 ButtonStyle::Primary
556 | ButtonStyle::Secondary
557 | ButtonStyle::Success
558 | ButtonStyle::Danger => Button::Interactive(InteractiveButton {
559 id,
560 style,
561 custom_id: Self::required(self.custom_id, "custom_id")?,
562 label: self.label,
563 emoji: self.emoji,
564 disabled: self.disabled,
565 }),
566 ButtonStyle::Link => Button::Link(LinkButton {
567 id,
568 url: Self::required(self.url, "url")?,
569 label: self.label,
570 emoji: self.emoji,
571 disabled: self.disabled,
572 }),
573 ButtonStyle::Premium => Button::Premium(PremiumButton {
574 id,
575 sku_id: Self::required(self.sku_id, "sku_id")?,
576 disabled: self.disabled,
577 }),
578 ButtonStyle::Unknown(_) => Button::Unknown(Box::new(UnknownButton {
579 id,
580 style,
581 custom_id: self.custom_id,
582 label: self.label,
583 emoji: self.emoji,
584 url: self.url,
585 sku_id: self.sku_id,
586 disabled: self.disabled,
587 })),
588 })
589 }
590}
591
592impl From<SelectOption> for GroupOption {
593 fn from(option: SelectOption) -> Self {
594 Self {
595 value: option.value,
596 label: option.label,
597 description: option.description,
598 default: option.default,
599 }
600 }
601}
602
603impl<'de> Deserialize<'de> for Component {
604 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
605 RawComponent::deserialize(deserializer)?.into_component::<D::Error>()
606 }
607}
608
609#[derive(Serialize)]
611struct Tagged<'a, T> {
612 #[serde(rename = "type")]
613 kind: ComponentType,
614 #[serde(flatten)]
615 component: &'a T,
616}
617
618impl Serialize for Component {
619 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
620 let kind = self.kind();
621 match self {
622 Self::ActionRow(component) => Tagged { kind, component }.serialize(serializer),
623 Self::Button(component) => Tagged { kind, component }.serialize(serializer),
624 Self::StringSelect(component) => Tagged { kind, component }.serialize(serializer),
625 Self::TextInput(component) => Tagged { kind, component }.serialize(serializer),
626 Self::UserSelect(component)
627 | Self::RoleSelect(component)
628 | Self::MentionableSelect(component) => {
629 Tagged { kind, component }.serialize(serializer)
630 }
631 Self::ChannelSelect(component) => Tagged { kind, component }.serialize(serializer),
632 Self::Section(component) => Tagged { kind, component }.serialize(serializer),
633 Self::TextDisplay(component) => Tagged { kind, component }.serialize(serializer),
634 Self::Thumbnail(component) => Tagged { kind, component }.serialize(serializer),
635 Self::MediaGallery(component) => Tagged { kind, component }.serialize(serializer),
636 Self::File(component) => Tagged { kind, component }.serialize(serializer),
637 Self::Separator(component) => Tagged { kind, component }.serialize(serializer),
638 Self::Container(component) => Tagged { kind, component }.serialize(serializer),
639 Self::Label(component) => Tagged { kind, component }.serialize(serializer),
640 Self::FileUpload(component) => Tagged { kind, component }.serialize(serializer),
641 Self::RadioGroup(component) => Tagged { kind, component }.serialize(serializer),
642 Self::CheckboxGroup(component) => Tagged { kind, component }.serialize(serializer),
643 Self::Checkbox(component) => Tagged { kind, component }.serialize(serializer),
644 Self::Unknown(component) => Tagged { kind, component }.serialize(serializer),
645 }
646 }
647}
648
649#[cfg(test)]
650mod tests {
651 use super::*;
652
653 #[test]
654 fn sizes() {
655 assert!(size_of::<Component>() <= 120, "{}", size_of::<Component>());
657 }
658}