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