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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//! Create embed authors.

use super::image_source::ImageSource;
use std::{
    error::Error,
    fmt::{Display, Formatter, Result as FmtResult},
};
use twilight_model::channel::embed::EmbedAuthor;

/// Error setting an embed author's name.
///
/// This is returned from [`EmbedAuthorBuilder::name`].
///
/// [`EmbedAuthorBuilder::name`]: struct.EmbedAuthorBuilder.html#method.name
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum EmbedAuthorNameError {
    /// Name is empty.
    Empty {
        /// Provided name. Although empty, the same owned allocation is
        /// included.
        name: String,
    },
    /// Name is longer than 256 UTF-16 code points.
    TooLong {
        /// Provided name.
        name: String,
    },
}

impl Display for EmbedAuthorNameError {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        match self {
            Self::Empty { .. } => f.write_str("the author name is empty"),
            Self::TooLong { .. } => f.write_str("the author name is too long"),
        }
    }
}

impl Error for EmbedAuthorNameError {}

/// Create an embed author with a builder.
///
/// This can be passed into [`EmbedBuilder::author`].
///
/// [`EmbedBuilder::author`]: ../builder/struct.EmbedBuilder.html#method.author
#[derive(Clone, Debug, Eq, PartialEq)]
#[must_use = "must be built into an embed author"]
pub struct EmbedAuthorBuilder(EmbedAuthor);

impl EmbedAuthorBuilder {
    /// The maximum number of UTF-16 code points that can be in an author name.
    ///
    /// This is used by [`name`].
    ///
    /// [`name`]: #method.name
    pub const NAME_LENGTH_LIMIT: usize = 256;

    /// Create a new default embed author builder.
    pub fn new() -> Self {
        Self::default()
    }

    /// Build into an embed author.
    #[must_use = "should be used as part of an embed builder"]
    pub fn build(self) -> EmbedAuthor {
        self.0
    }

    /// Add an author icon.
    pub fn icon_url(mut self, image_source: ImageSource) -> Self {
        self.0.icon_url.replace(image_source.0);

        self
    }

    /// The author's name.
    ///
    /// Refer to [`NAME_LENGTH_LIMIT`] for the maximum number of UTF-16
    /// code points that can be in a description.
    ///
    /// # Errors
    ///
    /// Returns [`EmbedAuthorNameError::Empty`] if the provided name is empty.
    ///
    /// Returns [`EmbedAuthorNameError::TooLong`] if the provided name is longer
    /// than the maximum number of code points.
    ///
    /// [`NAME_LENGTH_LIMIT`]: #const.NAME_LENGTH_LIMIT
    /// [`EmbedAuthorNameError::Empty`]: enum.EmbedAuthorNameError.html#variant.Empty
    /// [`EmbedAuthorNameError::TooLong`]: enum.EmbedAuthorNameError.html#variant.TooLong
    pub fn name(self, name: impl Into<String>) -> Result<Self, EmbedAuthorNameError> {
        self._name(name.into())
    }

    fn _name(mut self, name: String) -> Result<Self, EmbedAuthorNameError> {
        if name.is_empty() {
            return Err(EmbedAuthorNameError::Empty { name });
        }

        if name.chars().count() > Self::NAME_LENGTH_LIMIT {
            return Err(EmbedAuthorNameError::TooLong { name });
        }

        self.0.name.replace(name);

        Ok(self)
    }

    /// The author's url.
    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(EmbedAuthor {
            icon_url: None,
            name: None,
            proxy_icon_url: None,
            url: None,
        })
    }
}

impl From<EmbedAuthorBuilder> for EmbedAuthor {
    /// Convert an embed author builder into an embed author.
    ///
    /// This is equivalent to calling [`EmbedAuthorBuilder::build`].
    ///
    /// [`EmbedAuthorBuilder::build`]: #method.build
    fn from(builder: EmbedAuthorBuilder) -> Self {
        builder.build()
    }
}

#[cfg(test)]
mod tests {
    use super::{EmbedAuthorBuilder, EmbedAuthorNameError};
    use crate::ImageSource;
    use static_assertions::{assert_fields, assert_impl_all, const_assert};
    use std::{error::Error, fmt::Debug};
    use twilight_model::channel::embed::EmbedAuthor;

    assert_impl_all!(
        EmbedAuthorNameError: Clone,
        Debug,
        Error,
        Eq,
        PartialEq,
        Send,
        Sync
    );
    assert_fields!(EmbedAuthorNameError::Empty: name);
    assert_fields!(EmbedAuthorNameError::TooLong: name);
    assert_impl_all!(
        EmbedAuthorBuilder: Clone,
        Debug,
        Default,
        Eq,
        PartialEq,
        Send,
        Sync
    );
    const_assert!(EmbedAuthorBuilder::NAME_LENGTH_LIMIT == 256);
    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() {
        assert!(matches!(
            EmbedAuthorBuilder::new().name(""),
            Err(EmbedAuthorNameError::Empty { .. })
        ));
    }

    #[test]
    fn test_name_too_long() {
        assert!(EmbedAuthorBuilder::new().name("a".repeat(256)).is_ok());
        assert!(matches!(
            EmbedAuthorBuilder::new().name("a".repeat(257)),
            Err(EmbedAuthorNameError::TooLong { .. })
        ));
    }

    #[test]
    fn test_builder() -> Result<(), Box<dyn Error>> {
        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")?;
        let actual = EmbedAuthorBuilder::new()
            .icon_url(source)
            .name("an author")?
            .url("https://example.com")
            .build();

        assert_eq!(actual, expected);

        Ok(())
    }
}