mlb_api/endpoints/jobs/
mod.rs

1use std::fmt::{Display, Formatter};
2use chrono::NaiveDate;
3use serde::Deserialize;
4use crate::endpoints::{JobTypeId, StatsAPIUrl};
5use crate::endpoints::person::Person;
6use crate::endpoints::sports::SportId;
7use crate::gen_params;
8use crate::types::{Copyright, MLB_API_DATE_FORMAT};
9
10pub mod datacasters; // done
11pub mod official_scorers; // done
12pub mod umpire; // done
13
14#[derive(Debug, Deserialize, PartialEq, Eq, Clone)]
15pub struct JobsResponse {
16    pub copyright: Copyright,
17    #[serde(default)]
18    pub roster: Vec<EmployedPerson>,
19}
20
21#[derive(Debug, Deserialize, PartialEq, Eq, Clone)]
22#[serde(rename_all = "camelCase")]
23pub struct EmployedPerson {
24    pub person: Person,
25    #[serde(deserialize_with = "crate::types::try_from_str")]
26    pub jersey_number: Option<u8>,
27    #[serde(rename = "job")] pub job_name: String,
28    pub job_id: JobTypeId,
29    #[serde(rename = "title")] pub job_title: String,
30}
31
32pub struct JobsEndpointUrl {
33    pub job_type: JobTypeId,
34    pub sport_id: Option<SportId>,
35    pub date: Option<NaiveDate>,
36}
37
38impl Display for JobsEndpointUrl {
39    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
40        write!(f, "http://statsapi.mlb.com/api/v1/jobs{}", gen_params! {
41            "jobType": self.job_type,
42            "sportId"?: self.sport_id,
43            "date"?: self.date.as_ref().map(|date| date.format(MLB_API_DATE_FORMAT))
44        })
45    }
46}
47
48impl StatsAPIUrl for JobsEndpointUrl {
49	type Response = JobsResponse;
50}
51
52#[cfg(test)]
53mod tests {
54    use crate::endpoints::{JobType, StatsAPIUrl};
55    use crate::endpoints::jobs::JobsEndpointUrl;
56    use crate::endpoints::meta::MetaEndpointUrl;
57
58    #[tokio::test]
59    async fn parse_all_job_types() {
60        let job_types = MetaEndpointUrl::<JobType>::new().get().await.unwrap().entries;
61        for job_type in job_types.into_iter() {
62            let _response = JobsEndpointUrl { job_type: job_type.id.clone(), sport_id: None, date: None }.get().await.unwrap();
63        }
64    }
65}