twilight_embed_builder/
field.rs

1//! Create embed fields.
2
3use twilight_model::channel::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::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 default embed field builder.
19    ///
20    /// Refer to [`EmbedBuilder::FIELD_NAME_LENGTH_LIMIT`] for the maximum
21    /// number of UTF-16 code points that can be in a field name.
22    ///
23    /// Refer to [`EmbedBuilder::FIELD_VALUE_LENGTH_LIMIT`] for the maximum
24    /// number of UTF-16 code points that can be in a field value.
25    ///
26    /// [`EmbedBuilder::FIELD_NAME_LENGTH_LIMIT`]: crate::EmbedBuilder::FIELD_NAME_LENGTH_LIMIT
27    /// [`EmbedBuilder::FIELD_VALUE_LENGTH_LIMIT`]: crate::EmbedBuilder::FIELD_VALUE_LENGTH_LIMIT
28    pub fn new(name: impl Into<String>, value: impl Into<String>) -> Self {
29        Self::_new(name.into(), value.into())
30    }
31
32    const fn _new(name: String, value: String) -> Self {
33        Self(EmbedField {
34            inline: false,
35            name,
36            value,
37        })
38    }
39
40    /// Build into an embed field.
41    #[allow(clippy::missing_const_for_fn)]
42    #[must_use = "should be used as part of an embed builder"]
43    pub fn build(self) -> EmbedField {
44        self.0
45    }
46
47    /// Inline the field.
48    ///
49    /// # Examples
50    ///
51    /// Create an inlined field:
52    ///
53    /// ```
54    /// use twilight_embed_builder::EmbedFieldBuilder;
55    ///
56    /// let field = EmbedFieldBuilder::new("twilight", "is cool")
57    ///     .inline()
58    ///     .build();
59    /// ```
60    pub const fn inline(mut self) -> Self {
61        self.0.inline = true;
62
63        self
64    }
65}
66
67impl From<EmbedFieldBuilder> for EmbedField {
68    /// Convert an embed field builder into an embed field.
69    ///
70    /// This is equivalent to calling [`EmbedFieldBuilder::build`].
71    fn from(builder: EmbedFieldBuilder) -> Self {
72        builder.build()
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use super::EmbedFieldBuilder;
79    use crate::{EmbedBuilder, EmbedErrorType};
80    use static_assertions::assert_impl_all;
81    use std::fmt::Debug;
82    use twilight_model::channel::embed::EmbedField;
83
84    assert_impl_all!(EmbedFieldBuilder: Clone, Debug, Eq, PartialEq, Send, Sync);
85    assert_impl_all!(EmbedField: From<EmbedFieldBuilder>);
86
87    #[test]
88    fn new_errors() {
89        assert!(matches!(
90            EmbedBuilder::new().field(EmbedFieldBuilder::new("", "a")).build().unwrap_err().kind(),
91            EmbedErrorType::FieldNameEmpty { name, value }
92            if name.is_empty() && value.len() == 1
93        ));
94        assert!(matches!(
95            EmbedBuilder::new().field(EmbedFieldBuilder::new("a".repeat(257), "a")).build().unwrap_err().kind(),
96            EmbedErrorType::FieldNameTooLong { name, value }
97            if name.len() == 257 && value.len() == 1
98        ));
99        assert!(matches!(
100            EmbedBuilder::new().field(EmbedFieldBuilder::new("a", "")).build().unwrap_err().kind(),
101            EmbedErrorType::FieldValueEmpty { name, value }
102            if name.len() == 1 && value.is_empty()
103        ));
104        assert!(matches!(
105            EmbedBuilder::new().field(EmbedFieldBuilder::new("a", "a".repeat(1025))).build().unwrap_err().kind(),
106            EmbedErrorType::FieldValueTooLong { name, value }
107            if name.len() == 1 && value.len() == 1025
108        ));
109    }
110
111    #[test]
112    fn builder_inline() {
113        let expected = EmbedField {
114            inline: true,
115            name: "name".to_owned(),
116            value: "value".to_owned(),
117        };
118        let actual = EmbedFieldBuilder::new("name", "value").inline().build();
119
120        assert_eq!(actual, expected);
121    }
122
123    #[test]
124    fn builder_no_inline() {
125        let expected = EmbedField {
126            inline: false,
127            name: "name".to_owned(),
128            value: "value".to_owned(),
129        };
130        let actual = EmbedFieldBuilder::new("name", "value").build();
131
132        assert_eq!(actual, expected);
133    }
134}