mlb_api/requests/
awards.rs1use crate::league::LeagueId;
2use crate::request::RequestURL;
3use crate::season::SeasonId;
4use crate::sport::SportId;
5use crate::types::Copyright;
6use bon::Builder;
7use serde::Deserialize;
8use std::fmt::{Display, Formatter};
9use crate::cache::Requestable;
10
11#[cfg(feature = "cache")]
12use crate::{rwlock_const_new, RwLock, cache::CacheTable};
13
14#[derive(Debug, Deserialize, PartialEq, Eq, Clone)]
15pub struct AwardsResponse {
16 pub copyright: Copyright,
17 pub awards: Vec<Award>,
18}
19
20#[derive(Debug, Deserialize, Clone)]
21pub struct Award {
22 pub name: String,
23 pub description: Option<String>,
24 pub sport: Option<SportId>,
25 pub league: Option<LeagueId>,
26 pub notes: Option<String>,
27 #[serde(flatten)]
28 pub id: AwardId,
29}
30
31id_only_eq_impl!(Award, id);
32id!(AwardId { id: String });
33
34#[derive(Builder)]
35#[builder(derive(Into))]
36pub struct AwardRequest {
37 #[builder(into)]
38 award_id: Option<AwardId>,
39 #[builder(into)]
40 sport_id: Option<SportId>,
41 #[builder(into)]
42 league_id: Option<LeagueId>,
43 #[builder(into)]
44 season: Option<SeasonId>,
45}
46
47impl<S: award_request_builder::State + award_request_builder::IsComplete> crate::request::RequestURLBuilderExt for AwardRequestBuilder<S> {
48 type Built = AwardRequest;
49}
50
51impl Display for AwardRequest {
52 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
53 write!(
54 f,
55 "http://statsapi.mlb.com/api/v1/awards{}",
56 gen_params! { "awardId"?: self.award_id.as_ref(), "sportId"?: self.sport_id, "leagueId"?: self.league_id, "season"?: self.season }
57 )
58 }
59}
60
61impl RequestURL for AwardRequest {
62 type Response = AwardsResponse;
63}
64
65#[cfg(feature = "cache")]
66static CACHE: RwLock<CacheTable<Award>> = rwlock_const_new(CacheTable::new());
67
68impl Requestable for Award {
69 type Identifier = AwardId;
70 type URL = AwardRequest;
71
72 fn id(&self) -> &Self::Identifier {
73 &self.id
74 }
75
76 #[cfg(feature = "aggressive_cache")]
77 fn url_for_id(_id: &Self::Identifier) -> Self::URL {
78 AwardRequest::builder().build()
79 }
80
81 #[cfg(not(feature = "aggressive_cache"))]
82 fn url_for_id(id: &Self::Identifier) -> Self::URL {
83 AwardRequest::builder().award_id(id.clone()).build()
84 }
85
86 fn get_entries(response: <Self::URL as RequestURL>::Response) -> impl IntoIterator<Item=Self>
87 where
88 Self: Sized
89 {
90 response.awards
91 }
92
93 #[cfg(feature = "cache")]
94 fn get_cache_table() -> &'static RwLock<CacheTable<Self>>
95 where
96 Self: Sized
97 {
98 &CACHE
99 }
100}
101
102entrypoint!(AwardId => Award);
103entrypoint!(Award.id => Award);
104
105#[cfg(test)]
106mod tests {
107 use crate::awards::AwardRequest;
108 use crate::request::RequestURLBuilderExt;
109
110 #[tokio::test]
111 async fn parse_this_season() {
112 let _response = AwardRequest::builder().build_and_get().await.unwrap();
113 }
114}