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
use std::marker::PhantomData;
/// A struct used for uploading images to the Api.
#[derive(Clone, Default, Serialize)]
pub struct UploadParams {
/// The kind of the image.
#[serde(rename = "baseType")]
pub base_type: String,
/// The image file data.
pub file: Vec<u8>,
/// Wheather the image is private.
pub hidden: Option<bool>,
/// Wheather the imsage is NSFW.
pub nsfw: Option<bool>,
/// The source of the image.
pub source: Option<String>,
/// The tags for the image.
pub tags: Option<Vec<String>>,
// Prevents manual userland instantiation of the struct, to avoid BC
// breaks if we add another field.
#[serde(skip)]
phantom: PhantomData<()>,
}
impl UploadParams {
/// Create a new [`UploadParams`] struct using all possible values.
///
/// [`UploadParams`]: struct.UploadParams.html
pub fn new(
base_type: String,
file: Vec<u8>,
hidden: Option<bool>,
nsfw: Option<bool>,
source: Option<String>,
tags: Option<Vec<String>>,
) -> Self {
Self {
phantom: PhantomData,
base_type,
file,
hidden,
nsfw,
source,
tags,
}
}
/// Create a new [`UploadParams`] struct using the bare minimum number of
/// values.
///
/// [`UploadParams`]: struct.UploadParams.html
pub fn simple (
base_type: String,
file: Vec<u8>,
) -> Self {
Self {
phantom: PhantomData,
base_type,
file,
hidden: None,
nsfw: None,
source: None,
tags: None,
}
}
pub(crate) fn into_data(self) -> (Vec<(&'static str, String)>, Vec<u8>) {
let mut data = vec![];
data.push(("base_type", self.base_type));
if let Some(hidden) = self.hidden {
data.push(("hidden", if hidden { "true" } else { "false" }.to_owned()));
}
if let Some(nsfw) = self.nsfw {
data.push(("nsfw", if nsfw { "true" } else { "false" }.to_owned()));
}
if let Some(source) = self.source {
data.push(("source", source));
}
if let Some(tags) = self.tags {
data.push(("tags", tags.join(",")));
}
(data, self.file)
}
}