1use crate::Button;
4use crate::types::{self, EntityKind, RowItem, SelectOption};
5
6#[derive(Debug, Clone)]
8pub enum Select {
9 Strings(types::StringSelect),
11 Entities(types::EntitySelect),
13}
14
15impl Select {
16 #[must_use]
18 pub fn strings(custom_id: impl Into<String>, options: &[(&str, &str)]) -> Self {
19 let options = options
20 .iter()
21 .map(|(label, value)| SelectOption {
22 label: (*label).to_owned(),
23 value: (*value).to_owned(),
24 description: None,
25 emoji: None,
26 is_default: false,
27 })
28 .collect();
29 Self::Strings(types::StringSelect {
30 id: None,
31 custom_id: custom_id.into(),
32 options,
33 placeholder: None,
34 min_values: None,
35 max_values: None,
36 required: None,
37 disabled: false,
38 })
39 }
40
41 #[must_use]
43 pub fn entities(kind: EntityKind, custom_id: impl Into<String>) -> Self {
44 Self::Entities(types::EntitySelect {
45 id: None,
46 kind,
47 custom_id: custom_id.into(),
48 placeholder: None,
49 min_values: None,
50 max_values: None,
51 default_values: Vec::new(),
52 channel_types: Vec::new(),
53 required: None,
54 disabled: false,
55 })
56 }
57
58 #[must_use]
60 pub fn placeholder(mut self, text: impl Into<String>) -> Self {
61 match &mut self {
62 Self::Strings(s) => s.placeholder = Some(text.into()),
63 Self::Entities(s) => s.placeholder = Some(text.into()),
64 }
65 self
66 }
67
68 #[must_use]
70 pub fn range(mut self, min: u8, max: u8) -> Self {
71 match &mut self {
72 Self::Strings(s) => (s.min_values, s.max_values) = (Some(min), Some(max)),
73 Self::Entities(s) => (s.min_values, s.max_values) = (Some(min), Some(max)),
74 }
75 self
76 }
77
78 pub(crate) fn row_item(self) -> RowItem {
79 match self {
80 Self::Strings(s) => RowItem::StringSelect(s),
81 Self::Entities(s) => RowItem::EntitySelect(s),
82 }
83 }
84}
85
86#[derive(Debug, Clone, Default)]
88pub struct ActionRow(Vec<RowItem>);
89
90impl ActionRow {
91 #[must_use]
93 pub fn new() -> Self {
94 Self::default()
95 }
96
97 #[must_use]
99 pub fn buttons(buttons: impl IntoIterator<Item = Button>) -> Self {
100 Self(
101 buttons
102 .into_iter()
103 .map(|b| RowItem::Button(b.build()))
104 .collect(),
105 )
106 }
107
108 #[must_use]
110 pub fn select(select: Select) -> Self {
111 Self(vec![select.row_item()])
112 }
113
114 #[must_use]
116 pub fn button(mut self, button: Button) -> Self {
117 self.0.push(RowItem::Button(button.build()));
118 self
119 }
120
121 #[must_use]
123 pub fn build(self) -> types::ActionRow {
124 types::ActionRow {
125 id: None,
126 components: self.0,
127 }
128 }
129}