Skip to main content

mlb_api/requests/meta/
hit_trajectories.rs

1use derive_more::Display;
2use serde::Deserialize;
3
4/// Different coarse definitions of how a ball is hit -- likely up to scorers interpretation.
5#[derive(Debug, Deserialize, PartialEq, Eq, Copy, Clone, Display, Hash)]
6#[serde(try_from = "__HitTrajectoryStruct")]
7pub enum HitTrajectory {
8	#[display("Bunt - Ground Ball")]
9	BuntGrounder,
10
11	#[display("Bunt - Popup")]
12	BuntPopup,
13
14	#[display("Bunt - Line Drive")]
15	BuntLineDrive,
16
17	#[display("Line Drive")]
18	LineDrive,
19
20	#[display("Ground Ball")]
21	GroundBall,
22
23	#[display("Fly Ball")]
24	FlyBall,
25
26	#[display("Popup")]
27	Popup,
28}
29
30impl HitTrajectory {
31	/// Uses launch angle data to derive a hit trajectory.
32	#[must_use]
33	pub const fn from_launch_angle(launch_angle: f64) -> Self {
34		match launch_angle {
35			..10.0 => Self::GroundBall,
36			10.0..25.0 => Self::LineDrive,
37			25.0..50.0 => Self::FlyBall,
38			_ => Self::Popup,
39		}
40	}
41}
42
43#[derive(Deserialize)]
44#[doc(hidden)]
45#[serde(untagged)]
46enum __HitTrajectoryStruct {
47	Wrapped { code: String },
48	Inline(String),
49}
50
51impl TryFrom<__HitTrajectoryStruct> for HitTrajectory {
52	type Error = &'static str;
53
54	fn try_from((__HitTrajectoryStruct::Wrapped { code } | __HitTrajectoryStruct::Inline(code)): __HitTrajectoryStruct) -> Result<Self, Self::Error> {
55		Ok(match &*code {
56			"bunt_grounder" => Self::BuntGrounder,
57			"bunt_popup" => Self::BuntPopup,
58			"bunt_line_drive" => Self::BuntLineDrive,
59			"line_drive" => Self::LineDrive,
60			"ground_ball" => Self::GroundBall,
61			"fly_ball" => Self::FlyBall,
62			"popup" => Self::Popup,
63			_ => return Err("unknown hit trajectory"),
64		})
65	}
66}
67
68meta_kind_impl!("hitTrajectories" => HitTrajectory);
69static_request_entry_cache_impl!(HitTrajectory);
70test_impl!(HitTrajectory);