Skip to content

Commit 4a71548

Browse files
committed
feat(model): Add into_* methods to Components
1 parent 82756d7 commit 4a71548

File tree

1 file changed

+188
-0
lines changed

1 file changed

+188
-0
lines changed

src/model/application/component.rs

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,72 @@ pub enum Component {
5656
Unknown(u8),
5757
}
5858

59+
impl Component {
60+
#[must_use]
61+
pub fn into_action_row(self) -> Option<ActionRow> {
62+
match self {
63+
Component::ActionRow(component) => Some(component),
64+
_ => None,
65+
}
66+
}
67+
68+
#[must_use]
69+
pub fn into_section(self) -> Option<Section> {
70+
match self {
71+
Component::Section(component) => Some(component),
72+
_ => None,
73+
}
74+
}
75+
76+
#[must_use]
77+
pub fn into_text_display(self) -> Option<TextDisplay> {
78+
match self {
79+
Component::TextDisplay(component) => Some(component),
80+
_ => None,
81+
}
82+
}
83+
84+
#[must_use]
85+
pub fn into_media_gallery(self) -> Option<MediaGallery> {
86+
match self {
87+
Component::MediaGallery(component) => Some(component),
88+
_ => None,
89+
}
90+
}
91+
92+
#[must_use]
93+
pub fn into_file(self) -> Option<FileComponent> {
94+
match self {
95+
Component::File(component) => Some(component),
96+
_ => None,
97+
}
98+
}
99+
100+
#[must_use]
101+
pub fn into_separator(self) -> Option<Separator> {
102+
match self {
103+
Component::Separator(component) => Some(component),
104+
_ => None,
105+
}
106+
}
107+
108+
#[must_use]
109+
pub fn into_container(self) -> Option<Container> {
110+
match self {
111+
Component::Container(component) => Some(component),
112+
_ => None,
113+
}
114+
}
115+
116+
#[must_use]
117+
pub fn into_label(self) -> Option<Label> {
118+
match self {
119+
Component::Label(component) => Some(component),
120+
_ => None,
121+
}
122+
}
123+
}
124+
59125
impl<'de> Deserialize<'de> for Component {
60126
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
61127
where
@@ -117,6 +183,16 @@ pub enum SectionComponent {
117183
TextDisplay(TextDisplay),
118184
}
119185

186+
impl SectionComponent {
187+
#[must_use]
188+
#[expect(clippy::unnecessary_wraps)] // Returns option to avoid a future breaking change
189+
pub fn into_text_display(self) -> Option<TextDisplay> {
190+
match self {
191+
SectionComponent::TextDisplay(component) => Some(component),
192+
}
193+
}
194+
}
195+
120196
impl<'de> Deserialize<'de> for SectionComponent {
121197
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
122198
#[derive(Deserialize)]
@@ -151,6 +227,24 @@ pub enum SectionAccessory {
151227
Thumbnail(Thumbnail),
152228
}
153229

230+
impl SectionAccessory {
231+
#[must_use]
232+
pub fn into_into_button(self) -> Option<Button> {
233+
match self {
234+
SectionAccessory::Button(component) => Some(component),
235+
_ => None,
236+
}
237+
}
238+
239+
#[must_use]
240+
pub fn into_into_thumbnail(self) -> Option<Thumbnail> {
241+
match self {
242+
SectionAccessory::Thumbnail(component) => Some(component),
243+
_ => None,
244+
}
245+
}
246+
}
247+
154248
impl<'de> Deserialize<'de> for SectionAccessory {
155249
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
156250
#[derive(Deserialize)]
@@ -340,6 +434,56 @@ pub enum ContainerComponent {
340434
Separator(Separator),
341435
}
342436

437+
impl ContainerComponent {
438+
#[must_use]
439+
pub fn into_action_row(self) -> Option<ActionRow> {
440+
match self {
441+
ContainerComponent::ActionRow(component) => Some(component),
442+
_ => None,
443+
}
444+
}
445+
446+
#[must_use]
447+
pub fn into_section(self) -> Option<Section> {
448+
match self {
449+
ContainerComponent::Section(component) => Some(component),
450+
_ => None,
451+
}
452+
}
453+
454+
#[must_use]
455+
pub fn into_text_display(self) -> Option<TextDisplay> {
456+
match self {
457+
ContainerComponent::TextDisplay(component) => Some(component),
458+
_ => None,
459+
}
460+
}
461+
462+
#[must_use]
463+
pub fn into_media_gallery(self) -> Option<MediaGallery> {
464+
match self {
465+
ContainerComponent::MediaGallery(component) => Some(component),
466+
_ => None,
467+
}
468+
}
469+
470+
#[must_use]
471+
pub fn into_file(self) -> Option<FileComponent> {
472+
match self {
473+
ContainerComponent::File(component) => Some(component),
474+
_ => None,
475+
}
476+
}
477+
478+
#[must_use]
479+
pub fn into_separator(self) -> Option<Separator> {
480+
match self {
481+
ContainerComponent::Separator(component) => Some(component),
482+
_ => None,
483+
}
484+
}
485+
}
486+
343487
impl<'de> Deserialize<'de> for ContainerComponent {
344488
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
345489
where
@@ -408,6 +552,32 @@ pub enum LabelComponent {
408552
FileUpload(FileUpload),
409553
}
410554

555+
impl LabelComponent {
556+
#[must_use]
557+
pub fn into_select_menu(self) -> Option<SelectMenu> {
558+
match self {
559+
LabelComponent::SelectMenu(component) => Some(component),
560+
_ => None,
561+
}
562+
}
563+
564+
#[must_use]
565+
pub fn into_input_text(self) -> Option<InputText> {
566+
match self {
567+
LabelComponent::InputText(component) => Some(component),
568+
_ => None,
569+
}
570+
}
571+
572+
#[must_use]
573+
pub fn into_file_upload(self) -> Option<FileUpload> {
574+
match self {
575+
LabelComponent::FileUpload(component) => Some(component),
576+
_ => None,
577+
}
578+
}
579+
}
580+
411581
impl<'de> Deserialize<'de> for LabelComponent {
412582
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
413583
#[derive(Deserialize)]
@@ -482,6 +652,24 @@ pub enum ActionRowComponent {
482652
SelectMenu(SelectMenu),
483653
}
484654

655+
impl ActionRowComponent {
656+
#[must_use]
657+
pub fn into_button(self) -> Option<Button> {
658+
match self {
659+
ActionRowComponent::Button(component) => Some(component),
660+
_ => None,
661+
}
662+
}
663+
664+
#[must_use]
665+
pub fn into_select_menu(self) -> Option<SelectMenu> {
666+
match self {
667+
ActionRowComponent::SelectMenu(component) => Some(component),
668+
_ => None,
669+
}
670+
}
671+
}
672+
485673
impl<'de> Deserialize<'de> for ActionRowComponent {
486674
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
487675
#[derive(Deserialize)]

0 commit comments

Comments
 (0)