twilight_util/builder/message/
section.rs

1use twilight_model::channel::message::{component::Section, Component};
2
3/// Create a section with a builder.
4#[derive(Clone, Debug, Eq, PartialEq)]
5#[must_use = "must be built into section"]
6pub struct SectionBuilder(Section);
7
8impl SectionBuilder {
9    /// Create a new section builder.
10    pub fn new(accessory: impl Into<Component>) -> Self {
11        Self(Section {
12            components: Vec::new(),
13            id: None,
14            accessory: Box::new(accessory.into()),
15        })
16    }
17
18    /// Add a component to this section.
19    pub fn component(mut self, component: impl Into<Component>) -> Self {
20        self.0.components.push(component.into());
21
22        self
23    }
24
25    /// Set the identifier of this section.
26    pub fn id(mut self, id: i32) -> Self {
27        self.0.id.replace(id);
28
29        self
30    }
31
32    /// Set the accessory of this section.
33    pub fn accessory(mut self, accessory: impl Into<Component>) -> Self {
34        self.0.accessory = Box::new(accessory.into());
35
36        self
37    }
38
39    /// Build into a section.
40    pub fn build(self) -> Section {
41        self.0
42    }
43}
44
45impl From<SectionBuilder> for Section {
46    fn from(builder: SectionBuilder) -> Self {
47        builder.build()
48    }
49}
50
51#[cfg(test)]
52mod tests {
53    use crate::builder::message::ActionRowBuilder;
54
55    use super::*;
56    use static_assertions::assert_impl_all;
57    use std::fmt::Debug;
58
59    assert_impl_all!(SectionBuilder: Clone, Debug, Eq, PartialEq, Send, Sync);
60    assert_impl_all!(Section: From<SectionBuilder>);
61
62    #[test]
63    fn builder() {
64        let action_row = ActionRowBuilder::new().build();
65
66        let expected = Section {
67            components: Vec::new(),
68            id: None,
69            accessory: Box::new(action_row.clone().into()),
70        };
71
72        let actual = SectionBuilder::new(action_row).build();
73
74        assert_eq!(actual, expected);
75    }
76}