Skip to main content

twilight_util/builder/message/
checkbox_group.rs

1use twilight_model::channel::message::component::{CheckboxGroup, CheckboxGroupOption};
2
3/// Create a checkbox group from a builder.
4#[derive(Clone, Debug, Eq, PartialEq)]
5#[must_use = "must be built into a checkbox group"]
6pub struct CheckboxGroupBuilder(CheckboxGroup);
7impl CheckboxGroupBuilder {
8    /// Create a new checkbox group builder
9    pub fn new(custom_id: impl Into<String>) -> Self {
10        Self(CheckboxGroup {
11            id: None,
12            custom_id: custom_id.into(),
13            options: vec![],
14            min_values: None,
15            max_values: None,
16            required: None,
17        })
18    }
19
20    /// Set the identifier of this checkbox group.
21    pub const fn id(mut self, id: i32) -> Self {
22        self.0.id.replace(id);
23
24        self
25    }
26
27    /// Add an option to this checkbox group
28    pub fn option(mut self, option: impl Into<CheckboxGroupOption>) -> Self {
29        self.0.options.push(option.into());
30
31        self
32    }
33
34    /// Set the minimum values for this checkbox group
35    pub const fn min_values(mut self, min_values: u8) -> Self {
36        self.0.min_values.replace(min_values);
37
38        self
39    }
40
41    /// Set the maximum values for this checkbox group
42    pub const fn max_values(mut self, max_values: u8) -> Self {
43        self.0.max_values.replace(max_values);
44
45        self
46    }
47
48    /// Set if this checkbox group is required or not
49    pub const fn required(mut self, required: bool) -> Self {
50        self.0.required.replace(required);
51
52        self
53    }
54
55    /// Build into a checkbox group
56    pub fn build(self) -> CheckboxGroup {
57        self.0
58    }
59}
60
61/// Create a checkbox group option with a builder
62#[derive(Clone, Debug, Eq, PartialEq)]
63#[must_use = "must be built into a checkbox group option"]
64pub struct CheckboxGroupOptionBuilder(CheckboxGroupOption);
65
66impl CheckboxGroupOptionBuilder {
67    /// Create a new checkbox group option builder
68    pub fn new(value: impl Into<String>, label: impl Into<String>) -> Self {
69        Self(CheckboxGroupOption {
70            value: value.into(),
71            label: label.into(),
72            description: None,
73            default: None,
74        })
75    }
76
77    /// Set if this option is selected by default
78    pub const fn default(mut self, default: bool) -> Self {
79        self.0.default.replace(default);
80
81        self
82    }
83
84    /// Set the description of this option
85    pub fn description(mut self, description: impl Into<String>) -> Self {
86        self.0.description.replace(description.into());
87
88        self
89    }
90
91    /// Build into a checkbox group option
92    pub fn build(self) -> CheckboxGroupOption {
93        self.0
94    }
95}