discord_webhook2/message/embed/
mod.rs1use iso8061_timestamp::Timestamp;
2use serde::{Deserialize, Serialize};
3
4use crate::message::embed::author::EmbedAuthor;
5use crate::message::embed::field::EmbedField;
6use crate::message::embed::footer::EmbedFooter;
7use crate::message::embed::image::EmbedImage;
8use crate::message::embed::provider::EmbedProvider;
9use crate::message::embed::thumbnail::EmbedThumbnail;
10use crate::message::embed::video::EmbedVideo;
11
12pub mod author;
13pub mod field;
14pub mod footer;
15pub mod image;
16pub mod provider;
17pub mod thumbnail;
18pub mod video;
19
20#[derive(Serialize, Deserialize, Debug, Clone)]
21pub struct Embed {
22 pub title: Option<String>,
23 pub description: Option<String>,
24 pub url: Option<String>,
25 pub timestamp: Option<Timestamp>,
26 pub color: Option<u32>,
27 pub footer: Option<EmbedFooter>,
28 pub image: Option<EmbedImage>,
29 pub thumbnail: Option<EmbedThumbnail>,
30 pub video: Option<EmbedVideo>,
31 pub provider: Option<EmbedProvider>,
32 pub author: Option<EmbedAuthor>,
33 pub fields: Option<Vec<EmbedField>>,
34}
35
36impl Embed {
37 pub fn new() -> Self {
38 Self {
39 title: None,
40 description: None,
41 url: None,
42 timestamp: None,
43 color: None,
44 footer: None,
45 image: None,
46 thumbnail: None,
47 video: None,
48 provider: None,
49 author: None,
50 fields: None,
51 }
52 }
53
54 pub fn title(mut self, title: impl Into<String>) -> Self {
55 self.title = Some(title.into());
56 self
57 }
58
59 pub fn description(mut self, description: impl Into<String>) -> Self {
60 self.description = Some(description.into());
61 self
62 }
63
64 pub fn url(mut self, url: impl Into<String>) -> Self {
65 self.url = Some(url.into());
66 self
67 }
68
69 pub fn timestamp(mut self, timestamp: Timestamp) -> Self {
70 self.timestamp = Some(timestamp);
71 self
72 }
73
74 pub fn color(mut self, color: u32) -> Self {
75 self.color = Some(color);
76 self
77 }
78
79 pub fn footer<Func>(mut self, function: Func) -> Self
80 where
81 Func: FnOnce(EmbedFooter) -> EmbedFooter,
82 {
83 let footer = function(EmbedFooter::new());
84
85 self.footer = Some(footer);
86 self
87 }
88
89 pub fn image<Func>(mut self, function: Func) -> Self
90 where
91 Func: FnOnce(EmbedImage) -> EmbedImage,
92 {
93 let image = function(EmbedImage::new());
94
95 self.image = Some(image);
96 self
97 }
98
99 pub fn thumbnail<Func>(mut self, function: Func) -> Self
100 where
101 Func: FnOnce(EmbedThumbnail) -> EmbedThumbnail,
102 {
103 let thumbnail = function(EmbedThumbnail::new());
104
105 self.thumbnail = Some(thumbnail);
106 self
107 }
108
109 pub fn video<Func>(mut self, function: Func) -> Self
110 where
111 Func: FnOnce(EmbedVideo) -> EmbedVideo,
112 {
113 let video = function(EmbedVideo::new());
114
115 self.video = Some(video);
116 self
117 }
118
119 pub fn provider<Func>(mut self, function: Func) -> Self
120 where
121 Func: FnOnce(EmbedProvider) -> EmbedProvider,
122 {
123 let provider = function(EmbedProvider::new());
124
125 self.provider = Some(provider);
126 self
127 }
128
129 pub fn author<Func>(mut self, function: Func) -> Self
130 where
131 Func: FnOnce(EmbedAuthor) -> EmbedAuthor,
132 {
133 let author = function(EmbedAuthor::new());
134
135 self.author = Some(author);
136 self
137 }
138
139 pub fn field<Func>(mut self, function: Func) -> Self
140 where
141 Func: FnOnce(EmbedField) -> EmbedField,
142 {
143 let field = function(EmbedField::new());
144
145 match self.fields {
146 None => self.fields = Some(vec![field]),
147 Some(ref mut v) => v.push(field),
148 }
149 self
150 }
151}
152
153impl Default for Embed {
154 fn default() -> Self {
155 Self::new()
156 }
157}