Skip to main content

mlb_api/requests/stats/wrappers/
mod.rs

1mod career;
2mod map;
3mod season;
4mod position_and_season;
5mod team;
6mod game;
7mod player;
8mod player_and_team;
9mod single_matchup;
10mod accumulated_matchup;
11mod accumulated_vs_player_matchup;
12mod accumulated_vs_team;
13mod accumulated_vs_team_seasonal;
14mod month;
15mod weekday;
16mod home_away;
17mod win_loss;
18mod with_none;
19
20pub use career::*;
21pub use map::*;
22pub use season::*;
23pub use position_and_season::*;
24pub use team::*;
25pub use game::*;
26pub use player::*;
27pub use player_and_team::*;
28pub use single_matchup::*;
29pub use accumulated_matchup::*;
30pub use accumulated_vs_player_matchup::*;
31pub use accumulated_vs_team::*;
32pub use accumulated_vs_team_seasonal::*;
33pub use month::*;
34pub use weekday::*;
35pub use home_away::*;
36pub use win_loss::*;
37pub use with_none::*;
38
39use std::convert::Infallible;
40use chrono::{Month, Weekday};
41use crate::game::GameId;
42use crate::meta::GameType;
43use crate::league::NamedLeague;
44use crate::person::NamedPerson;
45use crate::meta::NamedPosition;
46use crate::season::SeasonId;
47use crate::stats::{SingletonSplitStat, Stat};
48use crate::team::NamedTeam;
49
50impl<T: SingletonSplitStat> Stat for Vec<T> {
51	type Split = T;
52	type TryFromSplitError = Infallible;
53
54	fn from_splits(splits: impl Iterator<Item=Self::Split>) -> Result<Self, Self::TryFromSplitError>
55	where
56		Self: Sized
57	{
58		Ok(splits.collect::<Self>())
59	}
60}
61
62macro_rules! piece {
63    ($name:ident => $ty:ty) => {
64		::pastey::paste! {
65			pub trait [<$name Piece>] {
66				fn [<$name:snake>](&self) -> &$ty;
67			}
68
69			pub struct [<By $name>];
70
71			impl<T: [<$name Piece>]> MapKey<T> for [<By $name>] {
72				type Key = $ty;
73
74				fn get_key(this: &T) -> Self::Key { this.[<$name:snake>]().clone() }
75			}
76		}
77	};
78}
79
80piece!(Season => SeasonId);
81piece!(Month => Month);
82piece!(Weekday => Weekday);
83piece!(Position => NamedPosition);
84piece!(OpposingTeam => NamedTeam);
85piece!(GameType => GameType);
86piece!(Team => NamedTeam);
87piece!(Pitcher => NamedPerson);
88piece!(Batter => NamedPerson);
89piece!(Player => NamedPerson);
90piece!(Game => GameId);
91piece!(League => NamedLeague);