Skip to main content

twilight_util/builder/embed/
field.rs

1//! Create embed fields.
2
3use twilight_model::channel::message::embed::EmbedField;
4
5/// Create an embed field with a builder.
6///
7/// This can be passed into [`EmbedBuilder::field`].
8///
9/// Fields are not inlined by default. Use [`inline`] to inline a field.
10///
11/// [`EmbedBuilder::field`]: crate::builder::embed::EmbedBuilder::field
12/// [`inline`]: Self::inline
13#[derive(Clone, Debug, Eq, PartialEq)]
14#[must_use = "must be built into an embed field"]
15pub struct EmbedFieldBuilder(EmbedField);
16
17impl EmbedFieldBuilder {
18    /// Create a new embed field builder.
19    ///
20    /// Refer to [`FIELD_NAME_LENGTH`] for the maximum number of UTF-16 code
21    /// points that can be in a field name.
22    ///
23    /// Refer to [`FIELD_VALUE_LENGTH`] for the maximum number of UTF-16 code
24    /// points that can be in a field value.
25    ///
26    /// [`FIELD_NAME_LENGTH`]: twilight_validate::embed::FIELD_NAME_LENGTH
27    /// [`FIELD_VALUE_LENGTH`]: twilight_validate::embed::FIELD_VALUE_LENGTH
28    pub fn new(name: impl Into<String>, value: impl Into<String>) -> Self {
29        Self(EmbedField {
30            inline: false,
31            name: name.into(),
32            value: value.into(),
33        })
34    }
35
36    /// Build into an embed field.
37    #[must_use = "should be used as part of an embed builder"]
38    pub fn build(self) -> EmbedField {
39        self.0
40    }
41
42    /// Inline the field.
43    ///
44    /// # Examples
45    ///
46    /// Create an inlined field:
47    ///
48    /// ```no_run
49    /// use twilight_util::builder::embed::EmbedFieldBuilder;
50    ///
51    /// let field = EmbedFieldBuilder::new("twilight", "is cool")
52    ///     .inline()
53    ///     .build();
54    /// ```
55    pub const fn inline(mut self) -> Self {
56        self.0.inline = true;
57
58        self
59    }
60}
61
62impl From<EmbedFieldBuilder> for EmbedField {
63    /// Convert an embed field builder into an embed field.
64    ///
65    /// This is equivalent to calling [`EmbedFieldBuilder::build`].
66    fn from(builder: EmbedFieldBuilder) -> Self {
67        builder.build()
68    }
69}
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74    use static_assertions::assert_impl_all;
75    use std::fmt::Debug;
76
77    assert_impl_all!(EmbedFieldBuilder: Clone, Debug, Eq, PartialEq, Send, Sync);
78    assert_impl_all!(EmbedField: From<EmbedFieldBuilder>);
79
80    #[test]
81    fn builder_inline() {
82        let expected = EmbedField {
83            inline: true,
84            name: "name".to_owned(),
85            value: "value".to_owned(),
86        };
87        let actual = EmbedFieldBuilder::new("name", "value").inline().build();
88
89        assert_eq!(actual, expected);
90    }
91
92    #[test]
93    fn builder_no_inline() {
94        let expected = EmbedField {
95            inline: false,
96            name: "name".to_owned(),
97            value: "value".to_owned(),
98        };
99        let actual = EmbedFieldBuilder::new("name", "value").build();
100
101        assert_eq!(actual, expected);
102    }
103}