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(name: String) -> Self {
Self(EmbedAuthor {
icon_url: None,
name,
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())
}
#[allow(clippy::missing_const_for_fn)]
fn _name(mut self, name: String) -> Self {
self.0.name = 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 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, Eq, PartialEq, Send, Sync);
assert_impl_all!(EmbedAuthor: From<EmbedAuthorBuilder>);
#[test]
fn name_empty() {
let builder = EmbedBuilder::new().author(EmbedAuthorBuilder::new("".to_owned()));
assert!(matches!(
builder.build().unwrap_err().kind(),
EmbedErrorType::AuthorNameEmpty { .. }
));
}
#[test]
fn name_too_long() {
let builder = EmbedBuilder::new().author(EmbedAuthorBuilder::new("a".repeat(256)));
assert!(builder.build().is_ok());
let builder = EmbedBuilder::new().author(EmbedAuthorBuilder::new("a".repeat(257)));
assert!(matches!(
builder.build().unwrap_err().kind(),
EmbedErrorType::AuthorNameTooLong { .. }
));
}
#[test]
fn builder() {
let expected = EmbedAuthor {
icon_url: Some("https://example.com/1.png".to_owned()),
name: "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("an author".to_owned())
.icon_url(source)
.url("https://example.com")
.build();
assert_eq!(actual, expected);
}
}