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
//! Module containing everything related to media attachements.
use serde::{Deserialize, Deserializer};
use super::Empty;
/// A struct representing a media attachment.
#[derive(Debug, Clone, Deserialize)]
pub struct Attachment {
/// ID of the attachment.
pub id: String,
/// The media type of an attachment.
#[serde(rename="type")]
pub media_type: MediaType,
/// URL of the locally hosted version of the image.
pub url: String,
/// For remote images, the remote URL of the original image.
pub remote_url: Option<String>,
/// URL of the preview image.
pub preview_url: String,
/// Shorter URL for the image, for insertion into text
/// (only present on local images)
pub text_url: Option<String>,
/// Meta information about the attachment.
#[serde(deserialize_with="empty_as_none")]
pub meta: Option<Meta>,
/// Noop will be removed.
pub description: Option<String>,
}
fn empty_as_none<'de, D: Deserializer<'de>>(val: D)
-> Result<Option<Meta>, D::Error>
{
#[derive(Deserialize)]
#[serde(untagged)]
enum EmptyOrMeta {
Empty(Empty),
Meta(Meta),
}
Ok(match EmptyOrMeta::deserialize(val)? {
EmptyOrMeta::Empty(_) => None,
EmptyOrMeta::Meta(m) => Some(m),
})
}
/// Information about the attachment itself.
#[derive(Debug, Deserialize, Clone)]
pub struct Meta {
/// Original version.
original: ImageDetails,
/// Smaller version.
small: ImageDetails,
}
/// Dimensions of an attachement.
#[derive(Debug, Deserialize, Clone)]
pub struct ImageDetails {
/// width of attachment.
width: u64,
/// height of attachment.
height: u64,
/// A string of `widthxheight`.
size: String,
/// The aspect ratio of the attachment.
aspect: f64,
}
/// The type of media attachment.
#[derive(Debug, Deserialize, Clone, Copy)]
pub enum MediaType {
/// An image.
#[serde(rename = "image")]
Image,
/// A video file.
#[serde(rename = "video")]
Video,
/// A gifv format file.
#[serde(rename = "gifv")]
Gifv,
/// Unknown format.
#[serde(rename = "unknown")]
Unknown,
}