summaryrefslogtreecommitdiffstats
path: root/src/requests/directory.rs
blob: 14ff2c4f3367c8c32a851228feb71d0471f737d0 (plain)
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
use crate::errors::Error;
use serde::Serialize;
use std::borrow::Cow;

mod bool_qs_serialize {
    use serde::Serializer;

    pub fn is_false(b: &bool) -> bool {
        !*b
    }

    pub fn serialize<S: Serializer>(b: &bool, s: S) -> Result<S::Ok, S::Error> {
        if *b {
            s.serialize_i64(1)
        } else {
            s.serialize_i64(0)
        }
    }
}

/// Represents the options for the directory request
#[derive(Debug, Clone, Default, PartialEq, Serialize)]
pub struct DirectoryRequest<'a> {
    offset: Option<usize>,
    limit: Option<usize>,
    order: Option<Cow<'a, str>>, // TODO enum
    #[serde(skip_serializing_if = "bool_qs_serialize::is_false")]
    #[serde(serialize_with = "bool_qs_serialize::serialize")]
    local: bool,
}
impl<'a> DirectoryRequest<'a> {
    /// make a new DirectoryRequest builder
    pub fn new() -> Self {
        DirectoryRequest::default()
    }

    /// sets the offset
    pub fn offset(mut self, offset: usize) -> Self {
        self.offset = Some(offset);
        self
    }

    /// sets the limit
    pub fn limit(mut self, limit: usize) -> Self {
        self.limit = Some(limit);
        self
    }

    /// sets the order
    pub fn order<I: Into<Cow<'a, str>>>(mut self, order: I) -> Self {
        self.order = Some(order.into());
        self
    }

    /// sets the local
    pub fn local(mut self) -> Self {
        self.local = true;
        self
    }

    /// Turns this builder into a querystring
    ///
    /// # Example
    ///
    /// ```
    /// # extern crate elefren;
    /// # use elefren::requests::DirectoryRequest;
    /// let request = DirectoryRequest::new();
    /// assert_eq!(
    ///     &request
    ///         .limit(10)
    ///         .to_querystring()
    ///         .expect("Couldn't serialize qs"),
    ///     "limit=10"
    /// );
    /// ```
    pub fn to_querystring(&self) -> Result<String, Error> {
        Ok(format!("{}", serde_qs::to_string(&self)?))
    }
}