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
//! The category module handles retrieving Categories. A Category is a ruleset for a Game and may
//! contain Runs.
//!
//! [API Documentation](https://github.com/glacials/splits-io/blob/master/docs/api.md#category)

use crate::{
    get_json,
    platform::Body,
    wrapper::{ContainsCategory, ContainsRunners, ContainsRuns},
    Category, Client, Error, Run, Runner,
};
use http::Request;
use url::Url;

impl Category {
    /// Gets a Category.
    pub async fn get(client: &Client, id: &str) -> Result<Self, Error> {
        self::get(client, id).await
    }

    /// Gets the Runners that belong to the Category.
    pub async fn runners(&self, client: &Client) -> Result<Vec<Runner>, Error> {
        get_runners(client, &self.id).await
    }

    /// Gets the Runs that belong to the Category.
    pub async fn runs(&self, client: &Client) -> Result<Vec<Run>, Error> {
        get_runs(client, &self.id).await
    }
}

/// Gets a Category.
pub async fn get(client: &Client, id: &str) -> Result<Category, Error> {
    let mut url = Url::parse("https://splits.io/api/v4/categories").unwrap();
    url.path_segments_mut().unwrap().push(id);

    let ContainsCategory { category } = get_json(
        client,
        Request::get(url.as_str()).body(Body::empty()).unwrap(),
    )
    .await?;

    Ok(category)
}

/// Gets the Runners that belong to a Category.
pub async fn get_runners(client: &Client, id: &str) -> Result<Vec<Runner>, Error> {
    let mut url = Url::parse("https://splits.io/api/v4/categories").unwrap();
    url.path_segments_mut().unwrap().extend(&[id, "runners"]);

    let ContainsRunners { runners } = get_json(
        client,
        Request::get(url.as_str()).body(Body::empty()).unwrap(),
    )
    .await?;

    Ok(runners)
}

/// Gets the Runs that belong to a Category.
pub async fn get_runs(client: &Client, id: &str) -> Result<Vec<Run>, Error> {
    let mut url = Url::parse("https://splits.io/api/v4/categories").unwrap();
    url.path_segments_mut().unwrap().extend(&[id, "runs"]);

    let ContainsRuns { runs } = get_json(
        client,
        Request::get(url.as_str()).body(Body::empty()).unwrap(),
    )
    .await?;

    Ok(runs)
}