Lots of good changes - iter_mut on WeatherPeriods, changing to i32 for temp, add colored, get full schedule.
This commit is contained in:
parent
2d936f6272
commit
aa1893fbf6
@ -1,6 +1,7 @@
|
|||||||
#![cfg_attr(debug_assertions, allow(dead_code, unused_imports))]
|
#![cfg_attr(debug_assertions, allow(dead_code, unused_imports))]
|
||||||
mod weather;
|
mod weather;
|
||||||
mod redsox;
|
mod redsox;
|
||||||
|
use colored::Colorize;
|
||||||
use tabled::{Table, Tabled};
|
use tabled::{Table, Tabled};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tabled::settings::{
|
use tabled::settings::{
|
||||||
@ -12,14 +13,14 @@ use tabled::settings::{
|
|||||||
struct TableRow {
|
struct TableRow {
|
||||||
date: String,
|
date: String,
|
||||||
time_of_day: String,
|
time_of_day: String,
|
||||||
temp: u64,
|
temp: i32,
|
||||||
red_sox: String,
|
red_sox: String,
|
||||||
forecast: String,
|
forecast: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unreachable_code)]
|
#[allow(unreachable_code)]
|
||||||
fn main() {
|
fn main() {
|
||||||
let baseball_diamond = '\u{f15ec}';
|
let baseball_diamond = '\u{f0852}';
|
||||||
// Set the weather location here.
|
// Set the weather location here.
|
||||||
let location = weather::WeatherOfficeLocation {
|
let location = weather::WeatherOfficeLocation {
|
||||||
x: 75,
|
x: 75,
|
||||||
|
|||||||
@ -51,7 +51,7 @@ pub fn get_upcoming_games() -> Vec<Game> {
|
|||||||
// @todo - change this to be a dynamic request from the API endpoint instead of a local file.
|
// @todo - change this to be a dynamic request from the API endpoint instead of a local file.
|
||||||
let schedule_json: String = fs::read_to_string("/Users/danchadwick/Projects/rust/weather/assets/sox-schedule.json").expect("Unable to read file").to_owned();
|
let schedule_json: String = fs::read_to_string("/Users/danchadwick/Projects/rust/weather/assets/sox-schedule.json").expect("Unable to read file").to_owned();
|
||||||
let json: Vec<Game> = serde_json::from_str(&schedule_json).expect("Something not good?");
|
let json: Vec<Game> = serde_json::from_str(&schedule_json).expect("Something not good?");
|
||||||
let upcoming_games: &Vec<Game> = &json.into_iter().take(7).collect();
|
let upcoming_games: &Vec<Game> = &json.into_iter().collect();
|
||||||
upcoming_games.to_owned()
|
upcoming_games.to_owned()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -17,7 +17,7 @@ struct Properties {
|
|||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct WeatherPeriod {
|
pub struct WeatherPeriod {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub temperature: u64,
|
pub temperature: i32,
|
||||||
pub wind_direction: String,
|
pub wind_direction: String,
|
||||||
pub wind_speed: String,
|
pub wind_speed: String,
|
||||||
pub detailed_forecast: String,
|
pub detailed_forecast: String,
|
||||||
@ -54,29 +54,17 @@ pub fn get_full_forecast(location: WeatherOfficeLocation) -> Vec<WeatherPeriod>
|
|||||||
.text().unwrap().to_string();
|
.text().unwrap().to_string();
|
||||||
let json: ForecastWrapper = serde_json::from_str(&forecast).expect("JSON was not well-formatted");
|
let json: ForecastWrapper = serde_json::from_str(&forecast).expect("JSON was not well-formatted");
|
||||||
let weather_periods: Vec<WeatherPeriod> = json.properties.periods.into_iter().collect();
|
let weather_periods: Vec<WeatherPeriod> = json.properties.periods.into_iter().collect();
|
||||||
let icon_forecasts = enhance_forecasts(&weather_periods);
|
let icon_forecasts = enhance_forecasts(weather_periods);
|
||||||
|
|
||||||
icon_forecasts
|
icon_forecasts
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn enhance_forecasts(periods: &Vec<WeatherPeriod>) -> Vec<WeatherPeriod> {
|
pub fn enhance_forecasts(mut periods: Vec<WeatherPeriod>) -> Vec<WeatherPeriod> {
|
||||||
let mut rebuilt_periods: Vec<WeatherPeriod> = vec![];
|
for period in periods.iter_mut() {
|
||||||
for period in periods {
|
|
||||||
let icon = detect_icon(&period.short_forecast).unwrap();
|
let icon = detect_icon(&period.short_forecast).unwrap();
|
||||||
let icon_forecast = format!("{} {}", icon, &period.detailed_forecast);
|
let icon_forecast = format!("{} {}", icon, &period.detailed_forecast);
|
||||||
let rebuilt_period = WeatherPeriod {
|
period.detailed_forecast = icon_forecast;
|
||||||
name: period.name.to_string(),
|
|
||||||
temperature: period.temperature,
|
|
||||||
wind_direction: period.wind_direction.to_string(),
|
|
||||||
wind_speed: period.wind_speed.to_string(),
|
|
||||||
detailed_forecast: icon_forecast,
|
|
||||||
short_forecast: period.short_forecast.to_string(),
|
|
||||||
start_time: period.start_time.to_string(),
|
|
||||||
};
|
|
||||||
rebuilt_periods.push(rebuilt_period);
|
|
||||||
}
|
}
|
||||||
|
periods
|
||||||
rebuilt_periods
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn detect_icon(short_forecast: &String) -> Option<char> {
|
pub fn detect_icon(short_forecast: &String) -> Option<char> {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user