Skip to main content

mlb_api/requests/
awards.rs

1//! An award, such as the Cy Young, MVP, etc.
2//! 
3//! There are over a thousand awards for pretty much every organized league you can think of.
4
5use crate::league::LeagueId;
6use crate::request::RequestURL;
7use crate::season::SeasonId;
8use crate::sport::SportId;
9use crate::Copyright;
10use bon::Builder;
11use serde::Deserialize;
12use std::fmt::{Display, Formatter};
13use crate::cache::Requestable;
14
15#[cfg(feature = "cache")]
16use crate::{rwlock_const_new, RwLock, cache::CacheTable};
17
18/// Response from the `awards` endpoint.
19/// Returns a [`Vec`] of [`Award`]s
20///
21/// Example: <http://statsapi.mlb.com/api/v1/awards>
22#[derive(Debug, Deserialize, PartialEq, Clone)]
23pub struct AwardsResponse {
24	pub copyright: Copyright,
25	pub awards: Vec<Award>,
26}
27
28/// An award, such as the Cy Young or MVP.
29#[derive(Debug, Deserialize, Clone)]
30pub struct Award {
31	/// Name of the award
32	pub name: String,
33	/// Description of the award
34	pub description: Option<String>,
35	/// Sport associated with the award
36	pub sport: Option<SportId>,
37	/// League associated with the award
38	pub league: Option<LeagueId>,
39	/// Notes (if necessary)
40	pub notes: Option<String>,
41	#[serde(flatten)]
42	pub id: AwardId,
43}
44
45id_only_eq_impl!(Award, id);
46id!(#[doc = "A string representing an award, ID of award, used in [`AwardRequest`]"] AwardId { id: String });
47
48/// Returns [`AwardResponse`]
49#[derive(Builder)]
50#[builder(derive(Into))]
51pub struct AwardRequest {
52	#[builder(into)]
53	award_id: Option<AwardId>,
54	#[builder(into)]
55	sport_id: Option<SportId>,
56	#[builder(into)]
57	league_id: Option<LeagueId>,
58	#[builder(into)]
59	season: Option<SeasonId>,
60}
61
62impl<S: award_request_builder::State + award_request_builder::IsComplete> crate::request::RequestURLBuilderExt for AwardRequestBuilder<S> {
63    type Built = AwardRequest;
64}
65
66impl Display for AwardRequest {
67	fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
68		write!(
69			f,
70			"http://statsapi.mlb.com/api/v1/awards{}",
71			gen_params! { "awardId"?: self.award_id.as_ref(), "sportId"?: self.sport_id, "leagueId"?: self.league_id, "season"?: self.season }
72		)
73	}
74}
75
76impl RequestURL for AwardRequest {
77	type Response = AwardsResponse;
78}
79
80#[cfg(feature = "cache")]
81static CACHE: RwLock<CacheTable<Award>> = rwlock_const_new(CacheTable::new());
82
83impl Requestable for Award {
84	type Identifier = AwardId;
85	type URL = AwardRequest;
86
87	fn id(&self) -> &Self::Identifier {
88		&self.id
89	}
90
91	#[cfg(feature = "aggressive_cache")]
92	fn url_for_id(_id: &Self::Identifier) -> Self::URL {
93		AwardRequest::builder().build()
94	}
95
96	#[cfg(not(feature = "aggressive_cache"))]
97	fn url_for_id(id: &Self::Identifier) -> Self::URL {
98		AwardRequest::builder().award_id(id.clone()).build()
99	}
100
101	fn get_entries(response: <Self::URL as RequestURL>::Response) -> impl IntoIterator<Item=Self>
102	where
103		Self: Sized
104	{
105		response.awards
106	}
107
108	#[cfg(feature = "cache")]
109	fn get_cache_table() -> &'static RwLock<CacheTable<Self>>
110	where
111		Self: Sized
112	{
113		&CACHE
114	}
115}
116
117entrypoint!(AwardId => Award);
118entrypoint!(Award.id => Award);
119
120#[cfg(test)]
121mod tests {
122	use crate::awards::AwardRequest;
123	use crate::request::RequestURLBuilderExt;
124
125	#[tokio::test]
126	async fn parse_this_season() {
127		let _response = AwardRequest::builder().build_and_get().await.unwrap();
128	}
129}