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
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//! A set of builders for use with some request functions that have multiple
//! optional parameters.

use serde_json;
use std::collections::BTreeMap;
use super::GetBeatmapUser;
use ::model::*;

enum GetBeatmapType {
    /// Use for user_ids.
    Id,
    /// Use for usernames.
    String,
}

impl GetBeatmapType {
    pub fn name(&self) -> &str {
        match *self {
            GetBeatmapType::Id => "id",
            GetBeatmapType::String => "string",
        }
    }
}

/// A builder used in conjunction with [`OsuRequester::get_beatmaps`] for
/// optional parameters.
///
/// [`OsuRequester::get_beatmaps`]: trait.OsuRequester.html#method.get_beatmaps
#[derive(Default)]
pub struct GetBeatmapsRequest<'a>(pub BTreeMap<&'a str, String>);

impl<'a> GetBeatmapsRequest<'a> {
    /// Specify the beatmap ID to filter by.
    pub fn beatmap_id(mut self, beatmap_id: u64) -> Self {
        self.0.insert("b", beatmap_id.to_string());

        GetBeatmapsRequest(self.0)
    }

    /// Specify the beatmap set ID to filter by.
    pub fn beatmap_set_id(mut self, beatmap_set_id: u64) -> Self {
        self.0.insert("s", beatmap_set_id.to_string());

        GetBeatmapsRequest(self.0)
    }

    /// Specify the hash to filter by.
    pub fn hash(mut self, hash: &str) -> Self {
        self.0.insert("h", hash.to_owned());

        GetBeatmapsRequest(self.0)
    }

    /// Specify whether converted beatmaps are included.
    ///
    /// Only has an effect if the [`mode`] is chosen and not [`Standard`].
    ///
    /// [`Standard`]: ../model/enum.PlayMode.html#variant.Standard
    /// [`mode`]: #method.mode
    pub fn include_converted(mut self, include_converted: bool) -> Self {
        self.0.insert("a", include_converted.to_string());

        GetBeatmapsRequest(self.0)
    }

    /// The amount of results to return.
    ///
    /// Defaults to `500`. Maximum is `500`.
    pub fn limit(mut self, limit: u16) -> Self {
        self.0.insert("limit", limit.to_string());

        GetBeatmapsRequest(self.0)
    }

    /// Specify the mode to filter by.
    ///
    /// Refer to [`GetUserRequest::mode`] for examples.
    ///
    /// [`GetUserRequest::mode`]: struct.GetUserRequest.html#method.mode
    pub fn mode(mut self, mode: PlayMode) -> Self {
        if let Ok(mode) = serde_json::to_string(&mode) {
            self.0.insert("m", mode);
        }

        GetBeatmapsRequest(self.0)
    }

    /// Return all beatmaps ranked since the given date. Must be a MySQL date.
    pub fn since(mut self, since: &str) -> Self {
        self.0.insert("since", since.to_owned());

        GetBeatmapsRequest(self.0)
    }

    /// Specify the user to retrieve a beatmap for. Pass in a u64 to search by
    /// Id, or a string to search by username.
    ///
    /// This will automatically set the 'type' based on what is passed.
    pub fn user<U: Into<GetBeatmapUser>>(mut self, user: U) -> Self {
        match user.into() {
            GetBeatmapUser::UserId(id) => {
                self.0.insert("u", id.to_string());
                self.0.insert("type", GetBeatmapType::Id.name().to_owned());
            },
            GetBeatmapUser::Username(name) => {
                self.0.insert("u", name);
                self.0.insert("type", GetBeatmapType::String.name().to_owned());
            }
        }

        GetBeatmapsRequest(self.0)
    }
}

/// A builder used in conjunction with [`OsuRequester::get_scores`] for
/// optional parameters.
///
/// [`OsuRequester::get_scores`]: trait.OsuRequester.html#method.get_scores
#[derive(Default)]
pub struct GetScoreRequest<'a>(pub BTreeMap<&'a str, String>);

impl<'a> GetScoreRequest<'a> {
    /// Specify the number of performances to retrieve at maximum.
    ///
    /// Refer to [`GetUserRequest::limit`] for examples.
    ///
    /// [`GetUserRequest::limit`]: struct.GetUserRequest.html#method.limit
    pub fn limit(mut self, limit: u16) -> Self {
        self.0.insert("limit", limit.to_string());

        GetScoreRequest(self.0)
    }

    /// Specify the mode to filter by.
    ///
    /// Refer to [`GetUserRequest::mode`] for examples.
    ///
    /// [`GetUserRequest::mode`]: struct.GetUserRequest.html#method.mode
    pub fn mode(mut self, mode: PlayMode) -> Self {
        if let Ok(mode) = serde_json::to_string(&mode) {
            self.0.insert("m", mode);
        }

        GetScoreRequest(self.0)
    }

    /// Filters results by scores with certain Mods enabled.
    pub fn mods(mut self, mods: Mods) -> Self {
        self.0.insert("mods", mods.bits().to_string());

        GetScoreRequest(self.0)
    }

    /// Specify the user to retrieve a beatmap's top scores for. Pass in a u64
    /// to search by Id, or a string to search by username.
    ///
    /// This will automatically set the 'type' based on what is passed.
    pub fn user<U: Into<GetBeatmapUser>>(mut self, user: U) -> Self {
        match user.into() {
            GetBeatmapUser::UserId(id) => {
                self.0.insert("u", id.to_string());
                self.0.insert("type", GetBeatmapType::Id.name().to_owned());
            },
            GetBeatmapUser::Username(name) => {
                self.0.insert("u", name);
                self.0.insert("type", GetBeatmapType::String.name().to_owned());
            }
        }

        GetScoreRequest(self.0)
    }
}

/// A builder used in conjunction with [`OsuRequester::get_user_best`] for
/// optional parameters.
///
/// [`OsuRequester::get_user_best`]: trait.OsuRequester.html#method.get_user_best
#[derive(Default)]
pub struct GetUserBestRequest<'a>(pub BTreeMap<&'a str, String>);

impl<'a> GetUserBestRequest<'a> {
    /// Specify the number of best performances to retrieve at maximum.
    pub fn limit(mut self, limit: u16) -> Self {
        self.0.insert("limit", limit.to_string());

        GetUserBestRequest(self.0)
    }

    /// Specify the mode to filter a user's performances by.
    pub fn mode(mut self, mode: PlayMode) -> Self {
        if let Ok(mode) = serde_json::to_string(&mode) {
            self.0.insert("m", mode);
        }

        GetUserBestRequest(self.0)
    }

    /// Specify the user to retrieve top scores for. Pass in a u64 to search by
    /// Id, or a string to search by username.
    ///
    /// This will automatically set the 'type' based on what is passed.
    #[doc(hidden)]
    pub fn user<U: Into<GetBeatmapUser>>(mut self, user: U) -> Self {
        match user.into() {
            GetBeatmapUser::UserId(id) => {
                self.0.insert("u", id.to_string());
                self.0.insert("type", GetBeatmapType::Id.name().to_owned());
            },
            GetBeatmapUser::Username(name) => {
                self.0.insert("u", name);
                self.0.insert("type", GetBeatmapType::String.name().to_owned());
            }
        }

        GetUserBestRequest(self.0)
    }
}

/// A builder used in conjunction with [`OsuRequester::get_user_recent`] for
/// optional parameters.
///
/// [`OsuRequester::get_user_recent`]: trait.OsuRequester.html#method.get_user_recent
#[derive(Default)]
pub struct GetUserRecentRequest<'a>(pub BTreeMap<&'a str, String>);

impl<'a> GetUserRecentRequest<'a> {
    /// Specify the number of recent plays to retrieve at maximum.
    pub fn limit(mut self, limit: u16) -> Self {
        self.0.insert("limit", limit.to_string());

        GetUserRecentRequest(self.0)
    }

    /// Specify the mode to filter by.
    ///
    /// Refer to [`GetUserRequest::mode`] for examples.
    ///
    /// [`GetUserRequest::mode`]: struct.GetUserRequest.html#method.mode
    pub fn mode(mut self, mode: PlayMode) -> Self {
        if let Ok(mode) = serde_json::to_string(&mode) {
            self.0.insert("m", mode);
        }

        GetUserRecentRequest(self.0)
    }

    /// Specify the user to retrieve data for. Pass in a u64 to search by
    /// Id, or a string to search by username.
    ///
    /// This will automatically set the 'type' based on what is passed.
    #[doc(hidden)]
    pub fn user<U: Into<GetBeatmapUser>>(mut self, user: U) -> Self {
        match user.into() {
            GetBeatmapUser::UserId(id) => {
                self.0.insert("u", id.to_string());
                self.0.insert("type", GetBeatmapType::Id.name().to_owned());
            },
            GetBeatmapUser::Username(name) => {
                self.0.insert("u", name);
                self.0.insert("type", GetBeatmapType::String.name().to_owned());
            }
        }

        GetUserRecentRequest(self.0)
    }
}

/// A builder used in conjunction with [`OsuRequester::get_user`] for optional
/// parameters.
///
/// [`OsuRequester::get_user`]: trait.OsuRequester.html#method.get_user
#[derive(Default)]
pub struct GetUserRequest<'a>(pub BTreeMap<&'a str, String>);

impl<'a> GetUserRequest<'a> {
    /// Specify the number of event days to filter by.
    pub fn event_days(mut self, event_days: u8) -> Self {
        self.0.insert("event_days", event_days.to_string());

        GetUserRequest(self.0)
    }

    /// Specify the mode to filter results by.
    pub fn mode(mut self, mode: PlayMode) -> Self {
        if let Ok(mode) = serde_json::to_string(&mode) {
            self.0.insert("m", mode);
        }

        GetUserRequest(self.0)
    }

    /// Specify the user to retrieve data for. Pass in a u64 to search by
    /// Id, or a string to search by username.
    ///
    /// This will automatically set the 'type' based on what is passed.
    #[doc(hidden)]
    pub fn user<U: Into<GetBeatmapUser>>(mut self, user: U) -> Self {
        match user.into() {
            GetBeatmapUser::UserId(id) => {
                self.0.insert("u", id.to_string());
                self.0.insert("type", GetBeatmapType::Id.name().to_owned());
            },
            GetBeatmapUser::Username(name) => {
                self.0.insert("u", name);
                self.0.insert("type", GetBeatmapType::String.name().to_owned());
            }
        }

        GetUserRequest(self.0)
    }
}