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 #[allow(clippy::missing_const_for_fn)]
38 #[must_use = "should be used as part of an embed builder"]
39 pub fn build(self) -> EmbedField {
40 self.0
41 }
42
43 /// Inline the field.
44 ///
45 /// # Examples
46 ///
47 /// Create an inlined field:
48 ///
49 /// ```no_run
50 /// use twilight_util::builder::embed::EmbedFieldBuilder;
51 ///
52 /// let field = EmbedFieldBuilder::new("twilight", "is cool")
53 /// .inline()
54 /// .build();
55 /// ```
56 pub const fn inline(mut self) -> Self {
57 self.0.inline = true;
58
59 self
60 }
61}
62
63impl From<EmbedFieldBuilder> for EmbedField {
64 /// Convert an embed field builder into an embed field.
65 ///
66 /// This is equivalent to calling [`EmbedFieldBuilder::build`].
67 fn from(builder: EmbedFieldBuilder) -> Self {
68 builder.build()
69 }
70}
71
72#[cfg(test)]
73mod tests {
74 use super::*;
75 use static_assertions::assert_impl_all;
76 use std::fmt::Debug;
77
78 assert_impl_all!(EmbedFieldBuilder: Clone, Debug, Eq, PartialEq, Send, Sync);
79 assert_impl_all!(EmbedField: From<EmbedFieldBuilder>);
80
81 #[test]
82 fn builder_inline() {
83 let expected = EmbedField {
84 inline: true,
85 name: "name".to_owned(),
86 value: "value".to_owned(),
87 };
88 let actual = EmbedFieldBuilder::new("name", "value").inline().build();
89
90 assert_eq!(actual, expected);
91 }
92
93 #[test]
94 fn builder_no_inline() {
95 let expected = EmbedField {
96 inline: false,
97 name: "name".to_owned(),
98 value: "value".to_owned(),
99 };
100 let actual = EmbedFieldBuilder::new("name", "value").build();
101
102 assert_eq!(actual, expected);
103 }
104}