mlb_api/requests/team/
personnel.rs1use crate::person::PeopleResponse;
4use crate::season::SeasonId;
5use crate::team::TeamId;
6use crate::MLB_API_DATE_FORMAT;
7use crate::request::RequestURL;
8use bon::Builder;
9use chrono::NaiveDate;
10use std::fmt::{Display, Formatter};
11use crate::person::PersonHydrations;
12
13#[derive(Builder)]
14#[builder(derive(Into))]
15pub struct PersonnelRequest<H: PersonHydrations = ()> {
16 #[builder(into)]
17 team_id: TeamId,
18 #[builder(into)]
19 season: Option<SeasonId>,
20 date: Option<NaiveDate>,
21 hydrations: H::RequestData,
22}
23
24impl PersonnelRequest {
25 pub fn for_team(team_id: impl Into<TeamId>) -> PersonnelRequestBuilder<(), personnel_request_builder::SetHydrations<personnel_request_builder::SetTeamId>> {
26 Self::builder()
27 .team_id(team_id)
28 .hydrations(())
29 }
30}
31
32impl<H: PersonHydrations, S: personnel_request_builder::State + personnel_request_builder::IsComplete> crate::request::RequestURLBuilderExt for PersonnelRequestBuilder<H, S> {
33 type Built = PersonnelRequest<H>;
34}
35
36impl<H: PersonHydrations> Display for PersonnelRequest<H> {
37 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
38 let hydrations = Some(H::hydration_text(&self.hydrations)).filter(|h| !h.is_empty());
39 write!(f, "http://statsapi.mlb.com/api/v1/teams/{}/personnel{}", self.team_id, gen_params! { "season"?: self.season, "date"?: self.date.as_ref().map(|date| date.format(MLB_API_DATE_FORMAT)), "hydrate"?: hydrations })
40 }
41}
42
43impl<H: PersonHydrations> RequestURL for PersonnelRequest<H> {
44 type Response = PeopleResponse<H>;
45}
46
47#[cfg(test)]
48mod tests {
49 use crate::request::RequestURLBuilderExt;
50 use crate::team::personnel::PersonnelRequest;
51 use crate::team::TeamsRequest;
52 use crate::TEST_YEAR;
53
54 #[tokio::test]
55 #[cfg_attr(not(feature = "_heavy_tests"), ignore)]
56 async fn test_heavy() {
57 let season = TEST_YEAR;
58 let teams = TeamsRequest::mlb_teams().season(season).build_and_get().await.unwrap();
59 for team in teams.teams {
60 let _ = PersonnelRequest::<()>::for_team(team.id).season(season).build_and_get().await.unwrap();
61 }
62 }
63}