Skip to content

Commit

Permalink
fix: do not fail on missing daily/hourly/... forecast data
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gourlaysama committed Sep 10, 2021
1 parent c8603e5 commit bc50527
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<!-- next-header -->
## [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

Expand Down
6 changes: 3 additions & 3 deletions src/api/one_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ pub struct OneCallResponse {
pub lon: f32,
pub timezone_offset: i32,
pub current: WeatherData,
pub minutely: Vec<MinutelyForecast>,
pub hourly: Vec<WeatherData>,
pub daily: Vec<WeatherData>,
pub minutely: Option<Vec<MinutelyForecast>>,
pub hourly: Option<Vec<WeatherData>>,
pub daily: Option<Vec<WeatherData>>,
pub alerts: Option<Vec<Alert>>,
}

Expand Down
10 changes: 6 additions & 4 deletions src/segments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,14 +751,15 @@ impl DailyForecast {
resp: &Response,
) -> Result<RenderStatus> {
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;

Expand Down Expand Up @@ -831,17 +832,18 @@ impl HourlyForecast {
resp: &Response,
) -> Result<RenderStatus> {
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;

Expand Down

0 comments on commit bc50527

Please sign in to comment.