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
use crate::models::Snowflake;
use serde::{Deserialize, Serialize};
/// Attachment in a message.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct MessageAttachment {
/// The attachment's ID
pub id: Option<Snowflake>,
/// The attachment's filename
pub filename: Option<String>,
/// The attachment's content type
pub content_type: Option<String>,
/// The attachment's size in bytes
pub size: Option<u64>,
/// The attachment's URL
pub url: Option<String>,
/// The attachment's width (for images)
pub width: Option<u32>,
/// The attachment's height (for images)
pub height: Option<u32>,
}
impl MessageAttachment {
/// Creates a new message attachment from API data.
pub fn from_data(data: serde_json::Value) -> Self {
serde_json::from_value(data).unwrap_or_default()
}
/// Returns true if this attachment is an image.
pub fn is_image(&self) -> bool {
self.content_type
.as_ref()
.is_some_and(|ct| ct.starts_with("image/"))
}
/// Returns true if this attachment is a video.
pub fn is_video(&self) -> bool {
self.content_type
.as_ref()
.is_some_and(|ct| ct.starts_with("video/"))
}
/// Returns true if this attachment is an audio file.
pub fn is_audio(&self) -> bool {
self.content_type
.as_ref()
.is_some_and(|ct| ct.starts_with("audio/"))
}
}