From f0381a7fb305a371208df62d339540ce3d48f9da Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 14 Dec 2019 00:32:05 +0100 Subject: Use `surf` in `pokedex` example --- Cargo.toml | 2 +- examples/pokedex.rs | 23 +++++++++-------------- examples/timer.rs | 2 +- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index decc7d52..ac4d3bed 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,8 +42,8 @@ serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" directories = "2.0" futures = "0.3" -reqwest = "0.9" async-std = { version = "1.3", features = ["unstable"] } +surf = { version = "1.0", git = "https://github.com/http-rs/surf.git", rev = "2ff0f95513e82bdb5ccc56767f9dd0985f2eb8fe" } rand = "0.7" [target.'cfg(target_arch = "wasm32")'.dev-dependencies] diff --git a/examples/pokedex.rs b/examples/pokedex.rs index b9daeabd..2d595ec4 100644 --- a/examples/pokedex.rs +++ b/examples/pokedex.rs @@ -150,7 +150,6 @@ impl Pokemon { async fn search() -> Result { use rand::Rng; use serde::Deserialize; - use std::io::Read; #[derive(Debug, Deserialize)] struct Entry { @@ -179,7 +178,11 @@ impl Pokemon { let url = format!("https://pokeapi.co/api/v2/pokemon-species/{}", id); let sprite = format!("https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/{}.png", id); - let entry: Entry = reqwest::get(&url)?.json()?; + let (entry, sprite): (Entry, _) = futures::future::try_join( + surf::get(&url).recv_json(), + surf::get(&sprite).recv_bytes(), + ) + .await?; let description = entry .flavor_text_entries @@ -188,13 +191,6 @@ impl Pokemon { .next() .ok_or(Error::LanguageError)?; - let mut sprite = reqwest::get(&sprite)?; - let mut bytes = Vec::new(); - - sprite - .read_to_end(&mut bytes) - .map_err(|_| Error::ImageError)?; - Ok(Pokemon { number: id, name: entry.name.to_uppercase(), @@ -203,7 +199,7 @@ impl Pokemon { .chars() .map(|c| if c.is_control() { ' ' } else { c }) .collect(), - image: image::Handle::from_memory(bytes), + image: image::Handle::from_memory(sprite), }) } } @@ -211,13 +207,12 @@ impl Pokemon { #[derive(Debug, Clone)] enum Error { APIError, - ImageError, LanguageError, } -impl From for Error { - fn from(error: reqwest::Error) -> Error { - dbg!(&error); +impl From for Error { + fn from(exception: surf::Exception) -> Error { + dbg!(&exception); Error::APIError } diff --git a/examples/timer.rs b/examples/timer.rs index b64b3ef5..367c5b2b 100644 --- a/examples/timer.rs +++ b/examples/timer.rs @@ -92,7 +92,7 @@ impl Application for Timer { let duration = Text::new(format!( "{:0>2}:{:0>2}:{:0>2}.{:0>2}", seconds / HOUR, - seconds / MINUTE, + (seconds % HOUR) / MINUTE, seconds % MINUTE, self.duration.subsec_millis() / 10, )) -- cgit