fanduel_data/lib.rs
1//! FanDuel DFS salary CSV parsing and SQLite storage.
2//!
3//! This crate is primarily used by `nfl-data` to import FanDuel salary CSV files. It parses
4//! CSV rows into [`SalariedPlayer`] records, then stores and retrieves them by NFL season week.
5//!
6//! FanDuel salary CSV files do not include the NFL week, so parsing requires the caller to
7//! provide a [`SeasonWeek`]. Parsing is separate from persistence, allowing the
8//! returned records to be inspected before saving.
9//!
10//! # Importing salaries
11//!
12//! Connect, creating the database and applying this crate's migrations as
13//! needed, parse the export, and save the resulting records.
14//!
15//! ```no_run
16//! use fanduel_data::FanduelData;
17//! use nfl_model::{SeasonWeek, Week};
18//!
19//! # async fn import(database_url: &str, csv_bytes: &[u8]) -> Result<(), Box<dyn std::error::Error>> {
20//! let data = FanduelData::connect(database_url).await?;
21//! let week = SeasonWeek::from_week(Week(1)).expect("valid season week");
22//! let salaries = data.parse_fanduel_csv(week, csv_bytes)?;
23//! data.save_salaried_players(&salaries).await?;
24//!
25//! let stored = data.salaried_players_for(week).await?;
26//! println!("Stored {} salary entries", stored.len());
27//! # Ok(())
28//! # }
29//! ```
30//!
31//! [DFS positions](nfl_model::DfsPosition) and [team abbreviations](nfl_model::TeamAbbr) use
32//! shared types from `nfl-model`. [`DST`](nfl_model::DfsPosition::Dst) entries represent team
33//! defenses and have no [`Player`]; entries at other positions contain a player.
34//! [`weekly_unmatched_counts`](FanduelData::weekly_unmatched_counts) counts unmatched player
35//! entries only, excluding defenses.
36
37mod csv;
38mod error;
39mod models;
40mod store;
41
42pub use error::FanduelDataError;
43pub use models::{Game, Matchup, Player, SalariedPlayer};
44use std::collections::BTreeMap;
45
46use csv::parse_csv;
47use nfl_model::SeasonWeek;
48use store::Store;
49
50/// A handle for parsing and storing FanDuel salary data in SQLite.
51///
52/// Clone values share the same underlying database connection pool.
53#[derive(Clone)]
54pub struct FanduelData {
55 store: Store,
56}
57
58impl FanduelData {
59 /// Connect, creating the database and applying this crate's migrations
60 /// if needed. Migration applies are idempotent.
61 pub async fn connect(database_url: &str) -> Result<Self, FanduelDataError> {
62 Ok(Self {
63 store: Store::connect(database_url).await?,
64 })
65 }
66
67 /// Create an empty in-memory database and apply all migrations.
68 ///
69 /// This constructor is primarily intended for tests.
70 pub async fn in_memory() -> Result<Self, FanduelDataError> {
71 Ok(Self {
72 store: Store::in_memory().await?,
73 })
74 }
75
76 /// Parse a FanDuel salary CSV into typed records without saving them.
77 ///
78 /// The season week must be provided because the CSV does not identify the NFL week it
79 /// represents. Review or modify the returned records before passing them to
80 /// [`save_salaried_players`](Self::save_salaried_players).
81 pub fn parse_fanduel_csv(
82 &self,
83 season_week: SeasonWeek,
84 csv_data: &[u8],
85 ) -> Result<Vec<SalariedPlayer>, FanduelDataError> {
86 parse_csv(season_week, csv_data)
87 }
88
89 /// Save salary records to the database, typically after parsing them with
90 /// [`parse_fanduel_csv`](Self::parse_fanduel_csv).
91 ///
92 /// Existing records for the same player and game (or defense team and game) are updated,
93 /// making repeated imports safe. The entire slice is saved as one transaction.
94 pub async fn save_salaried_players(
95 &self,
96 salaries: &[SalariedPlayer],
97 ) -> Result<(), FanduelDataError> {
98 self.store.save_salaried_players(salaries).await
99 }
100
101 /// Count all salary entries for each season week present in the database.
102 ///
103 /// Weeks with no entries are omitted.
104 pub async fn weekly_salary_counts(
105 &self,
106 ) -> Result<BTreeMap<SeasonWeek, usize>, FanduelDataError> {
107 self.store.salary_counts_by_week().await
108 }
109
110 /// Count unmatched player salary entries by season week.
111 ///
112 /// `matched_ids` lists player IDs that have been matched elsewhere. Defense (`DST`) entries
113 /// are excluded, and weeks with no unmatched players are omitted.
114 pub async fn weekly_unmatched_counts(
115 &self,
116 matched_ids: &[u32],
117 ) -> Result<BTreeMap<SeasonWeek, usize>, FanduelDataError> {
118 self.store.unmatched_counts_by_week(matched_ids).await
119 }
120
121 /// Return all player and defense salary entries for a season week.
122 ///
123 /// Returns an empty vector when the database has no entries for that week.
124 pub async fn salaried_players_for(
125 &self,
126 season_week: SeasonWeek,
127 ) -> Result<Vec<SalariedPlayer>, FanduelDataError> {
128 self.store.salaried_players_for(season_week).await
129 }
130}