Skip to main content

mlb_api/requests/
conference.rs

1//! A conference, like in NCAA Baseball.
2//!
3//! At the time of writing, the only two registered are the PCL American Conference and the PCL Pacific Conference.
4
5use crate::league::LeagueId;
6use crate::season::SeasonId;
7use crate::sport::SportId;
8use crate::Copyright;
9use crate::request::RequestURL;
10use bon::Builder;
11use derive_more::{Deref, DerefMut};
12use serde::Deserialize;
13use std::fmt::{Display, Formatter};
14use crate::cache::Requestable;
15
16#[cfg(feature = "cache")]
17use crate::{rwlock_const_new, RwLock, cache::CacheTable};
18
19/// Returns a [`Vec`] of [`Conference`]s
20#[derive(Debug, Deserialize, PartialEq, Clone)]
21#[serde(rename_all = "camelCase")]
22pub struct ConferencesResponse {
23	pub copyright: Copyright,
24	pub conferences: Vec<Conference>,
25}
26
27/// A conference containing all details registered
28#[derive(Debug, Deserialize, Deref, DerefMut, Clone)]
29#[serde(rename_all = "camelCase")]
30pub struct Conference {
31	pub abbreviation: String,
32	#[serde(rename = "nameShort")]
33	pub short_name: String,
34	/// If the conferene has a wildcard slot system for the postseason
35	pub has_wildcard: bool,
36	pub league: LeagueId,
37	pub sport: SportId,
38
39	#[deref]
40	#[deref_mut]
41	#[serde(flatten)]
42	inner: NamedConference,
43}
44
45/// Conference with a name
46#[derive(Debug, Deserialize, Clone, Eq)]
47#[serde(rename_all = "camelCase")]
48pub struct NamedConference {
49	pub name: String,
50	#[serde(flatten)]
51	pub id: ConferenceId,
52}
53
54id!(#[doc = "A [`u32`] representing the ID of the conference, used in [`ConferencesRequest`]"] ConferenceId { id: u32 });
55
56id_only_eq_impl!(Conference, id);
57id_only_eq_impl!(NamedConference, id);
58
59/// Returns a [`ConferencesResponse`]
60#[derive(Builder)]
61#[builder(derive(Into))]
62pub struct ConferencesRequest {
63	#[builder(into)]
64	conference_id: Option<ConferenceId>,
65	#[builder(into)]
66	season: Option<SeasonId>,
67}
68
69impl<S: conferences_request_builder::State + conferences_request_builder::IsComplete> crate::request::RequestURLBuilderExt for ConferencesRequestBuilder<S> {
70    type Built = ConferencesRequest;
71}
72
73impl Display for ConferencesRequest {
74	fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
75		write!(f, "http://statsapi.mlb.com/api/v1/conferences{}", gen_params! { "conferenceId"?: self.conference_id, "season"?: self.season })
76	}
77}
78
79impl RequestURL for ConferencesRequest {
80	type Response = ConferencesResponse;
81}
82
83#[cfg(feature = "cache")]
84static CACHE: RwLock<CacheTable<Conference>> = rwlock_const_new(CacheTable::new());
85
86impl Requestable for Conference {
87	type Identifier = ConferenceId;
88	type URL = ConferencesRequest;
89
90	fn id(&self) -> &Self::Identifier {
91		&self.id
92	}
93
94	#[cfg(feature = "aggressive_cache")]
95	fn url_for_id(_id: &Self::Identifier) -> Self::URL {
96		ConferencesRequest::builder().build()
97	}
98
99	#[cfg(not(feature = "aggressive_cache"))]
100	fn url_for_id(id: &Self::Identifier) -> Self::URL {
101		ConferencesRequest::builder().conference_id(*id).build()
102	}
103
104	fn get_entries(response: <Self::URL as RequestURL>::Response) -> impl IntoIterator<Item=Self>
105	where
106		Self: Sized
107	{
108		response.conferences
109	}
110
111	#[cfg(feature = "cache")]
112	fn get_cache_table() -> &'static RwLock<CacheTable<Self>>
113	where
114		Self: Sized
115	{
116		&CACHE
117	}
118}
119
120entrypoint!(Conference.id => Conference);
121entrypoint!(NamedConference.id => Conference);
122entrypoint!(ConferenceId => Conference);
123
124#[cfg(test)]
125mod tests {
126	use crate::conference::ConferencesRequest;
127	use crate::request::RequestURLBuilderExt;
128
129	#[tokio::test]
130	async fn parse_all_conferences() {
131		let _response = ConferencesRequest::builder().build_and_get().await.unwrap();
132	}
133}