titanium_model/builder/
embed.rs

1use crate::{Embed, EmbedAuthor, EmbedField, EmbedFooter, EmbedMedia, TitanString};
2
3/// Builder for creating an Embed.
4#[derive(Debug, Clone, Default)]
5#[must_use]
6pub struct EmbedBuilder<'a> {
7    embed: Embed<'a>,
8}
9
10impl<'a> EmbedBuilder<'a> {
11    /// Create a new `EmbedBuilder`.
12    #[inline]
13    pub fn new() -> Self {
14        Self::default()
15    }
16
17    /// Create a simple embed with title and description.
18    #[inline]
19    pub fn simple(
20        title: impl Into<TitanString<'a>>,
21        description: impl Into<TitanString<'a>>,
22    ) -> Self {
23        Self::new().title(title).description(description)
24    }
25
26    /// Create a success embed (green color).
27    #[inline]
28    pub fn success(
29        title: impl Into<TitanString<'a>>,
30        description: impl Into<TitanString<'a>>,
31    ) -> Self {
32        Self::simple(title, description).color(0x0057_F287)
33    }
34
35    /// Create an error embed (red color).
36    #[inline]
37    pub fn error(
38        title: impl Into<TitanString<'a>>,
39        description: impl Into<TitanString<'a>>,
40    ) -> Self {
41        Self::simple(title, description).color(0x00ED_4245)
42    }
43
44    /// Create an info embed (blurple color).
45    #[inline]
46    pub fn info(
47        title: impl Into<TitanString<'a>>,
48        description: impl Into<TitanString<'a>>,
49    ) -> Self {
50        Self::simple(title, description).color(0x0058_65F2)
51    }
52
53    /// Create a warning embed (yellow color).
54    #[inline]
55    pub fn warning(
56        title: impl Into<TitanString<'a>>,
57        description: impl Into<TitanString<'a>>,
58    ) -> Self {
59        Self::simple(title, description).color(0x00FE_E75C)
60    }
61
62    /// Set the title of the embed.
63    #[inline]
64    pub fn title(mut self, title: impl Into<TitanString<'a>>) -> Self {
65        self.embed.title = Some(title.into());
66        self
67    }
68
69    /// Set the description of the embed.
70    pub fn description(mut self, description: impl Into<TitanString<'a>>) -> Self {
71        self.embed.description = Some(description.into());
72        self
73    }
74
75    /// Set the URL of the embed.
76    pub fn url(mut self, url: impl Into<TitanString<'a>>) -> Self {
77        self.embed.url = Some(url.into());
78        self
79    }
80
81    /// Set the timestamp of the embed.
82    pub fn timestamp(mut self, timestamp: impl Into<TitanString<'a>>) -> Self {
83        self.embed.timestamp = Some(timestamp.into());
84        self
85    }
86
87    /// Set the color of the embed.
88    pub fn color(mut self, color: u32) -> Self {
89        self.embed.color = Some(color);
90        self
91    }
92
93    /// Set the color of the embed from RGB values.
94    pub fn color_rgb(mut self, r: u8, g: u8, b: u8) -> Self {
95        self.embed.color = Some((u32::from(r) << 16) | (u32::from(g) << 8) | u32::from(b));
96        self
97    }
98
99    /// Set the footer of the embed.
100    pub fn footer(
101        mut self,
102        text: impl Into<TitanString<'a>>,
103        icon_url: Option<impl Into<TitanString<'a>>>,
104    ) -> Self {
105        self.embed.footer = Some(EmbedFooter {
106            text: text.into(),
107            icon_url: icon_url.map(Into::into),
108            proxy_icon_url: None,
109        });
110        self
111    }
112
113    /// Set the image of the embed.
114    pub fn image(mut self, url: impl Into<TitanString<'a>>) -> Self {
115        self.embed.image = Some(EmbedMedia {
116            url: Some(url.into()),
117            proxy_url: None,
118            height: None,
119            width: None,
120        });
121        self
122    }
123
124    /// Set the thumbnail of the embed.
125    pub fn thumbnail(mut self, url: impl Into<TitanString<'a>>) -> Self {
126        self.embed.thumbnail = Some(EmbedMedia {
127            url: Some(url.into()),
128            proxy_url: None,
129            height: None,
130            width: None,
131        });
132        self
133    }
134
135    /// Set the author of the embed.
136    pub fn author(
137        mut self,
138        name: impl Into<TitanString<'a>>,
139        url: Option<impl Into<TitanString<'a>>>,
140        icon_url: Option<impl Into<TitanString<'a>>>,
141    ) -> Self {
142        self.embed.author = Some(EmbedAuthor {
143            name: name.into(),
144            url: url.map(Into::into),
145            icon_url: icon_url.map(Into::into),
146            proxy_icon_url: None,
147        });
148        self
149    }
150
151    /// Add a field to the embed.
152    pub fn field(
153        mut self,
154        name: impl Into<TitanString<'a>>,
155        value: impl Into<TitanString<'a>>,
156        inline: bool,
157    ) -> Self {
158        self.embed.fields.push(EmbedField {
159            name: name.into(),
160            value: value.into(),
161            inline,
162        });
163        self
164    }
165
166    /// Add an inline field.
167    pub fn field_inline(
168        self,
169        name: impl Into<TitanString<'a>>,
170        value: impl Into<TitanString<'a>>,
171    ) -> Self {
172        self.field(name, value, true)
173    }
174
175    /// Add a block field (not inline).
176    pub fn field_block(
177        self,
178        name: impl Into<TitanString<'a>>,
179        value: impl Into<TitanString<'a>>,
180    ) -> Self {
181        self.field(name, value, false)
182    }
183
184    /// Build the Embed.
185    #[must_use]
186    pub fn build(self) -> Embed<'a> {
187        self.embed
188    }
189}
190
191/// `EmbedBuilder` automatically converts to Embed
192impl<'a> From<EmbedBuilder<'a>> for Embed<'a> {
193    #[inline]
194    fn from(builder: EmbedBuilder<'a>) -> Self {
195        builder.build()
196    }
197}
198
199/// &str automatically converts to Embed (simple text embed)
200impl<'a> From<&'a str> for Embed<'a> {
201    fn from(text: &'a str) -> Self {
202        EmbedBuilder::new().description(text).build()
203    }
204}