commit ef89c772a66f01fa358101b50ef869f1bc829a63
parent f3ce23261f6d0a110371eb5b3c7b4cba4878a5a9
Author: Sylvia Ivory <git@sivory.net>
Date: Sun, 15 Jun 2025 22:18:20 -0700
Cleanup weather types
Diffstat:
2 files changed, 29 insertions(+), 19 deletions(-)
diff --git a/crates/weather/src/lib.rs b/crates/weather/src/lib.rs
@@ -59,16 +59,12 @@ impl MeteoClient {
match resp {
MeteoResponse::Success(s) => Ok(s),
- MeteoResponse::Error { error: _, reason } => Err(Error::Meteo(reason)),
+ MeteoResponse::Error { reason } => Err(Error::Meteo(reason)),
}
}
/// Endpoint: `/search`
- pub async fn geocode(
- &self,
- name: &str,
- opt: Option<GeocodeOptions>,
- ) -> Result<Vec<GeocodeResult>> {
+ pub async fn geocode(&self, name: &str, opt: Option<GeocodeOptions>) -> Result<Vec<Location>> {
let resp: GeocodeResponse = self
.request(GEOCODING_API_HOST, "search", Some(&[("name", name)]), opt)
.await?;
@@ -82,7 +78,7 @@ impl MeteoClient {
latitude: f64,
longitude: f64,
opt: Option<ForecastOptions>,
- ) -> Result<ForecastResponse> {
+ ) -> Result<Forecast> {
self.request(
FORECASTING_API_HOST,
"forecast",
@@ -97,7 +93,7 @@ impl MeteoClient {
mod tests {
use super::*;
- async fn get_london(client: &MeteoClient) -> GeocodeResult {
+ async fn get_london(client: &MeteoClient) -> Location {
let res = client
.geocode("London, United Kingdom", None)
.await
@@ -126,7 +122,7 @@ mod tests {
london.latitude,
london.longitude,
Some(ForecastOptions {
- hourly: Some(vec![HourlyVariable::Temperature2m]),
+ current: Some(vec![CurrentVariable::Temperature2m]),
..Default::default()
}),
)
diff --git a/crates/weather/src/model.rs b/crates/weather/src/model.rs
@@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize, Serializer};
#[serde(untagged)]
pub(crate) enum MeteoResponse<T> {
Success(T),
- Error { error: bool, reason: String },
+ Error { reason: String },
}
#[derive(Serialize, Default)]
@@ -19,7 +19,7 @@ pub struct GeocodeOptions {
}
#[derive(Deserialize, Debug, Clone)]
-pub struct GeocodeResult {
+pub struct Location {
pub id: usize,
pub name: String,
pub latitude: f64,
@@ -53,9 +53,8 @@ pub struct GeocodeResult {
}
#[derive(Deserialize, Debug, Clone)]
-pub struct GeocodeResponse {
- pub results: Vec<GeocodeResult>,
- pub generationtime_ms: f64,
+pub(crate) struct GeocodeResponse {
+ pub(crate) results: Vec<Location>,
}
#[derive(strum::Display)]
@@ -295,19 +294,34 @@ pub struct ForecastOptions {
}
#[derive(Deserialize, Debug, Clone)]
-pub struct ForecastResponse {
+pub struct ForecastData {
+ pub time: Vec<String>,
+ #[serde(flatten)]
+ pub data: HashMap<String, Vec<f64>>,
+}
+
+#[derive(Deserialize, Debug, Clone)]
+pub struct CurrentData {
+ pub time: String,
+ pub interval: usize,
+ #[serde(flatten)]
+ pub data: HashMap<String, f64>,
+}
+
+#[derive(Deserialize, Debug, Clone)]
+pub struct Forecast {
pub latitude: f64,
pub longitude: f64,
pub elevation: f64,
- #[serde(rename = "generationtime_ms")]
- pub generation_time_ms: f64,
pub utc_offset_seconds: isize,
pub timezone: String,
pub timezone_abbreviation: String,
- pub hourly: Option<HashMap<String, serde_json::Value>>,
+ pub hourly: Option<ForecastData>,
pub hourly_units: Option<HashMap<String, String>>,
- pub daily: Option<HashMap<String, serde_json::Value>>,
+ pub daily: Option<ForecastData>,
pub daily_units: Option<HashMap<String, String>>,
+ pub current: Option<CurrentData>,
+ pub current_units: Option<HashMap<String, String>>,
}
fn csv<S: Serializer, T: Display>(list: &Option<Vec<T>>, serializer: S) -> Result<S::Ok, S::Error> {