//! Fetch command implementation. //! //! Downloads car database, tracks database, and per-car timing data //! from remote repositories. use top_drives::{AppError, get_file}; use crate::parsing::fetch_times; /// Base URL for the remote data repository on GitHub. /// Single place to update if the repository path or branch changes. const GITHUB_RAW_BASE: &str = "https://github.com/TiagoXavi/topdrives-records/raw/refs/heads/main/src/database"; /// Returns the full URL for the car database file. fn cars_db_url() -> String { format!("{GITHUB_RAW_BASE}/cars_final.json") } /// Returns the full URL for the tracks database file. fn tracks_db_url() -> String { format!("{GITHUB_RAW_BASE}/tracks_repo.json") } /// Downloads the tracks database file from the remote repository. async fn get_tracks_file(tracks_db_path: &str) -> Result<(), AppError> { println!("Fetching tracks file..."); get_file(&tracks_db_url(), tracks_db_path).await } /// Downloads the cars database file from the remote repository. pub async fn get_cars_file(cars_db_path: &str) -> Result<(), AppError> { println!("Fetching cars file..."); get_file(&cars_db_url(), cars_db_path).await } /// Executes the fetch command: downloads all data files. /// /// Downloads the car database, tracks database, and per-car timing files. /// Fails fast on critical dependency failures — the cars DB must be available /// before per-car timing files can be fetched. pub async fn run_fetch(cars_db_path: &str, tracks_db_path: &str, times_dir: &str) -> Result<(), AppError> { get_cars_file(cars_db_path).await?; get_tracks_file(tracks_db_path).await?; fetch_times(cars_db_path, times_dir).await?; println!("Fetch complete — all files downloaded successfully."); Ok(()) }