Skip to main content

zaun_bot/
modal.rs

1//! [`Modal`] and [`TextInput`] builders (§8.14, types 4, 10, 18).
2
3use crate::types::{self, Label, LabelChild, ModalItem, TextInputStyle};
4use crate::{Select, layout::TextDisplay};
5
6/// Builds a [`types::TextInput`] for a modal.
7#[derive(Debug, Clone)]
8pub struct TextInput(types::TextInput);
9
10impl TextInput {
11    fn styled(custom_id: impl Into<String>, style: TextInputStyle) -> Self {
12        Self(types::TextInput {
13            id: None,
14            custom_id: custom_id.into(),
15            style,
16            min_length: None,
17            max_length: None,
18            required: None,
19            value: None,
20            placeholder: None,
21        })
22    }
23
24    /// A one-line input.
25    #[must_use]
26    pub fn short(custom_id: impl Into<String>) -> Self {
27        Self::styled(custom_id, TextInputStyle::Short)
28    }
29
30    /// A multi-line input.
31    #[must_use]
32    pub fn paragraph(custom_id: impl Into<String>) -> Self {
33        Self::styled(custom_id, TextInputStyle::Paragraph)
34    }
35
36    /// Length bounds (0–4 000).
37    #[must_use]
38    pub fn length(mut self, min: u16, max: u16) -> Self {
39        (self.0.min_length, self.0.max_length) = (Some(min), Some(max));
40        self
41    }
42
43    /// Whether it must be filled (default true).
44    #[must_use]
45    pub fn required(mut self, required: bool) -> Self {
46        self.0.required = Some(required);
47        self
48    }
49
50    /// Placeholder (≤ 100).
51    #[must_use]
52    pub fn placeholder(mut self, text: impl Into<String>) -> Self {
53        self.0.placeholder = Some(text.into());
54        self
55    }
56
57    /// Pre-filled value.
58    #[must_use]
59    pub fn value(mut self, text: impl Into<String>) -> Self {
60        self.0.value = Some(text.into());
61        self
62    }
63}
64
65/// Builds a [`types::Modal`]: 1–5 labelled inputs and text blocks.
66#[derive(Debug, Clone)]
67pub struct Modal {
68    custom_id: String,
69    title: String,
70    items: Vec<ModalItem>,
71}
72
73impl Modal {
74    /// A modal; `title` ≤ 45 scalars.
75    #[must_use]
76    pub fn new(custom_id: impl Into<String>, title: impl Into<String>) -> Self {
77        Self {
78            custom_id: custom_id.into(),
79            title: title.into(),
80            items: Vec::new(),
81        }
82    }
83
84    fn labelled(mut self, label: String, component: LabelChild) -> Self {
85        let label = Label {
86            id: None,
87            label,
88            description: None,
89            component,
90        };
91        self.items.push(ModalItem::Label(label));
92        self
93    }
94
95    /// Adds a text input under `label` (≤ 45).
96    #[must_use]
97    pub fn input(self, label: impl Into<String>, input: TextInput) -> Self {
98        self.labelled(label.into(), LabelChild::TextInput(input.0))
99    }
100
101    /// Adds a select under `label`.
102    #[must_use]
103    pub fn select(self, label: impl Into<String>, select: Select) -> Self {
104        let child = match select {
105            Select::Strings(s) => LabelChild::StringSelect(s),
106            Select::Entities(s) => LabelChild::EntitySelect(s),
107        };
108        self.labelled(label.into(), child)
109    }
110
111    /// Adds a block of text.
112    #[must_use]
113    pub fn text(mut self, content: impl Into<String>) -> Self {
114        self.items
115            .push(ModalItem::TextDisplay(TextDisplay::new(content)));
116        self
117    }
118
119    /// The finished modal.
120    #[must_use]
121    pub fn build(self) -> types::Modal {
122        types::Modal {
123            custom_id: self.custom_id,
124            title: self.title,
125            components: self.items,
126        }
127    }
128}