-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
day08.rs
35 lines (30 loc) · 1.04 KB
/
day08.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use axum::{extract::Path, http::StatusCode, response::IntoResponse, routing::get, Router};
pub(crate) fn router() -> Router {
Router::new()
.route("/8/weight/:pokedex_number", get(weight))
.route("/8/drop/:pokedex_number", get(drop))
}
#[derive(serde::Deserialize)]
struct PokeWeight {
weight: f32,
}
async fn weight(
Path(pokedex_number): Path<i32>,
) -> Result<impl IntoResponse, (StatusCode, String)> {
pokemon_weight(pokedex_number).await.map(|f| f.to_string())
}
async fn drop(Path(pokedex_number): Path<i32>) -> Result<impl IntoResponse, (StatusCode, String)> {
Ok((14.017_845f32 * pokemon_weight(pokedex_number).await?).to_string())
}
async fn pokemon_weight(pokedex_number: i32) -> Result<f32, (StatusCode, String)> {
Ok(reqwest::get(format!(
"https://pokeapi.co/api/v2/pokemon/{pokedex_number}"
))
.await
.map_err(|e| (StatusCode::BAD_REQUEST, format!("{e:?}")))?
.json::<PokeWeight>()
.await
.map_err(|e| (StatusCode::BAD_REQUEST, format!("{e:?}")))?
.weight
/ 10f32)
}