-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08.rs
44 lines (38 loc) · 1.19 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
36
37
38
39
40
41
42
43
44
use axum::{extract::Path, http::StatusCode, routing, Router};
use serde::Deserialize;
use sqlx::PgPool;
#[derive(Deserialize)]
struct Pokemon {
weight: f64,
}
async fn get_weight(id: u64) -> Result<f64, (StatusCode, String)> {
let pokemon: Pokemon = reqwest::get(format!("https://pokeapi.co/api/v2/pokemon/{id}/"))
.await
.map_err(|_| {
(
StatusCode::BAD_REQUEST,
format!("could not find pokemon {id}"),
)
})?
.json::<Pokemon>()
.await
.map_err(|_| {
(
StatusCode::BAD_REQUEST,
format!("could not find pokemon {id}"),
)
})?;
Ok(pokemon.weight / 10.0)
}
async fn weight(Path(id): Path<u64>) -> Result<String, (StatusCode, String)> {
Ok(get_weight(id).await?.to_string())
}
const DROP: f64 = 2.0 * 9.825 * 10.0;
async fn drop_momentum(Path(id): Path<u64>) -> Result<String, (StatusCode, String)> {
Ok((get_weight(id).await? * DROP.sqrt()).to_string())
}
pub fn get_routes() -> Router<PgPool> {
Router::new()
.route("/8/weight/:id", routing::get(weight))
.route("/8/drop/:id", routing::get(drop_momentum))
}