#![deny(warnings)]
#![warn(rust_2018_idioms)]
use fluxio::body::Buf;
use fluxio::Client;
use serde::Deserialize;
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
#[tokio::main]
async fn main() -> Result<()> {
let url = "http://jsonplaceholder.typicode.com/users".parse().unwrap();
let users = fetch_json(url).await?;
println!("users: {:#?}", users);
let sum = users.iter().fold(0, |acc, user| acc + user.id);
println!("sum of ids: {}", sum);
Ok(())
}
async fn fetch_json(url: fluxio::Uri) -> Result<Vec<User>> {
let client = Client::new();
let res = client.get(url).await?;
let body = fluxio::body::aggregate(res).await?;
let users = serde_json::from_reader(body.reader())?;
Ok(users)
}
#[derive(Deserialize, Debug)]
struct User {
id: i32,
#[allow(unused)]
name: String,
}