I'm learning Rust by writing a simple CLI that calls a weather API based on some user input (city/state). The problem that I'm experiencing is that the API returns different key/value pairs in the JSON object depending on what's available for the location requested. For example, I've already noticed that for some locations there is a wind gust speed that's returned in addition to a normal wind speed. My initial thought was that this would be a good use case for an Enum and I would have one variant with a gust property and another without. However, I don't really have any way to know all of the different variants that are possible - maybe for some locations I can't get humidity, maybe for others I can't get pressure, etc.
My structs look like this:
#[derive(Serialize, Deserialize, Debug)]
pub struct Wind {
pub speed: f32,
pub deg: i32,
pub gust: f32
}
#[derive(Serialize, Deserialize, Debug)]
pub struct WeatherResponse {
pub coord: Coords,
pub weather: Vec<Weather>,
pub base: String,
pub main: Main,
pub visibility: i32,
pub wind: Wind,
pub clouds: Clouds,
pub dt: i32,
pub sys: Sys,
pub timezone: i32,
pub id: i32,
pub name: String,
pub cod: i32
}
I'm using the json feature from reqwest to deserialize the response directly into the WeatherResponse struct like this:
let url = reqwest::Url::parse_with_params(url, ¶ms)?;
let res: WeatherResponse = blocking::get(url)?.json()?;
This works fine as long as the response object from the API comes back with the gust key:
"wind": {
"speed": 16.11,
"deg": 350,
"gust": 23.02
},
but sometimes it comes back without it:
"wind": {
"speed": 9.22,
"deg": 360
},