mlb_api/endpoints/meta/kinds/
hit_trajectories.rs1use crate::endpoints::meta::kinds::MetaKind;
2use derive_more::Display;
3use serde::Deserialize;
4use crate::cache::{EndpointEntryCache, HydratedCacheTable};
5use crate::{rwlock_const_new, RwLock};
6use crate::endpoints::meta::MetaEndpointUrl;
7use crate::endpoints::StatsAPIUrl;
8
9#[derive(Debug, Deserialize, PartialEq, Eq, Copy, Clone, Display, Hash)]
10#[serde(try_from = "__HitTrajectoryStruct")]
11pub enum HitTrajectory {
12 #[display("Bunt - Ground Ball")]
13 BuntGrounder,
14
15 #[display("Bunt - Popup")]
16 BuntPopup,
17
18 #[display("Bunt - Line Drive")]
19 BuntLineDrive,
20
21 #[display("Line Drive")]
22 LineDrive,
23
24 #[display("Ground Ball")]
25 GroundBall,
26
27 #[display("Fly Ball")]
28 FlyBall,
29
30 #[display("Popup")]
31 Popup,
32}
33
34#[derive(Deserialize)]
35struct __HitTrajectoryStruct {
36 code: String,
37}
38
39impl TryFrom<__HitTrajectoryStruct> for HitTrajectory {
40 type Error = &'static str;
41
42 fn try_from(value: __HitTrajectoryStruct) -> Result<Self, Self::Error> {
43 Ok(match &*value.code {
44 "bunt_grounder" => HitTrajectory::BuntGrounder,
45 "bunt_popup" => HitTrajectory::BuntPopup,
46 "bunt_line_drive" => HitTrajectory::BuntLineDrive,
47 "line_drive" => HitTrajectory::LineDrive,
48 "ground_ball" => HitTrajectory::GroundBall,
49 "fly_ball" => HitTrajectory::FlyBall,
50 "popup" => HitTrajectory::Popup,
51 _ => return Err("unknown hit trajectory"),
52 })
53 }
54}
55
56impl MetaKind for HitTrajectory {
57 const ENDPOINT_NAME: &'static str = "hitTrajectories";
58}
59
60static CACHE: RwLock<HydratedCacheTable<HitTrajectory>> = rwlock_const_new(HydratedCacheTable::new());
61
62impl EndpointEntryCache for HitTrajectory {
63 type HydratedVariant = HitTrajectory;
64 type Identifier = HitTrajectory;
65 type URL = MetaEndpointUrl<Self>;
66
67 fn into_hydrated_variant(self) -> Option<Self::HydratedVariant> {
68 Some(self)
69 }
70
71 fn id(&self) -> &Self::Identifier {
72 self
73 }
74
75 fn url_for_id(_id: &Self::Identifier) -> Self::URL {
76 MetaEndpointUrl::new()
77 }
78
79 fn get_entries(response: <Self::URL as StatsAPIUrl>::Response) -> impl IntoIterator<Item=Self>
80 where
81 Self: Sized
82 {
83 response.entries
84 }
85
86 fn get_hydrated_cache_table() -> &'static RwLock<HydratedCacheTable<Self>>
87 where
88 Self: Sized
89 {
90 &CACHE
91 }
92}
93
94#[cfg(test)]
95mod tests {
96 use crate::endpoints::StatsAPIUrl;
97 use crate::endpoints::meta::MetaEndpointUrl;
98
99 #[tokio::test]
100 async fn parse_meta() {
101 let _response = MetaEndpointUrl::<super::HitTrajectory>::new().get().await.unwrap();
102 }
103}