speedrun_api/api/
variables.rs

1//! # Variables
2//!
3//! Endpoints available for variables
4use std::{borrow::Cow, fmt::Display};
5
6use serde::{Deserialize, Serialize};
7
8use super::endpoint::Endpoint;
9
10/// Represents a variable ID
11#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq, Hash)]
12pub struct VariableId<'a>(Cow<'a, str>);
13
14impl<'a> VariableId<'a> {
15    /// Create a new [`VariableId`]
16    pub fn new<T>(id: T) -> Self
17    where
18        T: Into<Cow<'a, str>>,
19    {
20        Self(id.into())
21    }
22}
23
24impl<'a, T> From<T> for VariableId<'a>
25where
26    T: Into<Cow<'a, str>>,
27{
28    fn from(value: T) -> Self {
29        Self::new(value)
30    }
31}
32
33impl Display for VariableId<'_> {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        write!(f, "{}", &self.0)
36    }
37}
38
39/// Represents a value ID
40#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq, Hash)]
41pub struct ValueId<'a>(Cow<'a, str>);
42
43impl<'a> ValueId<'a> {
44    /// Create a new [`ValueId`]
45    pub fn new<T>(id: T) -> Self
46    where
47        T: Into<Cow<'a, str>>,
48    {
49        Self(id.into())
50    }
51}
52
53impl<'a, T> From<T> for ValueId<'a>
54where
55    T: Into<Cow<'a, str>>,
56{
57    fn from(value: T) -> Self {
58        Self::new(value)
59    }
60}
61
62impl Display for ValueId<'_> {
63    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
64        write!(f, "{}", &self.0)
65    }
66}
67
68/// Retrieves a single variable
69#[derive(Debug, Builder, Clone)]
70#[builder(setter(into, strip_option))]
71pub struct Variable<'a> {
72    #[doc = r"Variable ID"]
73    id: VariableId<'a>,
74}
75
76impl Variable<'_> {
77    /// Create a builder for this endpoint.
78    pub fn builder<'a>() -> VariableBuilder<'a> {
79        VariableBuilder::default()
80    }
81}
82
83impl Endpoint for Variable<'_> {
84    fn endpoint(&self) -> Cow<'static, str> {
85        format!("/variables/{}", self.id).into()
86    }
87}