mlb_api/endpoints/meta/kinds/
wind_direction.rs

1use crate::endpoints::meta::{MetaEndpointUrl, MetaKind};
2use derive_more::{Deref, DerefMut, Display, From};
3use serde::Deserialize;
4use std::ops::{Deref, DerefMut};
5use strum::EnumTryAs;
6use crate::cache::{EndpointEntryCache, HydratedCacheTable};
7use crate::{rwlock_const_new, RwLock};
8use crate::endpoints::StatsAPIUrl;
9
10#[derive(Debug, Deserialize, PartialEq, Eq, Clone)]
11pub struct IdentifiableWindDirection {
12	#[serde(rename = "code")] pub id: WindDirectionId,
13}
14
15#[repr(transparent)]
16#[derive(Debug, Deserialize, Deref, Display, PartialEq, Eq, Clone, Hash)]
17pub struct WindDirectionId(String);
18
19#[derive(Debug, Deserialize, Deref, DerefMut, PartialEq, Eq, Clone)]
20pub struct HydratedWindDirection {
21	pub description: String,
22
23	#[deref]
24	#[deref_mut]
25	#[serde(flatten)]
26	inner: IdentifiableWindDirection,
27}
28
29#[derive(Debug, Deserialize, Eq, Clone, From, EnumTryAs)]
30#[serde(untagged)]
31pub enum WindDirection {
32	Hydrated(HydratedWindDirection),
33	Identifiable(IdentifiableWindDirection),
34}
35
36impl PartialEq for WindDirection {
37	fn eq(&self, other: &Self) -> bool {
38		self.id == other.id
39	}
40}
41
42impl Deref for WindDirection {
43	type Target = IdentifiableWindDirection;
44
45	fn deref(&self) -> &Self::Target {
46		match self {
47			Self::Hydrated(inner) => inner,
48			Self::Identifiable(inner) => inner,
49		}
50	}
51}
52
53impl DerefMut for WindDirection {
54	fn deref_mut(&mut self) -> &mut Self::Target {
55		match self {
56			Self::Hydrated(inner) => inner,
57			Self::Identifiable(inner) => inner,
58		}
59	}
60}
61
62impl MetaKind for WindDirection {
63	const ENDPOINT_NAME: &'static str = "windDirection";
64}
65
66static CACHE: RwLock<HydratedCacheTable<WindDirection>> = rwlock_const_new(HydratedCacheTable::new());
67
68impl EndpointEntryCache for WindDirection {
69	type HydratedVariant = HydratedWindDirection;
70	type Identifier = WindDirectionId;
71	type URL = MetaEndpointUrl<Self>;
72
73	fn into_hydrated_variant(self) -> Option<Self::HydratedVariant> {
74		self.try_as_hydrated()
75	}
76
77	fn id(&self) -> &Self::Identifier {
78		&self.id
79	}
80
81	fn url_for_id(_id: &Self::Identifier) -> Self::URL {
82		MetaEndpointUrl::new()
83	}
84
85	fn get_entries(response: <Self::URL as StatsAPIUrl>::Response) -> impl IntoIterator<Item=Self>
86	where
87		Self: Sized
88	{
89		response.entries
90	}
91
92	fn get_hydrated_cache_table() -> &'static RwLock<HydratedCacheTable<Self>>
93	where
94		Self: Sized
95	{
96		&CACHE
97	}
98}
99
100#[cfg(test)]
101mod tests {
102	use crate::endpoints::StatsAPIUrl;
103	use crate::endpoints::meta::MetaEndpointUrl;
104
105	#[tokio::test]
106	async fn parse_meta() {
107		let _response = MetaEndpointUrl::<super::WindDirection>::new().get().await.unwrap();
108	}
109}