Number forecast output per metric (for now)

Also don't include a `null` value in the JSON if it is not enabled.
This commit is contained in:
Paul van Tilburg 2022-02-12 15:01:02 +01:00
parent 0b5e980f04
commit ebf1b0618b
Signed by untrusted user: paul
GPG Key ID: C6DE073EDA9EEC4D
1 changed files with 50 additions and 38 deletions

View File

@ -26,32 +26,44 @@ use rocket::{get, launch, routes, FromFormField};
struct Forecast { struct Forecast {
/// The latitude of the position. /// The latitude of the position.
lat: f64, lat: f64,
/// The longitude of the position. /// The longitude of the position.
lon: f64, lon: f64,
/// The current time (in seconds since the UNIX epoch). /// The current time (in seconds since the UNIX epoch).
time: i64, time: i64,
/// The air quality index (if asked for).
#[serde(rename = "AQI")] /// The air quality index (when asked for).
aqi: Option<()>, #[serde(rename = "AQI", skip_serializing_if = "Option::is_none")]
/// The NO₂ concentration (if asked for). aqi: Option<u8>,
#[serde(rename = "NO2")]
no2: Option<()>, /// The NO₂ concentration (when asked for).
#[serde(rename = "O3")] #[serde(rename = "NO2", skip_serializing_if = "Option::is_none")]
/// The O₃ concentration (if asked for). no2: Option<u8>,
o3: Option<()>,
#[serde(rename = "PAQI")] /// The O₃ concentration (when asked for).
/// The FIXME air quality index (if asked for). #[serde(rename = "O3", skip_serializing_if = "Option::is_none")]
paqi: Option<()>, o3: Option<u8>,
#[serde(rename = "PM10")]
/// The particulate matter in the air (if asked for). /// The FIXME air quality index (when asked for).
pm10: Option<()>, #[serde(rename = "PAQI", skip_serializing_if = "Option::is_none")]
/// The pollen in the air (if asked for). paqi: Option<u8>,
pollen: Option<()>,
/// The precipitation (if asked for). /// The particulate matter in the air (when asked for).
precipitation: Option<()>, #[serde(rename = "PM10", skip_serializing_if = "Option::is_none")]
/// The UV index (if asked for). pm10: Option<u8>,
#[serde(rename = "UVI")]
uvi: Option<()>, /// The pollen in the air (when asked for).
#[serde(skip_serializing_if = "Option::is_none")]
pollen: Option<u8>,
/// The precipitation (when asked for).
#[serde(skip_serializing_if = "Option::is_none")]
precipitation: Option<u8>,
/// The UV index (when asked for).
#[serde(rename = "UVI", skip_serializing_if = "Option::is_none")]
uvi: Option<u8>,
} }
impl Forecast { impl Forecast {
@ -103,23 +115,23 @@ async fn forecast(lat: f64, lon: f64, metrics: Vec<Metric>) -> Forecast {
match metric { match metric {
// TODO: Find a way to handle the "All" case more gracefully! // TODO: Find a way to handle the "All" case more gracefully!
Metric::All => { Metric::All => {
forecast.aqi = Some(()); forecast.aqi = Some(1);
forecast.no2 = Some(()); forecast.no2 = Some(2);
forecast.o3 = Some(()); forecast.o3 = Some(3);
forecast.paqi = Some(()); forecast.paqi = Some(4);
forecast.pm10 = Some(()); forecast.pm10 = Some(5);
forecast.pollen = Some(()); forecast.pollen = Some(6);
forecast.precipitation = Some(()); forecast.precipitation = Some(7);
forecast.uvi = Some(()); forecast.uvi = Some(8);
} }
Metric::AQI => forecast.aqi = Some(()), Metric::AQI => forecast.aqi = Some(1),
Metric::NO2 => forecast.no2 = Some(()), Metric::NO2 => forecast.no2 = Some(2),
Metric::O3 => forecast.o3 = Some(()), Metric::O3 => forecast.o3 = Some(3),
Metric::PAQI => forecast.paqi = Some(()), Metric::PAQI => forecast.paqi = Some(4),
Metric::PM10 => forecast.pm10 = Some(()), Metric::PM10 => forecast.pm10 = Some(5),
Metric::Pollen => forecast.pollen = Some(()), Metric::Pollen => forecast.pollen = Some(6),
Metric::Precipitation => forecast.precipitation = Some(()), Metric::Precipitation => forecast.precipitation = Some(7),
Metric::UVI => forecast.uvi = Some(()), Metric::UVI => forecast.uvi = Some(8),
} }
} }