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
use super::image_source::ImageSource;
use twilight_model::channel::embed::EmbedFooter;
#[derive(Clone, Debug, Eq, PartialEq)]
#[must_use = "must be built into an embed footer"]
pub struct EmbedFooterBuilder(EmbedFooter);
impl EmbedFooterBuilder {
pub fn new(text: impl Into<String>) -> Self {
Self::_new(text.into())
}
const fn _new(text: String) -> Self {
Self(EmbedFooter {
icon_url: None,
proxy_icon_url: None,
text,
})
}
#[allow(clippy::missing_const_for_fn)]
#[must_use = "should be used as part of an embed builder"]
pub fn build(self) -> EmbedFooter {
self.0
}
pub fn icon_url(mut self, image_source: ImageSource) -> Self {
self.0.icon_url.replace(image_source.0);
self
}
}
impl From<EmbedFooterBuilder> for EmbedFooter {
fn from(builder: EmbedFooterBuilder) -> Self {
builder.build()
}
}
#[cfg(test)]
mod tests {
use super::EmbedFooterBuilder;
use crate::{EmbedBuilder, EmbedErrorType, ImageSource};
use static_assertions::assert_impl_all;
use std::fmt::Debug;
use twilight_model::channel::embed::EmbedFooter;
assert_impl_all!(EmbedFooterBuilder: Clone, Debug, Eq, PartialEq, Send, Sync);
assert_impl_all!(EmbedFooter: From<EmbedFooterBuilder>);
#[test]
fn test_text() {
assert!(matches!(
EmbedBuilder::new().footer(EmbedFooterBuilder::new("")).build().unwrap_err().kind(),
EmbedErrorType::FooterTextEmpty { text }
if text.is_empty()
));
let too_long_len = EmbedBuilder::FOOTER_TEXT_LENGTH_LIMIT + 1;
assert!(matches!(
EmbedBuilder::new().footer(EmbedFooterBuilder::new("a".repeat(too_long_len))).build().unwrap_err().kind(),
EmbedErrorType::FooterTextTooLong { text }
if text.len() == too_long_len
));
let expected = EmbedFooter {
icon_url: None,
proxy_icon_url: None,
text: "a footer".to_owned(),
};
let actual = EmbedFooterBuilder::new("a footer").build();
assert_eq!(actual, expected);
}
#[test]
fn test_builder() {
let expected = EmbedFooter {
icon_url: Some("https://example.com/1.png".to_owned()),
proxy_icon_url: None,
text: "a footer".to_owned(),
};
let image = ImageSource::url("https://example.com/1.png").unwrap();
let actual = EmbedFooterBuilder::new("a footer").icon_url(image).build();
assert_eq!(actual, expected);
}
}