twilight_validate/
message.rs

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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//! Constants, error types, and functions for validating [`Message`] fields.
//!
//! [`Message`]: twilight_model::channel::Message

use crate::{
    component::{ComponentValidationErrorType, COMPONENT_COUNT},
    embed::{chars as embed_chars, EmbedValidationErrorType, EMBED_TOTAL_LENGTH},
    request::ValidationError,
};
use std::{
    error::Error,
    fmt::{Display, Formatter, Result as FmtResult},
};
use twilight_model::{
    channel::message::{Component, Embed},
    http::attachment::Attachment,
    id::{marker::StickerMarker, Id},
};

/// Maximum length of an attachment's description.
pub const ATTACHMENT_DESCIPTION_LENGTH_MAX: usize = 1024;

/// Maximum number of embeds that a message may have.
pub const EMBED_COUNT_LIMIT: usize = 10;

/// Maximum length of message content.
pub const MESSAGE_CONTENT_LENGTH_MAX: usize = 2000;

/// Maximum amount of stickers.
pub const STICKER_MAX: usize = 3;

/// ASCII dash.
const DASH: char = '-';

/// ASCII dot.
const DOT: char = '.';

/// ASCII underscore.
const UNDERSCORE: char = '_';

/// A message is not valid.
#[derive(Debug)]
pub struct MessageValidationError {
    /// Type of error that occurred.
    kind: MessageValidationErrorType,
    /// Source of the error, if any.
    source: Option<Box<dyn Error + Send + Sync>>,
}

impl MessageValidationError {
    /// Immutable reference to the type of error that occurred.
    #[must_use = "retrieving the type has no effect if left unused"]
    pub const fn kind(&self) -> &MessageValidationErrorType {
        &self.kind
    }

    /// Consume the error, returning the source error if there is any.
    #[must_use = "consuming the error and retrieving the source has no effect if left unused"]
    pub fn into_source(self) -> Option<Box<dyn Error + Send + Sync>> {
        self.source
    }

    /// Consume the error, returning the owned error type and the source error.
    #[must_use = "consuming the error into its parts has no effect if left unused"]
    pub fn into_parts(
        self,
    ) -> (
        MessageValidationErrorType,
        Option<Box<dyn Error + Send + Sync>>,
    ) {
        (self.kind, self.source)
    }

    /// Create a [`MessageValidationError`] from a [`ValidationError`].
    #[must_use = "has no effect if unused"]
    pub fn from_validation_error(
        kind: MessageValidationErrorType,
        source: ValidationError,
    ) -> Self {
        Self {
            kind,
            source: Some(Box::new(source)),
        }
    }
}

impl Display for MessageValidationError {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        match &self.kind {
            MessageValidationErrorType::AttachmentDescriptionTooLarge { chars } => {
                f.write_str("the attachment description is ")?;
                Display::fmt(chars, f)?;
                f.write_str(" characters long, but the max is ")?;

                Display::fmt(&ATTACHMENT_DESCIPTION_LENGTH_MAX, f)
            }
            MessageValidationErrorType::AttachmentFilename { filename } => {
                f.write_str("attachment filename `")?;
                Display::fmt(filename, f)?;

                f.write_str("`is invalid")
            }
            MessageValidationErrorType::ComponentCount { count } => {
                Display::fmt(count, f)?;
                f.write_str(" components were provided, but only ")?;
                Display::fmt(&COMPONENT_COUNT, f)?;

                f.write_str(" root components are allowed")
            }
            MessageValidationErrorType::ComponentInvalid { .. } => {
                f.write_str("a provided component is invalid")
            }
            MessageValidationErrorType::ContentInvalid => f.write_str("message content is invalid"),
            MessageValidationErrorType::EmbedInvalid { idx, .. } => {
                f.write_str("embed at index ")?;
                Display::fmt(idx, f)?;

                f.write_str(" is invalid")
            }
            MessageValidationErrorType::StickersInvalid { len } => {
                f.write_str("amount of stickers provided is ")?;
                Display::fmt(len, f)?;
                f.write_str(" but it must be at most ")?;

                Display::fmt(&STICKER_MAX, f)
            }
            MessageValidationErrorType::TooManyEmbeds { .. } => {
                f.write_str("message has too many embeds")
            }
            MessageValidationErrorType::WebhookUsername { .. } => {
                if let Some(source) = self.source() {
                    Display::fmt(&source, f)
                } else {
                    f.write_str("webhook username is invalid")
                }
            }
        }
    }
}

impl Error for MessageValidationError {}

/// Type of [`MessageValidationError`] that occurred.
#[derive(Debug)]
pub enum MessageValidationErrorType {
    /// Attachment filename is not valid.
    AttachmentFilename {
        /// Invalid filename.
        filename: String,
    },
    /// Attachment description is too large.
    AttachmentDescriptionTooLarge {
        /// Provided number of codepoints.
        chars: usize,
    },
    /// Too many message components were provided.
    ComponentCount {
        /// Number of components that were provided.
        count: usize,
    },
    /// An invalid message component was provided.
    ComponentInvalid {
        /// Index of the component.
        idx: usize,
        /// Additional details about the validation failure type.
        kind: ComponentValidationErrorType,
    },
    /// Returned when the content is over 2000 UTF-16 characters.
    ContentInvalid,
    /// Returned when the embed is invalid.
    EmbedInvalid {
        /// Index of the embed.
        idx: usize,
        /// Additional details about the validation failure type.
        kind: EmbedValidationErrorType,
    },
    /// Amount of stickers provided is invalid.
    StickersInvalid {
        /// Invalid length.
        len: usize,
    },
    /// Too many embeds were provided.
    ///
    /// A followup message can have up to 10 embeds.
    TooManyEmbeds,
    /// Provided webhook username was invalid.
    WebhookUsername,
}

/// Ensure an attachment is correct.
///
/// # Errors
///
/// Returns an error of type [`AttachmentDescriptionTooLarge`] if
/// the attachments's description is too large.
///
/// Returns an error of type [`AttachmentFilename`] if the
/// filename is invalid.
///
/// [`AttachmentDescriptionTooLarge`]: MessageValidationErrorType::AttachmentDescriptionTooLarge
/// [`AttachmentFilename`]: MessageValidationErrorType::AttachmentFilename
pub fn attachment(attachment: &Attachment) -> Result<(), MessageValidationError> {
    attachment_filename(&attachment.filename)?;

    if let Some(description) = &attachment.description {
        attachment_description(description)?;
    }

    Ok(())
}

/// Ensure an attachment's description is correct.
///
/// # Errors
///
/// Returns an error of type [`AttachmentDescriptionTooLarge`] if
/// the attachment's description is too large.
///
/// [`AttachmentDescriptionTooLarge`]: MessageValidationErrorType::AttachmentDescriptionTooLarge
pub fn attachment_description(description: impl AsRef<str>) -> Result<(), MessageValidationError> {
    let chars = description.as_ref().chars().count();
    if chars <= ATTACHMENT_DESCIPTION_LENGTH_MAX {
        Ok(())
    } else {
        Err(MessageValidationError {
            kind: MessageValidationErrorType::AttachmentDescriptionTooLarge { chars },
            source: None,
        })
    }
}

/// Ensure an attachment's description is correct.
///
/// The filename can contain ASCII alphanumeric characters, dots, dashes, and
/// underscores.
///
/// # Errors
///
/// Returns an error of type [`AttachmentFilename`] if the filename is invalid.
///
/// [`AttachmentFilename`]: MessageValidationErrorType::AttachmentFilename
pub fn attachment_filename(filename: impl AsRef<str>) -> Result<(), MessageValidationError> {
    if filename
        .as_ref()
        .chars()
        .all(|c| (c.is_ascii_alphanumeric() || c == DOT || c == DASH || c == UNDERSCORE))
    {
        Ok(())
    } else {
        Err(MessageValidationError {
            kind: MessageValidationErrorType::AttachmentFilename {
                filename: filename.as_ref().to_string(),
            },
            source: None,
        })
    }
}

/// Ensure a list of components is correct.
///
/// # Errors
///
/// Returns a [`ComponentValidationErrorType::ComponentCount`] if there are
/// too many components in the provided list.
///
/// Refer to the errors section of [`component`] for a list of errors that may
/// be returned as a result of validating each provided component.
///
/// [`component`]: crate::component::component
pub fn components(components: &[Component]) -> Result<(), MessageValidationError> {
    let count = components.len();

    if count > COMPONENT_COUNT {
        Err(MessageValidationError {
            kind: MessageValidationErrorType::ComponentCount { count },
            source: None,
        })
    } else {
        for (idx, component) in components.iter().enumerate() {
            crate::component::component(component).map_err(|source| {
                let (kind, source) = source.into_parts();

                MessageValidationError {
                    kind: MessageValidationErrorType::ComponentInvalid { idx, kind },
                    source,
                }
            })?;
        }

        Ok(())
    }
}

/// Ensure a message's content is correct.
///
/// # Errors
///
/// Returns an error of type [`ContentInvalid`] if the message's content is
/// invalid.
///
/// [`ContentInvalid`]: MessageValidationErrorType::ContentInvalid
pub fn content(value: impl AsRef<str>) -> Result<(), MessageValidationError> {
    // <https://discordapp.com/developers/docs/resources/channel#create-message-params>
    if value.as_ref().chars().count() <= MESSAGE_CONTENT_LENGTH_MAX {
        Ok(())
    } else {
        Err(MessageValidationError {
            kind: MessageValidationErrorType::ContentInvalid,
            source: None,
        })
    }
}

/// Ensure a list of embeds is correct.
///
/// # Errors
///
/// Returns an error of type [`TooManyEmbeds`] if there are too many embeds.
///
/// Otherwise, refer to the errors section of [`embed`] for a list of errors
/// that may occur.
///
/// [`TooManyEmbeds`]: MessageValidationErrorType::TooManyEmbeds
/// [`embed`]: crate::embed::embed
pub fn embeds(embeds: &[Embed]) -> Result<(), MessageValidationError> {
    if embeds.len() > EMBED_COUNT_LIMIT {
        Err(MessageValidationError {
            kind: MessageValidationErrorType::TooManyEmbeds,
            source: None,
        })
    } else {
        let mut chars = 0;
        for (idx, embed) in embeds.iter().enumerate() {
            chars += embed_chars(embed);

            if chars > EMBED_TOTAL_LENGTH {
                return Err(MessageValidationError {
                    kind: MessageValidationErrorType::EmbedInvalid {
                        idx,
                        kind: EmbedValidationErrorType::EmbedTooLarge { chars },
                    },
                    source: None,
                });
            }

            crate::embed::embed(embed).map_err(|source| {
                let (kind, source) = source.into_parts();

                MessageValidationError {
                    kind: MessageValidationErrorType::EmbedInvalid { idx, kind },
                    source,
                }
            })?;
        }

        Ok(())
    }
}

/// Ensure that the amount of stickers in a message is correct.
///
/// There must be at most [`STICKER_MAX`] stickers. This is based on [this
/// documentation entry].
///
/// # Errors
///
/// Returns an error of type [`StickersInvalid`] if the length is invalid.
///
/// [`StickersInvalid`]: MessageValidationErrorType::StickersInvalid
/// [this documentation entry]: https://discord.com/developers/docs/resources/channel#create-message-jsonform-params
pub fn sticker_ids(sticker_ids: &[Id<StickerMarker>]) -> Result<(), MessageValidationError> {
    let len = sticker_ids.len();

    if len <= STICKER_MAX {
        Ok(())
    } else {
        Err(MessageValidationError {
            kind: MessageValidationErrorType::StickersInvalid { len },
            source: None,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn attachment_description_limit() {
        assert!(attachment_description("").is_ok());
        assert!(attachment_description(str::repeat("a", 1024)).is_ok());

        assert!(matches!(
            attachment_description(str::repeat("a", 1025))
                .unwrap_err()
                .kind(),
            MessageValidationErrorType::AttachmentDescriptionTooLarge { chars: 1025 }
        ));
    }

    #[test]
    fn attachment_allowed_filename() {
        assert!(attachment_filename("one.jpg").is_ok());
        assert!(attachment_filename("two.png").is_ok());
        assert!(attachment_filename("three.gif").is_ok());
        assert!(attachment_filename(".dots-dashes_underscores.gif").is_ok());

        assert!(attachment_filename("????????").is_err());
    }

    #[test]
    fn content_length() {
        assert!(content("").is_ok());
        assert!(content("a".repeat(2000)).is_ok());

        assert!(content("a".repeat(2001)).is_err());
    }
}