1use std::{collections::BTreeMap, fmt, future::Future};
2
3use serde::{Deserialize, Serialize};
4use time::{Date, OffsetDateTime};
5
6use crate::{SeasonWeek, TeamAbbr};
7
8#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
9#[serde(transparent)]
10pub struct EspnPlayerId(pub String);
11
12#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
13pub struct LiveSlate {
14 pub date: Date,
15 pub games: Vec<LiveGame>,
16}
17
18#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
19pub struct LiveGame {
20 pub game_id: String,
21 pub tank01_game_id: String,
22 pub season_week: SeasonWeek,
23 pub kickoff: Option<OffsetDateTime>,
24 pub home_team: TeamAbbr,
25 pub away_team: TeamAbbr,
26}
27
28#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
29pub enum LiveGamePhase {
30 Scheduled,
31 InProgress,
32 Final,
33 Unknown,
34}
35
36#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
37pub struct LiveScoreboard {
38 pub games: Vec<LiveScoreboardGame>,
39}
40
41#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
42pub struct LiveScoreboardGame {
43 pub tank01_game_id: String,
44 pub phase: LiveGamePhase,
45 pub period: Option<String>,
46 pub clock: Option<String>,
47 pub home_team: TeamAbbr,
48 pub away_team: TeamAbbr,
49 pub home_score: Option<i32>,
50 pub away_score: Option<i32>,
51}
52
53#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
54pub struct LiveGameSnapshot {
55 pub game: LiveGame,
56 pub observed_at: OffsetDateTime,
57 pub phase: LiveGamePhase,
58 pub period: Option<String>,
59 pub clock: Option<String>,
60 pub home_score: Option<i32>,
61 pub away_score: Option<i32>,
62 pub players: Option<Vec<LivePlayerStats>>,
63 pub defenses: Option<Vec<LiveTeamStats>>,
64}
65
66#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
67pub struct LivePlayerStats {
68 pub espn_id: Option<EspnPlayerId>,
69 pub name: Option<String>,
70 pub team: Option<TeamAbbr>,
71 pub position: Option<String>,
72 pub opponent: Option<TeamAbbr>,
73 pub completions: u32,
74 pub attempts: u32,
75 pub passing_tds: u32,
76 pub passing_interceptions: u32,
77 pub rushing_attempts: u32,
78 pub rushing_tds: u32,
79 pub targets: u32,
80 pub receptions: u32,
81 pub receiving_tds: u32,
82 pub fumbles_lost: u32,
83 pub two_point_conversions: u32,
84 pub special_teams_tds: u32,
85 pub fumble_recovery_tds: u32,
86 pub passing_yards: i32,
87 pub rushing_yards: i32,
88 pub receiving_yards: i32,
89}
90
91#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
92pub struct LiveTeamStats {
93 pub team: TeamAbbr,
94 pub opponent: TeamAbbr,
95 pub sacks: u32,
96 pub interceptions: u32,
97 pub fumble_recoveries: u32,
98 pub safeties: u32,
99 pub touchdowns: u32,
100 pub blocked_kicks: u32,
101 pub conversion_returns: u32,
102 pub points_allowed: Option<i32>,
104 pub dst_present: bool,
105 pub team_stats_present: bool,
106}
107
108#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
109pub enum RawBody {
110 Json(serde_json::Value),
111 Text(String),
112}
113
114#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, thiserror::Error)]
115pub enum LiveProviderError {
116 #[error("transport error: {message}")]
117 Transport { message: String },
118 #[error("provider returned HTTP {status}: {message}")]
119 Http { status: u16, message: String },
120 #[error("provider JSON error: {message}")]
121 Json { message: String },
122 #[error("provider normalization error: {message}")]
123 Normalization { message: String },
124 #[error("provider identity conflict: {message}")]
125 IdentityConflict { message: String },
126}
127
128#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
129pub enum ProviderOutcome<T> {
130 Value(T),
131 Pregame { message: String },
132 Error(LiveProviderError),
133}
134
135#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
136pub struct ProviderResponse<T> {
137 pub requested_at: OffsetDateTime,
138 pub received_at: OffsetDateTime,
139 pub elapsed_ms: u64,
140 pub http_status: Option<u16>,
141 pub headers: BTreeMap<String, String>,
142 pub raw_body: Option<RawBody>,
143 pub outcome: ProviderOutcome<T>,
144}
145
146pub trait LiveScoreProvider: Send + Sync {
147 fn scoreboard(
148 &self,
149 date: Date,
150 ) -> impl Future<Output = ProviderResponse<LiveScoreboard>> + Send;
151
152 fn box_score(
153 &self,
154 game: &LiveGame,
155 play_by_play: bool,
156 ) -> impl Future<Output = ProviderResponse<LiveGameSnapshot>> + Send;
157}
158
159#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, thiserror::Error)]
160#[serde(rename_all = "snake_case")]
161pub enum InjuryDesignation {
162 Questionable,
163 Doubtful,
164 Out,
165 InjuredReserve,
166}
167
168impl fmt::Display for InjuryDesignation {
169 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
170 f.write_str(match self {
171 Self::Questionable => "Q",
172 Self::Doubtful => "D",
173 Self::Out => "O",
174 Self::InjuredReserve => "IR",
175 })
176 }
177}
178
179#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
180pub struct InjuryEntry {
181 pub espn_id: Option<EspnPlayerId>,
182 pub name: String,
183 pub team: Option<TeamAbbr>,
184 pub position: Option<String>,
185 pub designation: InjuryDesignation,
186}
187
188#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
189pub struct InjuryReport {
190 pub report_date: Option<Date>,
192 pub entries: Vec<InjuryEntry>,
193}
194
195pub trait InjuryProvider: Send + Sync {
196 fn current_injuries(&self) -> impl Future<Output = ProviderResponse<InjuryReport>> + Send;
197}
198
199#[cfg(test)]
200mod tests {
201 use super::*;
202
203 #[test]
204 fn designation_serde_and_display_use_short_codes() {
205 let round = |json: &str, value: InjuryDesignation, code: &str| {
206 assert_eq!(
207 serde_json::from_str::<InjuryDesignation>(json).unwrap(),
208 value
209 );
210 assert_eq!(serde_json::to_string(&value).unwrap(), json);
211 assert_eq!(value.to_string(), code);
212 };
213 round("\"questionable\"", InjuryDesignation::Questionable, "Q");
214 round("\"doubtful\"", InjuryDesignation::Doubtful, "D");
215 round("\"out\"", InjuryDesignation::Out, "O");
216 round(
217 "\"injured_reserve\"",
218 InjuryDesignation::InjuredReserve,
219 "IR",
220 );
221 }
222
223 #[test]
224 fn injury_entries_and_reports_roundtrip() {
225 let report = InjuryReport {
226 report_date: Some(time::macros::date!(2026 - 09 - 22)),
227 entries: vec![InjuryEntry {
228 espn_id: Some(EspnPlayerId("4044138".into())),
229 name: "Matt Hennessy".into(),
230 team: Some(TeamAbbr::DAL),
231 position: Some("C".into()),
232 designation: InjuryDesignation::InjuredReserve,
233 }],
234 };
235 let text = serde_json::to_string(&report).unwrap();
236 assert_eq!(serde_json::from_str::<InjuryReport>(&text).unwrap(), report);
237 assert!(text.contains(r#""designation":"injured_reserve""#));
238 assert!(text.contains("2026"));
239 }
240}