Hi guys,
I want to write an API wrapper for a coronavirus API and already finished parts of it. Most of my code is working, but I specifically have a problem with one problem, which is parsing a list of objects into json. So my question is:
How do I parse a List of Objects into a Vec<\*structname\*>?
​
My current code is:
mod apistructs;
use reqwest::Error;
use reqwest::blocking::get;
use apistructs::*;
pub struct APIGetter {coronalink: String}
impl APIGetter {
pub fn new() -> Self {
Self {
coronalink: "https://corona.lmao.ninja".to_owned()
}
}
pub fn get_all(&self) -> Result<All, Error> {
Ok(get(&format!("{}/all", &self.coronalink))?.json()?)
}
pub fn get_countries(&self) -> Result<Vec<Country>, Error> {
Ok(get(&format!("{}/countries", &self.coronalink))?.json()?)
}
pub fn get_states(&self) -> Result<Vec<State>, Error> {
Ok(get(&format!("{}/states", &self.coronalink))?.json()?)
}
pub fn get_jhucsse(&self) -> Result<Vec<JhucsseEntry>, Error> {
Ok(get(&format!("{}/jhucsse", &self.coronalink))?.json()?)}
}
use serde::{Deserialize};
#[derive(Deserialize, Debug)]
pub struct All {
pub cases: u64,
pub deaths: u64,
pub recovered: u64,
pub updated: u128,
}
#[derive(Deserialize, Debug)]
pub struct Country {
pub country: String,
pub countryInfo: CountryInfo,
pub cases: u64,
pub todayCases: u64,
pub deaths: u64,
pub todayDeaths: u64,
pub recovered: u64,
pub active: u64,
pub critical: u64,
pub casesPerOneMillion: u64
}
#[derive(Deserialize, Debug)]
pub struct CountryInfo {
pub iso2: String,
pub iso3: String,
pub _id: u64,
pub lat: f64,
pub long: f64,
pub flag: String,
}
#[derive(Deserialize, Debug)]
pub struct State {
pub state: String,
pub cases: u64,
pub todayCases: u64,
pub deaths: u64,
pub todayDeaths: u64,
pub recovered: u64,
pub active: u64
}
#[derive(Deserialize, Debug)]
pub struct JhucsseEntry {
pub country: String,
pub province: Option<String>,
pub updatedAt: String,
pub stats: Stats,
pub coordinates: Coordinates,
}
#[derive(Deserialize, Debug)]
pub struct Stats {
pub confirmed: u64,
pub deaths: u64,
pub recovered: u64,
}
#[derive(Deserialize, Debug)]
pub struct Coordinates {
pub latitude: f64,
pub longitude: f64,
}