twilight_util/builder/message/
label.rs

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