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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//! # Runs
//!
//! Endpoints available for runs.
use std::{
    borrow::Cow,
    collections::{BTreeSet, HashMap},
    fmt::Display,
};

use http::Method;
use serde::{Deserialize, Serialize};

use super::{
    categories::CategoryId,
    endpoint::Endpoint,
    games::GameId,
    levels::LevelId,
    platforms::PlatformId,
    regions::RegionId,
    users::UserId,
    variables::{ValueId, VariableId},
    Direction, Pageable,
};

/// Embeds available for runs.
///
/// ## NOTE
/// Embeds can be nested. That is not handled by this API.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum RunEmbeds {
    /// Embeds the full game resource.
    Game,
    /// Embeds the category resource for the run.
    Category,
    /// Embeds the level for the run. This can be empty if it is a full-game
    /// run.
    Level,
    /// Embeds the full user/guest resource in place of the `players` field.
    Players,
    /// Embeds the full region resource. Can be empty if no region was set.
    Region,
    /// Embeds the full platform resource. Can be empty if no platform was set.
    Platform,
}

/// Verification status for the run.
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "kebab-case")]
pub enum RunStatus {
    /// Not yet reviewed.
    New,
    /// Run has been verified by a moderator.
    Verified,
    /// Run has been rejected by a moderator.
    Rejected,
}

/// Sorting options for runs
#[derive(Debug, Serialize, Clone, Copy)]
#[serde(rename_all = "kebab-case")]
pub enum RunsSorting {
    /// Sorts by the game the run was done in (default)
    Game,
    /// Sorts by the run category
    Category,
    /// Sorts by the run level
    Level,
    /// Sorts by the platform used for the run
    Platform,
    /// Sorts by the console region used for the run
    Region,
    /// Sorts by whether an emulator was used for the run
    Emulated,
    /// Sorts by the date of the run
    Date,
    /// Sorts by the date when the run was submitted to speedrun.com
    Submitted,
    /// Sorts by verification status
    Status,
    /// Sorts by the date the run was verified
    VerifyDate,
}

/// Identifies a player (either a user or a guest).
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
#[serde(tag = "rel")]
pub enum Player<'a> {
    /// A user.
    User {
        /// `ID` of the user.
        id: UserId<'a>,
    },
    /// A guest.
    Guest {
        /// Name of the guest player.
        name: Cow<'a, str>,
    },
}

/// Represents a [splits.io](https://splits.io) `ID` or URL.
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "kebab-case")]
#[serde(untagged)]
pub enum SplitsIo {
    /// Splits.io ID
    Id(String),
    /// Splits.io URL
    Url(url::Url),
}

// Does this belong here?
/// Type of the variable value.
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "kebab-case")]
pub enum ValueType<'a> {
    /// Pre-defined variable
    PreDefined {
        /// Value ID
        value: ValueId<'a>,
    },
    /// User defined variable
    UserDefined {
        /// Value ID
        value: ValueId<'a>,
    },
}

/// Updated status for a run.
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "kebab-case")]
#[serde(tag = "status")]
pub enum NewStatus {
    /// Run has been verified.
    Verified,
    /// Run has been rejected.
    Rejected {
        /// The reason the run was rejected (required).
        reason: String,
    },
}

/// Represents a run ID.
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq, Hash)]
pub struct RunId<'a>(Cow<'a, str>);

impl<'a> RunId<'a> {
    /// Create a new [`RunId`].
    pub fn new<T>(id: T) -> Self
    where
        T: Into<Cow<'a, str>>,
    {
        Self(id.into())
    }
}

impl<'a, T> From<T> for RunId<'a>
where
    T: Into<Cow<'a, str>>,
{
    fn from(value: T) -> Self {
        Self::new(value)
    }
}

impl Display for RunId<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", &self.0)
    }
}

/// Returns a list of all runs.
#[derive(Default, Debug, Builder, Serialize, Clone)]
#[builder(default, setter(into, strip_option))]
#[serde(rename_all = "kebab-case")]
pub struct Runs<'a> {
    #[doc = r"Return only runs done by `user`."]
    user: Option<UserId<'a>>,
    #[doc = r"Return only runs done by `guest`."]
    guest: Option<Cow<'a, str>>,
    #[doc = r"Return only runs examined by `examiner`."]
    examiner: Option<UserId<'a>>,
    #[doc = r"Restrict results to `game`."]
    game: Option<GameId<'a>>,
    #[doc = r"Restrict results to `level`."]
    level: Option<LevelId<'a>>,
    #[doc = r"Restrict results to `category`."]
    category: Option<CategoryId<'a>>,
    #[doc = r"Restrict results to `platform`."]
    platform: Option<PlatformId<'a>>,
    #[doc = r"Restrict results to `region`."]
    region: Option<RegionId<'a>>,
    #[doc = r"Only return games run on an emulator when `true`."]
    emulated: Option<bool>,
    #[doc = r"Filter runs based on status."]
    status: Option<RunStatus>,
    #[doc = r"Sorting options for results."]
    orderby: Option<RunsSorting>,
    #[doc = r"Sort direction"]
    direction: Option<Direction>,
    #[builder(setter(name = "_embed"), private)]
    #[serde(serialize_with = "super::utils::serialize_as_csv")]
    #[serde(skip_serializing_if = "BTreeSet::is_empty")]
    embed: BTreeSet<RunEmbeds>,
}

/// Retrieves a single run.
#[derive(Debug, Builder, Serialize, Clone)]
#[builder(setter(into, strip_option))]
#[serde(rename_all = "kebab-case")]
pub struct Run<'a> {
    #[doc = r"`ID` of the run."]
    id: RunId<'a>,
}

/// Submit a new run.
///
/// This endpoint requires a valid API key.
#[derive(Debug, Builder, Serialize, Clone)]
#[builder(setter(into, strip_option), build_fn(validate = "Self::validate"))]
#[serde(rename_all = "kebab-case")]
pub struct CreateRun<'a> {
    #[doc = r"Category ID for the run."]
    category: CategoryId<'a>,
    #[doc = r"Level ID for individual level runs."]
    #[builder(default)]
    level: Option<LevelId<'a>>,
    #[doc = r"Optional date the run was performed (defaults to the current date)."]
    #[builder(default)]
    date: Option<Cow<'a, str>>,
    #[doc = r"Optional region for the run. Some games require a region to be submitted."]
    #[builder(default)]
    region: Option<RegionId<'a>>,
    #[doc = r"Optional platform for the run. Some games require a platform to be submitted."]
    #[builder(default)]
    platform: Option<PlatformId<'a>>,
    #[doc = r"If the run has been verified by a moderator. Can only be set if the submitting user is a moderator of the game."]
    #[builder(default)]
    verified: Option<bool>,
    #[builder(setter(name = "_times"), private, default)]
    times: Times,
    #[builder(setter(name = "_players"), private, default)]
    #[serde(skip_serializing_if = "Vec::is_empty")]
    players: Vec<Player<'a>>,
    #[doc = r"When `true` the run was performed on an emulator (default: false)."]
    emulated: Option<bool>,
    #[doc = r"A valid video URL. Optional, but some games require a video to be included."]
    #[builder(default)]
    video: Option<url::Url>,
    #[doc = r"Optional comment on the run. Can include additional video URLs."]
    #[builder(default)]
    comment: Option<String>,
    #[doc = r"Splits.io ID or URL for the splits for the run."]
    #[builder(default)]
    splitsio: Option<SplitsIo>,
    #[doc = r"Variable values for the new run. Some games have mandatory variables."]
    #[builder(default)]
    #[serde(skip_serializing_if = "HashMap::is_empty")]
    variables: HashMap<VariableId<'a>, ValueType<'a>>,
}

#[derive(Default, Debug, Serialize, Clone)]
#[serde(rename_all = "snake_case")]
struct Times {
    realtime: Option<f64>,
    realtime_noloads: Option<f64>,
    ingame: Option<f64>,
}

/// Update the verification status for the run.
///
/// Requires a valid API key for an authenticated user. The authenticated user
/// must have sufficient permissions (global moderator or game moderator) to
/// change the verification status of a run.
#[derive(Debug, Builder, Serialize, Clone)]
#[builder(setter(into, strip_option))]
#[serde(rename_all = "kebab-case")]
pub struct UpdateRunStatus<'a> {
    #[doc = r"`ID` of the run."]
    #[serde(skip)]
    id: RunId<'a>,
    #[doc = r"Updated status for the run."]
    status: NewStatus,
}

/// Change the list of players that participated in a run.
///
/// The updated list must contain at least one player or guest.
///
/// The submitted list of players will replace the old list completely. i.e. you
/// cannot just add a player without also submitting the existing players.
///
/// Requires a valid API key for an authenticated user. The authenticated user
/// must have sufficient permissions (global moderator or game moderator) to
/// change the verification status of a run.
#[derive(Debug, Builder, Serialize, Clone)]
#[builder(setter(into, strip_option))]
#[serde(rename_all = "kebab-case")]
pub struct UpdateRunPlayers<'a> {
    #[doc = r"`ID` of the run."]
    #[serde(skip)]
    id: RunId<'a>,
    #[builder(setter(name = "_players"), private)]
    players: Vec<Player<'a>>,
}

/// Delete a run.
///
/// Requires a valid API key for an authenticated user. Regular users can only
/// delete their own runs. Moderators can delete runs by other users also.
#[derive(Debug, Builder, Serialize, Clone)]
#[builder(setter(into, strip_option))]
#[serde(rename_all = "kebab-case")]
pub struct DeleteRun<'a> {
    #[doc = r"`ID` of the run."]
    id: RunId<'a>,
}

impl<'a> Runs<'a> {
    /// Create a builder for this endpoint.
    pub fn builder() -> RunsBuilder<'a> {
        RunsBuilder::default()
    }
}

impl<'a> RunsBuilder<'a> {
    /// Add an embedded resource to this result
    pub fn embed(&mut self, embed: RunEmbeds) -> &mut Self {
        self.embed.get_or_insert_with(BTreeSet::new).insert(embed);
        self
    }

    /// Add multiple embedded resources to this result
    pub fn embeds<I>(&mut self, iter: I) -> &mut Self
    where
        I: Iterator<Item = RunEmbeds>,
    {
        self.embed.get_or_insert_with(BTreeSet::new).extend(iter);
        self
    }
}

impl<'a> Run<'a> {
    /// Create a builder for this endpoint
    pub fn builder() -> RunBuilder<'a> {
        RunBuilder::default()
    }
}

impl<'a> CreateRun<'a> {
    /// Create a builder for this endpoint
    pub fn buider() -> CreateRunBuilder<'a> {
        CreateRunBuilder::default()
    }
}

impl<'a> CreateRunBuilder<'a> {
    /// Real-world time of the run
    pub fn realtime<T: Into<f64>>(&mut self, value: T) -> &mut Self {
        self.times.get_or_insert_with(Times::default).realtime = Some(value.into());
        self
    }

    /// Real-world time of the run, excluding the loading times
    pub fn realtime_noloads<T: Into<f64>>(&mut self, value: T) -> &mut Self {
        self.times
            .get_or_insert_with(Times::default)
            .realtime_noloads = Some(value.into());
        self
    }

    /// Time measured by the game
    pub fn ingame<T: Into<f64>>(&mut self, value: T) -> &mut Self {
        self.times.get_or_insert_with(Times::default).ingame = Some(value.into());
        self
    }

    /// Add a player to this run.
    pub fn player(&mut self, player: Player<'a>) -> &mut Self {
        self.players.get_or_insert_with(Vec::new).push(player);
        self
    }

    /// Add multiple players to this run.
    pub fn players<I>(&mut self, iter: I) -> &mut Self
    where
        I: Iterator<Item = Player<'a>>,
    {
        self.players.get_or_insert_with(Vec::new).extend(iter);
        self
    }

    fn validate(&self) -> Result<(), String> {
        if let Some(times) = &self.times {
            if times.realtime.is_none()
                && times.realtime_noloads.is_none()
                && times.ingame.is_none()
            {
                return Err("At least one time must be set. Set one of `realtime`, \
                            `realtime_noloads`, or `ingame`."
                    .into());
            }
        }
        Ok(())
    }
}

impl<'a> UpdateRunStatus<'a> {
    /// Create a builder for this endpoint
    pub fn builder() -> UpdateRunStatusBuilder<'a> {
        UpdateRunStatusBuilder::default()
    }
}

impl<'a> UpdateRunPlayers<'a> {
    /// Create a builder for this endpoint
    pub fn builder() -> UpdateRunPlayersBuilder<'a> {
        UpdateRunPlayersBuilder::default()
    }
}

impl<'a> UpdateRunPlayersBuilder<'a> {
    /// Add a single user/guest to the updated list of players.
    pub fn player(&mut self, player: Player<'a>) -> &mut Self {
        self.players.get_or_insert_with(Vec::new).push(player);
        self
    }

    /// Add multiple users/guests to the updated list of players.
    pub fn players<I>(&mut self, iter: I) -> &mut Self
    where
        I: Iterator<Item = Player<'a>>,
    {
        self.players.get_or_insert_with(Vec::new).extend(iter);
        self
    }
}

impl<'a> DeleteRun<'a> {
    /// Create a builder for this endpoint
    pub fn builder() -> DeleteRunBuilder<'a> {
        DeleteRunBuilder::default()
    }
}

impl RunEmbeds {
    fn as_str(&self) -> &'static str {
        match self {
            RunEmbeds::Game => "game",
            RunEmbeds::Category => "category",
            RunEmbeds::Level => "level",
            RunEmbeds::Players => "players",
            RunEmbeds::Region => "region",
            RunEmbeds::Platform => "platform",
        }
    }
}

impl Default for RunsSorting {
    fn default() -> Self {
        Self::Game
    }
}

impl Endpoint for Runs<'_> {
    fn method(&self) -> http::Method {
        Method::GET
    }

    fn endpoint(&self) -> Cow<'static, str> {
        "/runs".into()
    }

    fn query_parameters(&self) -> Result<Cow<'static, str>, super::error::BodyError> {
        Ok(serde_urlencoded::to_string(self)?.into())
    }
}

impl Endpoint for Run<'_> {
    fn method(&self) -> Method {
        Method::GET
    }

    fn endpoint(&self) -> Cow<'static, str> {
        format!("/runs/{}", self.id).into()
    }
}

impl Endpoint for CreateRun<'_> {
    fn method(&self) -> Method {
        Method::POST
    }

    fn endpoint(&self) -> Cow<'static, str> {
        "/runs".into()
    }

    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, super::error::BodyError> {
        Ok(serde_json::to_vec(self).map(|body| Some(("application/json", body)))?)
    }

    fn requires_authentication(&self) -> bool {
        true
    }
}

impl Endpoint for UpdateRunStatus<'_> {
    fn method(&self) -> Method {
        Method::PUT
    }

    fn endpoint(&self) -> Cow<'static, str> {
        format!("/runs/{}/status", self.id).into()
    }

    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, super::error::BodyError> {
        Ok(serde_json::to_vec(self).map(|body| Some(("application/json", body)))?)
    }

    fn requires_authentication(&self) -> bool {
        true
    }
}

impl Endpoint for UpdateRunPlayers<'_> {
    fn method(&self) -> Method {
        Method::PUT
    }

    fn endpoint(&self) -> Cow<'static, str> {
        format!("/runs/{}/players", self.id).into()
    }

    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, super::error::BodyError> {
        Ok(serde_json::to_vec(self).map(|body| Some(("application/json", body)))?)
    }

    fn requires_authentication(&self) -> bool {
        true
    }
}

impl Endpoint for DeleteRun<'_> {
    fn method(&self) -> Method {
        Method::DELETE
    }

    fn endpoint(&self) -> Cow<'static, str> {
        format!("/runs/{}", self.id).into()
    }

    fn requires_authentication(&self) -> bool {
        true
    }
}

impl From<&RunEmbeds> for &'static str {
    fn from(value: &RunEmbeds) -> Self {
        value.as_str()
    }
}

impl Pageable for Runs<'_> {}