diff --git a/src/main.rs b/src/main.rs index ae02115..6c6613a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ mod weather; mod redsox; +use std::collections::HashMap; +use redsox::GameInfo; mod icons; use icons::Icons; use tabled::{Table, Tabled}; @@ -25,7 +27,7 @@ fn main() { y: 59, code: "GYX".to_string(), }.get_full_forecast(); - let sox_games: Vec = redsox::get_schedule(); + let sox_games: HashMap = redsox::get_schedule(); // Build icons. let baseball_icon = Icons::Baseball.get_icon_str(); let clock_icon = Icons::Clock.get_icon_str(); @@ -39,17 +41,20 @@ fn main() { let mut sox_status = String::new(); // Check if there is a sox game and print opp. if game_flag == false { - for sox_game in &sox_games { - if sox_game.date == yyyy_mm_dd { - sox_status = format!("{} {}\n{} {}", &baseball_icon, &sox_game.opponent, &clock_icon, &sox_game.start_time); - game_flag = true; - break; - } + if let Some(sox_game) = sox_games.get(yyyy_mm_dd) { + sox_status = format!( + "{} {}\n{} {}", + &baseball_icon, + &sox_game.opponent, + &clock_icon, + &sox_game.start_time + ); + game_flag = true; } - } - else { + } else { game_flag = false; } + let row = TableRow { date: yyyy_mm_dd.to_string(), time_of_day: forecast_period.name.clone(), diff --git a/src/redsox.rs b/src/redsox.rs index 71c8ae0..b1e46c5 100644 --- a/src/redsox.rs +++ b/src/redsox.rs @@ -1,5 +1,6 @@ use serde::{Deserialize, Serialize}; use serde_alias::serde_alias; +use std::collections::HashMap; use chrono::{DateTime, Utc}; use chrono_tz::US::Eastern; @@ -63,26 +64,27 @@ pub struct GameInfo { pub start_time: String, } -// Gets the full forecast from the response. -pub fn get_schedule() -> Vec { +// Gets the full schedule from the response. +pub fn get_schedule() -> HashMap { let client = reqwest::blocking::Client::new(); let schedule_url = build_api_url(); let schedule_json: String = client.get(&schedule_url).send().expect("Unable to get data").text().unwrap().to_string(); let schedule: Schedule = serde_json::from_str(&schedule_json).expect("JSON was not well-formatted"); - let mut full_schedule: Vec = vec![]; + let mut hashmap_schedule: HashMap = HashMap::new(); let dates = schedule.dates; for date in dates { for game in date.games { let facing = extract_opponent(&game.teams); + let game_date: String = game.official_date.to_string(); let game_info = GameInfo { opponent: facing, - date: game.official_date, + date: game_date, start_time: get_start_time(&game.game_date), }; - full_schedule.push(game_info); + hashmap_schedule.insert(game.official_date, game_info); } } - full_schedule + hashmap_schedule } // Determine who the opponent is from the teams. @@ -107,8 +109,6 @@ fn get_start_time(iso_string: &String) -> String { est_dt.format("%I:%M").to_string() } - - #[cfg(test)] mod team_tests { use super::*; @@ -134,7 +134,6 @@ mod team_tests { fn test_get_start_time() { let iso_string = "2025-04-02T22:35:00Z".to_string(); // UTC time let result = get_start_time(&iso_string); - // EST is UTC-5 or UTC-4 depending on DST. April is typically daylight saving (EDT = UTC-4) assert_eq!(result, "06:35"); // 22:35 UTC == 18:35 EDT == 06:35 PM in 12-hour format }