speedrun_api/api/
platforms.rs

1//! # Platforms
2//!
3//! Endpoints available for platforms.
4use std::{borrow::Cow, fmt::Display};
5
6use serde::{Deserialize, Serialize};
7
8use super::{endpoint::Endpoint, error::BodyError, query_params::QueryParams, Direction, Pageable};
9
10/// Sorting options for platforms
11#[derive(Debug, Serialize, Clone, Copy)]
12#[serde(rename_all = "kebab-case")]
13pub enum PlatformsSorting {
14    /// Sorts alphanumerically by the platform name (default)
15    Name,
16    /// Sorts by the year the platform was released
17    Released,
18}
19
20/// Represents a platform ID.
21#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)]
22pub struct PlatformId<'a>(Cow<'a, str>);
23
24impl<'a> PlatformId<'a> {
25    /// Create a new [`PlatformId`].
26    pub fn new<T>(id: T) -> Self
27    where
28        T: Into<Cow<'a, str>>,
29    {
30        Self(id.into())
31    }
32}
33
34impl<'a, T> From<T> for PlatformId<'a>
35where
36    T: Into<Cow<'a, str>>,
37{
38    fn from(value: T) -> Self {
39        Self::new(value)
40    }
41}
42
43impl Display for PlatformId<'_> {
44    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45        write!(f, "{}", &self.0)
46    }
47}
48
49/// Retrieves a list of all platforms.
50#[derive(Default, Debug, Builder, Serialize, Clone)]
51#[builder(default, setter(into, strip_option))]
52#[serde(rename_all = "kebab-case")]
53pub struct Platforms {
54    #[doc = r"Sorting options for results."]
55    orderby: Option<PlatformsSorting>,
56    #[doc = r"Sort direction"]
57    direction: Option<Direction>,
58}
59
60/// Retrieves a single platform by ID.
61#[derive(Debug, Builder, Serialize, Clone)]
62#[builder(setter(into, strip_option))]
63#[serde(rename_all = "kebab-case")]
64pub struct Platform<'a> {
65    #[doc = r"`ID` of the platform to retrieve."]
66    id: PlatformId<'a>,
67}
68
69impl Platforms {
70    /// Create a builder for this endpoint.
71    pub fn builder() -> PlatformsBuilder {
72        PlatformsBuilder::default()
73    }
74}
75
76impl Platform<'_> {
77    /// Create a builder for this endpoint.
78    pub fn builder<'a>() -> PlatformBuilder<'a> {
79        PlatformBuilder::default()
80    }
81}
82
83impl Default for PlatformsSorting {
84    fn default() -> Self {
85        Self::Name
86    }
87}
88
89impl Endpoint for Platforms {
90    fn endpoint(&self) -> std::borrow::Cow<'static, str> {
91        "/platforms".into()
92    }
93
94    fn query_parameters(&self) -> Result<QueryParams<'_>, BodyError> {
95        QueryParams::with(self)
96    }
97}
98
99impl Endpoint for Platform<'_> {
100    fn endpoint(&self) -> Cow<'static, str> {
101        format!("/platforms/{}", self.id).into()
102    }
103}
104
105impl Pageable for Platforms {}