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
use std::marker::PhantomData;

/// A struct containing the image parameters which are used for the random image
/// search of the relevant client implementation.
#[derive(Default, Serialize)]
pub struct ImageParams {
    /// The file type of the image you would like to recieve.
    #[serde(rename = "filetype")]
    pub file_type: Option<String>,
    /// Wheather to include hidden images in your random image search.
    pub hidden: Option<bool>,
    /// The kind of image you would like to recieve; hug, pat, ect.
    #[serde(rename = "type")]
    pub kind: Option<String>,
    /// Wheather the returned image should be NSFW or not.
    pub nsfw: Option<bool>,
    /// The tags you would like the returned image to have.
    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 ImageParams {
    /// Used for creating a full [`ImageParams`] struct to pass to the
    /// relevant request function.
    ///
    /// [`ImageParams`]: struct.ImageParams.html
    pub fn new(
        file_type: Option<String>,
        hidden: Option<bool>,
        kind: Option<String>,
        nsfw: Option<bool>,
        tags: Option<Vec<String>>,
    ) -> Self {
        Self {
            phantom: PhantomData,
            hidden,
            file_type,
            kind,
            nsfw,
            tags,
        }
    }

    /// Used to create an [`ImageParams`] struct with just the `kind` value
    /// filled using the passed value.
    ///
    /// [`ImageParams`]: struct.ImageParams.html
    pub fn kind<S: Into<String>>(kind: S) -> Self {
        Self {
            kind: Some(kind.into()),
            ..Default::default()
        }
    }

    /// Used to create an [`ImageParams`] struct with just the `tags` value
    /// filled using the passed value.
    ///
    /// [`ImageParams`]: struct.ImageParams.html
    pub fn tags<S: Into<String>>(tags: Vec<S>) -> Self {
        Self {
            tags: Some(tags.into_iter().map(|x| x.into()).collect()),
            ..Default::default()
        }
    }

    pub(crate) fn into_data(self) -> Vec<(&'static str, String)> {
        let mut data = vec![];

        if let Some(file_type) = self.file_type {
            data.push(("file_type", file_type));
        }

        if let Some(hidden) = self.hidden {
            data.push(("hidden", if hidden { "true" } else { "false" }.to_owned()));
        }


        if let Some(kind) = self.kind {
            data.push(("type", kind));
        }

        if let Some(nsfw) = self.nsfw {
            data.push(("nsfw", if nsfw { "true" } else { "false" }.to_owned()));
        }

        if let Some(tags) = self.tags {
            data.push(("tags", tags.join(",")));
        }

        data
    }
}