Don't use Option for the max sample/item return values

If the sample/item series are empty, the function already returns
`None`, so the tuple values are always `Some(_)` which makes the
`Option` type redundant.
This commit is contained in:
Paul van Tilburg 2022-05-08 14:01:22 +02:00
parent 34b63ec94d
commit a0c4e0da77
Signed by untrusted user: paul
GPG Key ID: C6DE073EDA9EEC4D
2 changed files with 18 additions and 22 deletions

View File

@ -148,8 +148,8 @@ pub(crate) async fn forecast(
providers::combined::get(position, metric, maps_handle).await
{
forecast.paqi = Some(paqi);
forecast.aqi_max = aqi_max;
forecast.pollen_max = pollen_max;
forecast.aqi_max = Some(aqi_max);
forecast.pollen_max = Some(pollen_max);
}
}
Metric::PM10 => forecast.pm10 = providers::luchtmeetnet::get(position, metric).await,

View File

@ -46,11 +46,7 @@ impl Item {
fn merge(
pollen_samples: Vec<BuienradarSample>,
aqi_items: Vec<LuchtmeetnetItem>,
) -> Option<(
Vec<Item>,
Option<BuienradarSample>,
Option<LuchtmeetnetItem>,
)> {
) -> Option<(Vec<Item>, BuienradarSample, LuchtmeetnetItem)> {
let mut pollen_samples = pollen_samples;
let mut aqi_items = aqi_items;
@ -85,14 +81,18 @@ fn merge(
}
// Find the maximum sample/item of each series.
// Note: Unwrapping is possible because each series has at least an item otherwise `.first`
// would have failed above.
let pollen_max = pollen_samples
.iter()
.max_by_key(|sample| sample.score)
.cloned();
.cloned()
.unwrap();
let aqi_max = aqi_items
.iter()
.max_by_key(|item| (item.value * 1_000.0) as u32)
.cloned();
.cloned()
.unwrap();
// Combine the samples with items by taking the maximum of pollen sample score and AQI item
// value.
@ -135,11 +135,7 @@ pub(crate) async fn get(
position: Position,
metric: Metric,
maps_handle: &MapsHandle,
) -> Option<(
Vec<Item>,
Option<BuienradarSample>,
Option<LuchtmeetnetItem>,
)> {
) -> Option<(Vec<Item>, BuienradarSample, LuchtmeetnetItem)> {
if metric != Metric::PAQI {
return None;
};
@ -183,7 +179,7 @@ mod tests {
LuchtmeetnetItem::new(t_2, 2.4),
]);
// A normal merge.
// Perform a normal merge.
let merged = super::merge(pollen_samples.clone(), aqi_items.clone());
assert!(merged.is_some());
let (paqi, max_pollen, max_aqi) = merged.unwrap();
@ -195,8 +191,8 @@ mod tests {
Item::new(t_2, 2.4),
])
);
assert_eq!(max_pollen, Some(BuienradarSample::new(t_1, 3)));
assert_eq!(max_aqi, Some(LuchtmeetnetItem::new(t_1, 2.9)));
assert_eq!(max_pollen, BuienradarSample::new(t_1, 3));
assert_eq!(max_aqi, LuchtmeetnetItem::new(t_1, 2.9));
// The pollen samples are shifted, i.e. one hour in the future.
let shifted_pollen_samples = pollen_samples[2..]
@ -211,8 +207,8 @@ mod tests {
assert!(merged.is_some());
let (paqi, max_pollen, max_aqi) = merged.unwrap();
assert_eq!(paqi, Vec::from([Item::new(t_1, 2.9), Item::new(t_2, 3.0),]));
assert_eq!(max_pollen, Some(BuienradarSample::new(t_2, 3)));
assert_eq!(max_aqi, Some(LuchtmeetnetItem::new(t_1, 2.9)));
assert_eq!(max_pollen, BuienradarSample::new(t_2, 3));
assert_eq!(max_aqi, LuchtmeetnetItem::new(t_1, 2.9));
// The AQI items are shifted, i.e. one hour in the future.
let shifted_aqi_items = aqi_items[2..]
@ -227,10 +223,10 @@ mod tests {
assert!(merged.is_some());
let (paqi, max_pollen, max_aqi) = merged.unwrap();
assert_eq!(paqi, Vec::from([Item::new(t_1, 3.0), Item::new(t_2, 2.9),]));
assert_eq!(max_pollen, Some(BuienradarSample::new(t_1, 3)));
assert_eq!(max_aqi, Some(LuchtmeetnetItem::new(t_2, 2.9)));
assert_eq!(max_pollen, BuienradarSample::new(t_1, 3));
assert_eq!(max_aqi, LuchtmeetnetItem::new(t_2, 2.9));
// Merging fails because the samples/items are too far apart.
// Merging fails because the samples/items are too far (6 hours) apart.
let shifted_aqi_items = aqi_items
.iter()
.cloned()