Skip to main content

splits_io_api/
category.rs

1//! The category module handles retrieving Categories. A Category is a ruleset for a Game and may
2//! contain Runs.
3//!
4//! [API Documentation](https://github.com/glacials/splits-io/blob/master/docs/api.md#category)
5
6use crate::{
7    get_json,
8    wrapper::{ContainsCategory, ContainsRunners, ContainsRuns},
9    Category, Client, Error, Run, Runner,
10};
11use reqwest::Url;
12
13impl Category {
14    /// Gets a Category.
15    pub async fn get(client: &Client, id: &str) -> Result<Self, Error> {
16        self::get(client, id).await
17    }
18
19    /// Gets the Runners that belong to the Category.
20    pub async fn runners(&self, client: &Client) -> Result<Vec<Runner>, Error> {
21        get_runners(client, &self.id).await
22    }
23
24    /// Gets the Runs that belong to the Category.
25    pub async fn runs(&self, client: &Client) -> Result<Vec<Run>, Error> {
26        get_runs(client, &self.id).await
27    }
28}
29
30/// Gets a Category.
31pub async fn get(client: &Client, id: &str) -> Result<Category, Error> {
32    let mut url = Url::parse("https://splits.io/api/v4/categories").unwrap();
33    url.path_segments_mut().unwrap().push(id);
34
35    let ContainsCategory { category } = get_json(client, client.client.get(url)).await?;
36
37    Ok(category)
38}
39
40/// Gets the Runners that belong to a Category.
41pub async fn get_runners(client: &Client, id: &str) -> Result<Vec<Runner>, Error> {
42    let mut url = Url::parse("https://splits.io/api/v4/categories").unwrap();
43    url.path_segments_mut().unwrap().extend(&[id, "runners"]);
44
45    let ContainsRunners { runners } = get_json(client, client.client.get(url)).await?;
46
47    Ok(runners)
48}
49
50/// Gets the Runs that belong to a Category.
51pub async fn get_runs(client: &Client, id: &str) -> Result<Vec<Run>, Error> {
52    let mut url = Url::parse("https://splits.io/api/v4/categories").unwrap();
53    url.path_segments_mut().unwrap().extend(&[id, "runs"]);
54
55    let ContainsRuns { runs } = get_json(client, client.client.get(url)).await?;
56
57    Ok(runs)
58}