an http based roguelike rpg about jelqing
Blame: src/http/parse.rs
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 1 | use axum::http::StatusCode; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 2 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 3 | use super::ctx::Ctx; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 4 | use crate::{ |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 5 | game::{Run, Technique}, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 6 | problem::Problem, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 7 | }; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 8 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 9 | pub(super) fn parse_techniques(run: &Run, ctx: &Ctx) -> Result<Vec<Technique>, Problem> { |
4f54028cba69 | atagen | 2026-10-14 11:45 UTC | 10 | let listed = |problem: Problem| problem.with("techniques", run.path("/techniques")); |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 11 | let required = || { |
4f54028cba69 | atagen | 2026-10-14 11:45 UTC | 12 | listed(Problem::new( |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 13 | StatusCode::BAD_REQUEST, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 14 | "technique-required", |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 15 | "No technique was specified.", |
4f54028cba69 | atagen | 2026-10-14 11:45 UTC | 16 | )) |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 17 | }; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 18 | let names = ctx |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 19 | .query("technique") |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 20 | .filter(|n| !n.trim().is_empty()) |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 21 | .ok_or_else(required)?; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 22 | names |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 23 | .split(',') |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 24 | .map(str::trim) |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 25 | .filter(|n| !n.is_empty()) |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 26 | .map(|name| { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 27 | Technique::parse(name).ok_or_else(|| { |
4f54028cba69 | atagen | 2026-10-14 11:45 UTC | 28 | listed(Problem::new( |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 29 | StatusCode::BAD_REQUEST, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 30 | "unknown-technique", |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 31 | format!("{name} is not a technique."), |
4f54028cba69 | atagen | 2026-10-14 11:45 UTC | 32 | )) |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 33 | }) |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 34 | }) |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 35 | .collect() |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 36 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 37 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 38 | pub(super) fn parse_intensity(ctx: &Ctx) -> Result<i32, Problem> { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 39 | match ctx.query("intensity").map(str::parse::<i32>) { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 40 | None => Ok(5), |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 41 | Some(Ok(i)) if (1..=10).contains(&i) => Ok(i), |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 42 | Some(_) => Err(Problem::new( |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 43 | StatusCode::BAD_REQUEST, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 44 | "intensity-out-of-range", |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 45 | "Intensity is measured from 1 to 10.", |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 46 | )), |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 47 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 48 | } |