Changing schedule to hashmap for more efficient lookup.

This commit is contained in:
calcu1on 2025-04-15 10:50:52 -04:00
parent 920c093ecf
commit a1add6d2f2
2 changed files with 22 additions and 18 deletions

View File

@ -1,5 +1,7 @@
mod weather; mod weather;
mod redsox; mod redsox;
use std::collections::HashMap;
use redsox::GameInfo;
mod icons; mod icons;
use icons::Icons; use icons::Icons;
use tabled::{Table, Tabled}; use tabled::{Table, Tabled};
@ -25,7 +27,7 @@ fn main() {
y: 59, y: 59,
code: "GYX".to_string(), code: "GYX".to_string(),
}.get_full_forecast(); }.get_full_forecast();
let sox_games: Vec<redsox::GameInfo> = redsox::get_schedule(); let sox_games: HashMap<String,GameInfo> = redsox::get_schedule();
// Build icons. // Build icons.
let baseball_icon = Icons::Baseball.get_icon_str(); let baseball_icon = Icons::Baseball.get_icon_str();
let clock_icon = Icons::Clock.get_icon_str(); let clock_icon = Icons::Clock.get_icon_str();
@ -39,17 +41,20 @@ fn main() {
let mut sox_status = String::new(); let mut sox_status = String::new();
// Check if there is a sox game and print opp. // Check if there is a sox game and print opp.
if game_flag == false { if game_flag == false {
for sox_game in &sox_games { if let Some(sox_game) = sox_games.get(yyyy_mm_dd) {
if sox_game.date == yyyy_mm_dd { sox_status = format!(
sox_status = format!("{} {}\n{} {}", &baseball_icon, &sox_game.opponent, &clock_icon, &sox_game.start_time); "{} {}\n{} {}",
game_flag = true; &baseball_icon,
break; &sox_game.opponent,
} &clock_icon,
&sox_game.start_time
);
game_flag = true;
} }
} } else {
else {
game_flag = false; game_flag = false;
} }
let row = TableRow { let row = TableRow {
date: yyyy_mm_dd.to_string(), date: yyyy_mm_dd.to_string(),
time_of_day: forecast_period.name.clone(), time_of_day: forecast_period.name.clone(),

View File

@ -1,5 +1,6 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_alias::serde_alias; use serde_alias::serde_alias;
use std::collections::HashMap;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use chrono_tz::US::Eastern; use chrono_tz::US::Eastern;
@ -63,26 +64,27 @@ pub struct GameInfo {
pub start_time: String, pub start_time: String,
} }
// Gets the full forecast from the response. // Gets the full schedule from the response.
pub fn get_schedule() -> Vec<GameInfo> { pub fn get_schedule() -> HashMap<String,GameInfo> {
let client = reqwest::blocking::Client::new(); let client = reqwest::blocking::Client::new();
let schedule_url = build_api_url(); 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_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 schedule: Schedule = serde_json::from_str(&schedule_json).expect("JSON was not well-formatted");
let mut full_schedule: Vec<GameInfo> = vec![]; let mut hashmap_schedule: HashMap<String,GameInfo> = HashMap::new();
let dates = schedule.dates; let dates = schedule.dates;
for date in dates { for date in dates {
for game in date.games { for game in date.games {
let facing = extract_opponent(&game.teams); let facing = extract_opponent(&game.teams);
let game_date: String = game.official_date.to_string();
let game_info = GameInfo { let game_info = GameInfo {
opponent: facing, opponent: facing,
date: game.official_date, date: game_date,
start_time: get_start_time(&game.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. // 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() est_dt.format("%I:%M").to_string()
} }
#[cfg(test)] #[cfg(test)]
mod team_tests { mod team_tests {
use super::*; use super::*;
@ -134,7 +134,6 @@ mod team_tests {
fn test_get_start_time() { fn test_get_start_time() {
let iso_string = "2025-04-02T22:35:00Z".to_string(); // UTC time let iso_string = "2025-04-02T22:35:00Z".to_string(); // UTC time
let result = get_start_time(&iso_string); 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) // 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 assert_eq!(result, "06:35"); // 22:35 UTC == 18:35 EDT == 06:35 PM in 12-hour format
} }