-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday07.rs
70 lines (60 loc) · 1.91 KB
/
day07.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use std::collections::HashMap;
use axum::{http::StatusCode, routing, Json, Router};
use axum_extra::extract::cookie::CookieJar;
use base64::{engine::general_purpose, Engine};
use serde::{Deserialize, Serialize};
use serde_json;
use sqlx::PgPool;
fn decode(encoded: &str) -> Result<Vec<u8>, (StatusCode, String)> {
general_purpose::STANDARD.decode(encoded).map_err(|e| {
(
StatusCode::BAD_REQUEST,
format!("recipe cookie improperly encoded: {e}"),
)
})
}
fn get_recipe(jar: CookieJar) -> Result<Vec<u8>, (StatusCode, String)> {
decode(
jar.get("recipe")
.ok_or((StatusCode::BAD_REQUEST, "recipe cookie missing".to_string()))?
.value(),
)
}
async fn decode_endpoint(jar: CookieJar) -> Result<Vec<u8>, (StatusCode, String)> {
get_recipe(jar)
}
#[derive(Deserialize)]
struct BakeRequest {
recipe: HashMap<String, i64>,
pantry: HashMap<String, i64>,
}
#[derive(Serialize)]
struct BakeResponse {
cookies: i64,
pantry: HashMap<String, i64>,
}
async fn bake(jar: CookieJar) -> Result<Json<BakeResponse>, (StatusCode, String)> {
let BakeRequest { pantry, recipe } = serde_json::from_slice(&get_recipe(jar)?)
.map_err(|e| (StatusCode::BAD_REQUEST, format!("json encoding error: {e}")))?;
let cookies = recipe
.iter()
.filter(|(_, v)| **v != 0)
.map(|(k, v)| pantry.get(k).unwrap_or(&0) / v)
.min()
.ok_or((
StatusCode::BAD_REQUEST,
"recipe must contain some ingredients".to_string(),
))?;
Ok(Json(BakeResponse {
cookies,
pantry: pantry
.into_iter()
.map(|(k, v)| (k.clone(), v - cookies * recipe.get(&k).unwrap_or(&0)))
.collect(),
}))
}
pub fn get_routes() -> Router<PgPool> {
Router::new()
.route("/7/decode", routing::get(decode_endpoint))
.route("/7/bake", routing::get(bake))
}