twilight_util/builder/message/
container.rs

1use twilight_model::channel::message::{component::Container, Component};
2
3/// Create a container with a builder.
4#[derive(Clone, Debug, Eq, PartialEq)]
5#[must_use = "must be built into a container"]
6pub struct ContainerBuilder(Container);
7
8impl ContainerBuilder {
9    /// Create a new container builder.
10    pub const fn new() -> Self {
11        Self(Container {
12            accent_color: None,
13            components: Vec::new(),
14            id: None,
15            spoiler: None,
16        })
17    }
18
19    /// Set the accent color of this container; setting it to null will remove the accent color.
20    pub fn accent_color(mut self, accent_color: Option<u32>) -> Self {
21        self.0.accent_color.replace(accent_color);
22
23        self
24    }
25
26    /// Add a component to this container.
27    pub fn component(mut self, component: impl Into<Component>) -> Self {
28        self.0.components.push(component.into());
29
30        self
31    }
32
33    /// Set the identifier of this container.
34    pub fn id(mut self, id: i32) -> Self {
35        self.0.id.replace(id);
36
37        self
38    }
39
40    /// Specify whether this container is spoilered.
41    pub fn spoiler(mut self, spoiler: bool) -> Self {
42        self.0.spoiler.replace(spoiler);
43
44        self
45    }
46
47    /// Build into a container.
48    pub fn build(self) -> Container {
49        self.0
50    }
51}
52
53impl Default for ContainerBuilder {
54    fn default() -> Self {
55        Self::new()
56    }
57}
58
59impl From<ContainerBuilder> for Container {
60    fn from(builder: ContainerBuilder) -> Self {
61        builder.build()
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68    use static_assertions::assert_impl_all;
69    use std::fmt::Debug;
70
71    assert_impl_all!(ContainerBuilder: Clone, Debug, Eq, PartialEq, Send, Sync);
72    assert_impl_all!(Container: From<ContainerBuilder>);
73
74    #[test]
75    fn builder() {
76        let expected = Container {
77            accent_color: None,
78            components: Vec::new(),
79            id: None,
80            spoiler: None,
81        };
82
83        let actual = ContainerBuilder::new().build();
84
85        assert_eq!(expected, actual);
86    }
87}