From bc50527b24c451ee50e7d3f2934c66851f9464d7 Mon Sep 17 00:00:00 2001 From: Antoine Gourlay Date: Fri, 10 Sep 2021 14:24:08 +0200 Subject: [PATCH] fix: do not fail on missing daily/hourly/... forecast data It was always the intent to not fail to deserialize the api results if any of the forecast fields are missing, since the availability of data can vary with location, but somehow the api can either return an empty array (which was supported) or a missing key (which wasn't). This makes a missing key the same as an empty forecast array. --- CHANGELOG.md | 3 +++ src/api/one_call.rs | 6 +++--- src/segments.rs | 10 ++++++---- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b3185a..2358c7c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ ## [Unreleased] - TBD +### Fixes + +* Querying some locations with a hourly/daily forecast segment could sometimes fail when local forecast data wasn't provided by OpenWeather. ## [0.6.1] - 2021-09-10 diff --git a/src/api/one_call.rs b/src/api/one_call.rs index f07f1cb..6dc7304 100644 --- a/src/api/one_call.rs +++ b/src/api/one_call.rs @@ -13,9 +13,9 @@ pub struct OneCallResponse { pub lon: f32, pub timezone_offset: i32, pub current: WeatherData, - pub minutely: Vec, - pub hourly: Vec, - pub daily: Vec, + pub minutely: Option>, + pub hourly: Option>, + pub daily: Option>, pub alerts: Option>, } diff --git a/src/segments.rs b/src/segments.rs index acadbbe..9ce9e8c 100644 --- a/src/segments.rs +++ b/src/segments.rs @@ -751,14 +751,15 @@ impl DailyForecast { resp: &Response, ) -> Result { let resp = resp.as_forecast()?; + let daily = resp.daily.as_deref().unwrap_or_default(); let timezone = FixedOffset::east(resp.timezone_offset); let mut first = true; out.set_color(conf.base_style)?; - let end = resp.daily.len().min(1 + self.days as usize); + let end = daily.len().min(1 + self.days as usize); for i in 1..end { - let day = &resp.daily[i as usize]; + let day = &daily[i as usize]; let dt = day.dt; @@ -831,17 +832,18 @@ impl HourlyForecast { resp: &Response, ) -> Result { let resp = resp.as_forecast()?; + let hourly = resp.hourly.as_deref().unwrap_or_default(); let timezone = FixedOffset::east(resp.timezone_offset); let mut first = true; out.set_color(conf.base_style)?; let hours = self.hours as usize; let step = 1.max(self.step as usize); - let end = resp.hourly.len(); + let end = hourly.len(); let mut i = 0; while i * step + 1 < end && i < hours { - let day = &resp.hourly[i * step + 1]; + let day = &hourly[i * step + 1]; let dt = day.dt;