mcsr_ranked_api/types/
mod.rs

1use std::{
2	fmt::{self, Display},
3	marker::PhantomData,
4	str::FromStr,
5};
6
7use serde::{
8	de::{self, MapAccess, Visitor},
9	Deserialize, Deserializer,
10};
11use uuid::Uuid;
12
13#[cfg(test)]
14mod tests;
15
16pub type Elo = u16;
17pub type EloChange = i16;
18pub type PhasePoints = u16;
19pub type Rank = u32;
20pub type Season = u8;
21pub type Phase = u8;
22#[cfg(feature = "matches")]
23pub type MatchId = u64;
24#[cfg(feature = "weekly_races")]
25pub type WeeklyRaceId = u32;
26pub type MinecraftSeed = u64;
27
28#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
29pub struct Time(pub u64);
30
31impl Time {
32	pub const fn new(value: u64) -> Self {
33		Self(value)
34	}
35	pub const fn millis(&self) -> u64 {
36		self.0 % 1000
37	}
38	pub const fn seconds(&self) -> u64 {
39		(self.0 / 1000) % 60
40	}
41	pub const fn minutes(&self) -> u64 {
42		(self.0 / 60000) % 60
43	}
44	pub const fn hours(&self) -> u64 {
45		self.0 / 3600000
46	}
47}
48
49#[doc(hidden)]
50/// Result with this crate's own `Error` type as default
51pub type Result<T, E = Error> = std::result::Result<T, E>;
52
53#[doc(hidden)]
54/// Error returned by a request to the API
55#[derive(Debug)]
56pub enum Error {
57	/// Ranked API error
58	Api(Option<Box<str>>),
59	/// Reqwest library error
60	Reqwest(reqwest::Error),
61}
62
63impl PartialEq for Error {
64	fn eq(&self, other: &Self) -> bool {
65		match (self, other) {
66			(Error::Api(lhs), Error::Api(rhs)) => lhs == rhs,
67			(Error::Reqwest(lhs), Error::Reqwest(rhs)) => lhs.to_string() == rhs.to_string(),
68			_ => false,
69		}
70	}
71}
72impl Eq for Error {}
73
74impl From<reqwest::Error> for Error {
75	fn from(value: reqwest::Error) -> Self {
76		Self::Reqwest(value)
77	}
78}
79
80impl Display for Error {
81	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
82		match self {
83			Error::Api(Some(api_err)) => write!(f, "API Error: {}", api_err),
84			Error::Api(None) => f.write_str("API Error! (No message)"),
85			Error::Reqwest(req_err) => write!(f, "Reqwest Error: {}", req_err),
86		}
87	}
88}
89impl std::error::Error for Error {}
90
91#[derive(Debug, PartialEq, Eq, Deserialize)]
92#[serde(tag = "status", content = "data", rename_all = "camelCase")]
93pub(crate) enum DeResult<T> {
94	Success(T),
95	Error(Option<Box<str>>),
96}
97
98impl<T> From<DeResult<T>> for Result<T> {
99	fn from(value: DeResult<T>) -> Self {
100		match value {
101			DeResult::Success(t) => Ok(t),
102			DeResult::Error(e) => Err(Error::Api(e)),
103		}
104	}
105}
106
107/// Container for ranked and casual values
108#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
109pub struct RankedAndCasual<T> {
110	ranked: T,
111	casual: T,
112}
113impl<T> RankedAndCasual<T> {
114	/// Value for ranked
115	pub fn ranked(&self) -> &T {
116		&self.ranked
117	}
118	/// Value for casual
119	pub fn casual(&self) -> &T {
120		&self.casual
121	}
122}
123
124/// Container for UUIDs and data of exactly two players
125#[derive(Debug, Clone, Copy, PartialEq, Eq)]
126pub struct TwoUserData<T> {
127	user_1_uuid: Uuid,
128	user_1_data: T,
129	user_2_uuid: Uuid,
130	user_2_data: T,
131}
132impl<T> TwoUserData<T> {
133	/// First user's UUID and data
134	pub fn user_1(&self) -> (Uuid, &T) {
135		(self.user_1_uuid, &self.user_1_data)
136	}
137	/// Second users's UUID and data
138	pub fn user_2(&self) -> (Uuid, &T) {
139		(self.user_2_uuid, &self.user_2_data)
140	}
141}
142
143impl<'de, T: Deserialize<'de>> Deserialize<'de> for TwoUserData<T> {
144	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
145	where
146		D: Deserializer<'de>,
147	{
148		struct TwoUserDataVisitor<T> {
149			_phantom: PhantomData<T>,
150		}
151
152		impl<'de, T: Deserialize<'de>> Visitor<'de> for TwoUserDataVisitor<T> {
153			type Value = TwoUserData<T>;
154
155			fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
156				formatter.write_str("struct TwoUserData")
157			}
158
159			fn visit_map<V>(self, mut map: V) -> Result<Self::Value, V::Error>
160			where
161				V: MapAccess<'de>,
162			{
163				let mut user_1 = None;
164				let mut user_2 = None;
165
166				while let Some(key) = map.next_key()? {
167					if let Ok(uuid) = Uuid::from_str(key) {
168						match (&user_1, &user_2) {
169							(None, None) => user_1 = Some((uuid, map.next_value()?)),
170							(Some(_), None) => user_2 = Some((uuid, map.next_value()?)),
171							_ => return Err(de::Error::duplicate_field("user_2")),
172						}
173					}
174				}
175
176				let user_1 = user_1.ok_or_else(|| de::Error::missing_field("user_1"))?;
177				let user_2 = user_2.ok_or_else(|| de::Error::missing_field("user_2"))?;
178
179				Ok(TwoUserData {
180					user_1_uuid: user_1.0,
181					user_1_data: user_1.1,
182					user_2_uuid: user_2.0,
183					user_2_data: user_2.1,
184				})
185			}
186		}
187
188		deserializer.deserialize_map(TwoUserDataVisitor {
189			_phantom: PhantomData::<T>,
190		})
191	}
192}