mlb_api/endpoints/meta/kinds/
stat_groups.rs

1use crate::endpoints::{MetaKind, StatsAPIUrl};
2use derive_more::{Display, FromStr};
3use serde::Deserialize;
4use crate::cache::{EndpointEntryCache, HydratedCacheTable};
5use crate::{rwlock_const_new, RwLock};
6use crate::endpoints::meta::MetaEndpointUrl;
7
8#[derive(Debug, Deserialize, PartialEq, Eq, Copy, Clone, FromStr, Hash, Display)]
9#[serde(try_from = "__StatGroupStruct")]
10pub enum StatGroup {
11	Hitting,
12	Pitching,
13	Fielding,
14	Catching,
15	Running,
16	Game,
17	Team,
18	Streak,
19}
20
21#[derive(Deserialize)]
22#[serde(rename_all = "camelCase")]
23struct __StatGroupStruct {
24	display_name: String,
25}
26
27impl TryFrom<__StatGroupStruct> for StatGroup {
28	type Error = derive_more::FromStrError;
29
30	fn try_from(value: __StatGroupStruct) -> Result<Self, Self::Error> {
31		value.display_name.parse::<Self>()
32	}
33}
34
35impl MetaKind for StatGroup {
36	const ENDPOINT_NAME: &'static str = "statGroups";
37}
38
39static CACHE: RwLock<HydratedCacheTable<StatGroup>> = rwlock_const_new(HydratedCacheTable::new());
40
41impl EndpointEntryCache for StatGroup {
42	type HydratedVariant = StatGroup;
43	type Identifier = StatGroup;
44	type URL = MetaEndpointUrl<Self>;
45
46	fn into_hydrated_variant(self) -> Option<Self::HydratedVariant> {
47		Some(self)
48	}
49
50	fn id(&self) -> &Self::Identifier {
51		self
52	}
53
54	fn url_for_id(_id: &Self::Identifier) -> Self::URL {
55		MetaEndpointUrl::new()
56	}
57
58	fn get_entries(response: <Self::URL as StatsAPIUrl>::Response) -> impl IntoIterator<Item=Self>
59	where
60		Self: Sized
61	{
62		response.entries
63	}
64
65	fn get_hydrated_cache_table() -> &'static RwLock<HydratedCacheTable<Self>>
66	where
67		Self: Sized
68	{
69		&CACHE
70	}
71}
72
73#[cfg(test)]
74mod tests {
75	use crate::endpoints::StatsAPIUrl;
76	use crate::endpoints::meta::MetaEndpointUrl;
77
78	#[tokio::test]
79	async fn parse_meta() {
80		let _response = MetaEndpointUrl::<super::StatGroup>::new().get().await.unwrap();
81	}
82}