Skip to main content

nfl_data/
nflverse.rs

1//! nflverse-backed reads and test seeding.
2
3use nflverse_data::NflverseData;
4
5use crate::{
6    Game, NflData, NflDataError, Player, PlayerWeekStats, Season, SeasonWeek, TeamAbbr,
7    TeamWeekStats, WeeklyRosterEntry,
8};
9
10/// All cached games for a season, ordered by week.
11pub(crate) async fn games(
12    nflverse: &NflverseData,
13    season: Season,
14) -> Result<Vec<Game>, NflDataError> {
15    nflverse.games(season).await.map_err(NflDataError::Nflverse)
16}
17
18impl NflData {
19    /// All cached players, ordered by name.
20    pub async fn players(&self) -> Result<Vec<Player>, NflDataError> {
21        self.nflverse
22            .players()
23            .await
24            .map_err(NflDataError::Nflverse)
25    }
26
27    /// A team's cached roster for one week, ordered by player name.
28    pub async fn weekly_roster(
29        &self,
30        season_week: SeasonWeek,
31        team: &TeamAbbr,
32    ) -> Result<Vec<WeeklyRosterEntry>, NflDataError> {
33        self.nflverse
34            .weekly_roster(season_week, team)
35            .await
36            .map_err(NflDataError::Nflverse)
37    }
38
39    /// All players' cached stat lines for one week, ordered by player GSIS id.
40    pub async fn player_week_stats(
41        &self,
42        season_week: SeasonWeek,
43    ) -> Result<Vec<PlayerWeekStats>, NflDataError> {
44        self.nflverse
45            .player_week_stats(season_week)
46            .await
47            .map_err(NflDataError::Nflverse)
48    }
49
50    /// All teams' pbp-derived DST lines for one week (sacks, takeaways, TDs,
51    /// blocks, conversion returns, and points_allowed = the opponent's
52    /// offensive points). Ordered by team.
53    pub async fn team_week_stats(
54        &self,
55        season_week: SeasonWeek,
56    ) -> Result<Vec<TeamWeekStats>, NflDataError> {
57        self.nflverse
58            .team_week_stats(season_week)
59            .await
60            .map_err(NflDataError::Nflverse)
61    }
62
63    /// Seed weekly roster entries directly, for tests and tooling.
64    #[doc(hidden)]
65    pub async fn seed_weekly_roster_for_test(
66        &self,
67        entries: &[WeeklyRosterEntry],
68    ) -> Result<(), NflDataError> {
69        self.nflverse
70            .seed_weekly_roster_for_test(entries)
71            .await
72            .map_err(NflDataError::Nflverse)
73    }
74
75    /// Seed players and games directly, for tests and tooling.
76    #[doc(hidden)]
77    pub async fn seed_for_test(
78        &self,
79        players: &[Player],
80        games: &[Game],
81    ) -> Result<(), NflDataError> {
82        self.nflverse
83            .seed_for_test(players, games)
84            .await
85            .map_err(NflDataError::Nflverse)
86    }
87
88    /// Seed player and team week stats directly, for tests and tooling. Reuses the
89    /// sync-path `replace` inserts (per-season delete-then-insert). Not part of the
90    /// stable API.
91    #[doc(hidden)]
92    pub async fn seed_week_stats_for_test(
93        &self,
94        season: Season,
95        players: &[PlayerWeekStats],
96        teams: &[TeamWeekStats],
97    ) -> Result<(), NflDataError> {
98        self.nflverse
99            .seed_week_stats_for_test(season, players, teams)
100            .await
101            .map_err(NflDataError::Nflverse)
102    }
103}
104
105#[cfg(test)]
106mod tests {
107    use crate::{NflData, Season, SeasonWeek, Week};
108
109    #[tokio::test]
110    async fn seeds_player_and_team_week_stats() {
111        let nfl = NflData::in_memory().await.unwrap();
112        let mut p = crate::PlayerWeekStats {
113            season_week: SeasonWeek::new(Season(2025), Week(1)).unwrap(),
114            gsis_id: "00-RB".into(),
115            team: crate::TeamAbbr::KC,
116            opponent: None,
117            completions: 0,
118            attempts: 0,
119            passing_yards: 0,
120            passing_tds: 0,
121            passing_interceptions: 0,
122            rushing_attempts: 0,
123            rushing_yards: 0,
124            rushing_tds: 0,
125            targets: 0,
126            receptions: 0,
127            receiving_yards: 0,
128            receiving_tds: 0,
129            fumbles_lost: 0,
130            two_point_conversions: 0,
131            special_teams_tds: 0,
132            fumble_recovery_tds: 0,
133        };
134        p.rushing_yards = 42;
135
136        let team = crate::TeamWeekStats {
137            season_week: SeasonWeek::new(Season(2025), Week(1)).unwrap(),
138            team: crate::TeamAbbr::KC,
139            opponent: crate::TeamAbbr::BUF,
140            game_id: "2025_01_BUF_KC".into(),
141            sacks: 3,
142            interceptions: 1,
143            fumble_recoveries: 0,
144            safeties: 0,
145            touchdowns: 0,
146            blocked_kicks: 0,
147            conversion_returns: 0,
148            points_allowed: 17,
149        };
150        p.rushing_tds = 1;
151        nfl.seed_week_stats_for_test(Season(2025), &[p], std::slice::from_ref(&team))
152            .await
153            .unwrap();
154        let week = SeasonWeek::new(Season(2025), Week(1)).unwrap();
155        let got = nfl.player_week_stats(week).await.unwrap();
156        assert_eq!(got.len(), 1);
157        assert_eq!(got[0].gsis_id, "00-RB");
158        assert_eq!(got[0].rushing_yards, 42);
159        assert_eq!(nfl.team_week_stats(week).await.unwrap(), [team]);
160        assert_eq!(got[0].rushing_tds, 1);
161        assert!(
162            nfl.nflverse
163                .freshness()
164                .await
165                .unwrap()
166                .iter()
167                .all(|row| row.assets == 0)
168        );
169    }
170}