mlb_api/endpoints/meta/kinds/
logical_events.rs

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