1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use super::image_source::ImageSource;
use twilight_model::channel::embed::EmbedAuthor;
#[derive(Clone, Debug, Eq, PartialEq)]
#[must_use = "must be built into an embed author"]
pub struct EmbedAuthorBuilder(EmbedAuthor);
impl EmbedAuthorBuilder {
pub const fn new() -> Self {
Self(EmbedAuthor {
icon_url: None,
name: None,
proxy_icon_url: None,
url: None,
})
}
#[allow(clippy::missing_const_for_fn)]
#[must_use = "should be used as part of an embed builder"]
pub fn build(self) -> EmbedAuthor {
self.0
}
pub fn icon_url(mut self, image_source: ImageSource) -> Self {
self.0.icon_url.replace(image_source.0);
self
}
pub fn name(self, name: impl Into<String>) -> Self {
self._name(name.into())
}
fn _name(mut self, name: String) -> Self {
self.0.name.replace(name);
self
}
pub fn url(self, url: impl Into<String>) -> Self {
self._url(url.into())
}
fn _url(mut self, url: String) -> Self {
self.0.url.replace(url);
self
}
}
impl Default for EmbedAuthorBuilder {
fn default() -> Self {
Self::new()
}
}
impl From<EmbedAuthorBuilder> for EmbedAuthor {
fn from(builder: EmbedAuthorBuilder) -> Self {
builder.build()
}
}
#[cfg(test)]
mod tests {
use super::EmbedAuthorBuilder;
use crate::{EmbedBuilder, EmbedErrorType, ImageSource};
use static_assertions::assert_impl_all;
use std::fmt::Debug;
use twilight_model::channel::embed::EmbedAuthor;
assert_impl_all!(
EmbedAuthorBuilder: Clone,
Debug,
Default,
Eq,
PartialEq,
Send,
Sync
);
assert_impl_all!(EmbedAuthor: From<EmbedAuthorBuilder>);
#[test]
fn test_defaults() {
let expected = EmbedAuthor {
icon_url: None,
name: None,
proxy_icon_url: None,
url: None,
};
assert_eq!(expected, EmbedAuthorBuilder::new().0);
assert_eq!(EmbedAuthorBuilder::new().0, EmbedAuthorBuilder::default().0);
}
#[test]
fn test_name_empty() {
let builder = EmbedBuilder::new().author(EmbedAuthorBuilder::new().name(""));
assert!(matches!(
builder.build().unwrap_err().kind(),
EmbedErrorType::AuthorNameEmpty { .. }
));
}
#[test]
fn test_name_too_long() {
let builder = EmbedBuilder::new().author(EmbedAuthorBuilder::new().name("a".repeat(256)));
assert!(builder.build().is_ok());
let builder = EmbedBuilder::new().author(EmbedAuthorBuilder::new().name("a".repeat(257)));
assert!(matches!(
builder.build().unwrap_err().kind(),
EmbedErrorType::AuthorNameTooLong { .. }
));
}
#[test]
fn test_builder() {
let expected = EmbedAuthor {
icon_url: Some("https://example.com/1.png".to_owned()),
name: Some("an author".to_owned()),
proxy_icon_url: None,
url: Some("https://example.com".to_owned()),
};
let source = ImageSource::url("https://example.com/1.png").unwrap();
let actual = EmbedAuthorBuilder::new()
.icon_url(source)
.name("an author")
.url("https://example.com")
.build();
assert_eq!(actual, expected);
}
}