an http based roguelike rpg about jelqing
Blame: src/http/runs.rs
782069860236 | atagen | 2026-10-10 12:44 UTC | 1 | //! The resources under `/runs/{id}`. |
782069860236 | atagen | 2026-10-10 12:44 UTC | 2 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 3 | use std::{convert::Infallible, fmt::Write as _}; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 4 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 5 | use axum::{ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 6 | Json, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 7 | body::{Body, Bytes}, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 8 | http::{StatusCode, header}, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 9 | response::{IntoResponse, Response}, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 10 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 11 | use serde_json::{Value, json}; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 12 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 13 | use super::{ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 14 | ctx::Ctx, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 15 | resource::{Resource, Verb}, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 16 | response::respond, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 17 | run::{Greeting, RunResource, Scope}, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 18 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 19 | use crate::{ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 20 | game::{ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 21 | Cause, Floor, Grip, Item, LUBRICANTS, Lube, PatchOp, Run, Technique, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 22 | balance::{EQ_BAND, MM_PER_INCH}, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 23 | shaft, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 24 | }, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 25 | headers::ACCEPT_PATCH, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 26 | problem::Problem, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 27 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 28 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 29 | const fn home(verbs: &'static [Verb<Run>]) -> RunResource { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 30 | RunResource { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 31 | scope: Scope::Home, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 32 | resource: Resource::new(verbs), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 33 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 34 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 35 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 36 | const fn pockets(verbs: &'static [Verb<Run>]) -> RunResource { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 37 | RunResource { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 38 | scope: Scope::Pockets, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 39 | resource: Resource::new(verbs), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 40 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 41 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 42 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 43 | fn not_in_clinic(run: &Run) -> bool { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 44 | run.floor != Floor::Clinic |
782069860236 | atagen | 2026-10-10 12:44 UTC | 45 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 46 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 47 | fn never(_: &Run) -> bool { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 48 | false |
782069860236 | atagen | 2026-10-10 12:44 UTC | 49 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 50 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 51 | fn unreachable_handler(_: &mut Run, _: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 52 | StatusCode::METHOD_NOT_ALLOWED.into_response() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 53 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 54 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 55 | pub static RUN: RunResource = RunResource { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 56 | scope: Scope::Home, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 57 | resource: Resource { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 58 | verbs: &[ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 59 | Verb::new("GET", get_run), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 60 | Verb::new("JELQ", jelq) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 61 | .only_when(not_in_clinic, "JELQ is not performed in the clinic."), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 62 | Verb::new("PUT", put_run), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 63 | Verb::new("PATCH", patch_run), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 64 | Verb::new("DELETE", delete_run), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 65 | Verb::new("TRACE", trace_run), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 66 | ], |
782069860236 | atagen | 2026-10-10 12:44 UTC | 67 | accept_patch: true, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 68 | }, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 69 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 70 | pub static LENGTH: RunResource = home(&[Verb::new("GET", get_length)]); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 71 | pub static FORESKIN: RunResource = home(&[ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 72 | Verb::new("GET", get_foreskin), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 73 | Verb::new("DELETE", delete_foreskin), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 74 | ]); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 75 | pub static INVENTORY: RunResource = pockets(&[Verb::new("GET", get_inventory)]); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 76 | pub static ITEM: RunResource = |
782069860236 | atagen | 2026-10-10 12:44 UTC | 77 | pockets(&[Verb::new("GET", get_item), Verb::new("DELETE", delete_item)]); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 78 | pub static TECHNIQUES: RunResource = home(&[Verb::new("GET", get_techniques)]); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 79 | pub static DRAWER: RunResource = home(&[Verb::new("POST", search_drawer)]); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 80 | pub static BRANCH: RunResource = home(&[Verb::new("POST", choose_branch)]); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 81 | pub static FLOOR: RunResource = |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 82 | home(&[Verb::new("GET", get_floor), Verb::new("POST", leave_floor)]); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 83 | pub static SHOP: RunResource = home(&[Verb::new("GET", get_shop)]); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 84 | pub static BUY: RunResource = home(&[Verb::new("POST", buy)]); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 85 | pub static PORTAL: RunResource = pockets(&[ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 86 | Verb::new("GET", get_portal), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 87 | Verb::new("POST", accept_portal), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 88 | ]); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 89 | pub static ELDER: RunResource = home(&[Verb::new("GET", get_elder)]); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 90 | pub static GRIP: RunResource = home(&[Verb::new("GET", get_grip)]); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 91 | pub static DIAGNOSIS: RunResource = pockets(&[Verb::new("GET", get_diagnosis)]); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 92 | pub static FOOTAGE: RunResource = pockets(&[ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 93 | Verb::new("GET", get_footage), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 94 | Verb::new("DELETE", unreachable_handler).only_when( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 95 | never, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 96 | "The footage has been cached by intermediaries. It cannot be deleted.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 97 | ), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 98 | ]); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 99 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 100 | fn get_run(run: &mut Run, ctx: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 101 | if ctx |
782069860236 | atagen | 2026-10-10 12:44 UTC | 102 | .header(header::ACCEPT) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 103 | .is_some_and(|accept| accept.contains("reassurance")) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 104 | { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 105 | return Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 106 | StatusCode::NOT_ACCEPTABLE, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 107 | "no-reassurance", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 108 | "The server does not produce reassurance.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 109 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 110 | .into_response(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 111 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 112 | ( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 113 | StatusCode::OK, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 114 | [(header::ETAG, run.etag())], |
782069860236 | atagen | 2026-10-10 12:44 UTC | 115 | Json(run.view()), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 116 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 117 | .into_response() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 118 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 119 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 120 | fn parse_techniques(run: &Run, ctx: &Ctx) -> Result<Vec<Technique>, Problem> { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 121 | let required = || { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 122 | Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 123 | StatusCode::BAD_REQUEST, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 124 | "technique-required", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 125 | "No technique was specified.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 126 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 127 | .with("techniques", run.path("/techniques")) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 128 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 129 | let names = ctx |
782069860236 | atagen | 2026-10-10 12:44 UTC | 130 | .query("technique") |
782069860236 | atagen | 2026-10-10 12:44 UTC | 131 | .filter(|n| !n.trim().is_empty()) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 132 | .ok_or_else(required)?; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 133 | names |
782069860236 | atagen | 2026-10-10 12:44 UTC | 134 | .split(',') |
782069860236 | atagen | 2026-10-10 12:44 UTC | 135 | .map(str::trim) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 136 | .filter(|n| !n.is_empty()) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 137 | .map(|name| { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 138 | Technique::parse(name).ok_or_else(|| { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 139 | Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 140 | StatusCode::BAD_REQUEST, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 141 | "unknown-technique", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 142 | format!("{name} is not a technique."), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 143 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 144 | .with("techniques", run.path("/techniques")) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 145 | }) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 146 | }) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 147 | .collect() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 148 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 149 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 150 | fn parse_intensity(ctx: &Ctx) -> Result<i32, Problem> { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 151 | match ctx.query("intensity").map(str::parse::<i32>) { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 152 | None => Ok(5), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 153 | Some(Ok(i)) if (1..=10).contains(&i) => Ok(i), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 154 | Some(_) => Err(Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 155 | StatusCode::BAD_REQUEST, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 156 | "intensity-out-of-range", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 157 | "Intensity is measured from 1 to 10.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 158 | )), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 159 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 160 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 161 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 162 | fn jelq(run: &mut Run, ctx: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 163 | let parsed = parse_techniques(run, ctx).and_then(|techniques| { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 164 | let intensity = parse_intensity(ctx)?; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 165 | let lube = Lube::from_content_type(ctx.header(header::CONTENT_TYPE))?; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 166 | Ok((techniques, intensity, lube)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 167 | }); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 168 | match parsed { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 169 | Ok((techniques, intensity, lube)) => { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 170 | respond(run.jelq(&techniques, intensity, lube, ctx.now)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 171 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 172 | Err(problem) => problem.into_response(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 173 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 174 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 175 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 176 | fn put_run(run: &mut Run, _: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 177 | run.put().into_response() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 178 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 179 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 180 | fn patch_run(run: &mut Run, ctx: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 181 | let is_patch = ctx |
782069860236 | atagen | 2026-10-10 12:44 UTC | 182 | .header(header::CONTENT_TYPE) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 183 | .is_some_and(|ct| ct.starts_with("application/json-patch+json")); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 184 | if !is_patch { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 185 | return Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 186 | StatusCode::UNSUPPORTED_MEDIA_TYPE, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 187 | "patch-format", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 188 | "Patches are accepted as application/json-patch+json.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 189 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 190 | .header(ACCEPT_PATCH, "application/json-patch+json") |
782069860236 | atagen | 2026-10-10 12:44 UTC | 191 | .into_response(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 192 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 193 | match serde_json::from_slice::<Vec<PatchOp>>(&ctx.body) { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 194 | Ok(ops) => respond(run.patch(&ops)), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 195 | Err(_) => Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 196 | StatusCode::BAD_REQUEST, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 197 | "malformed-patch", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 198 | "The patch could not be parsed.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 199 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 200 | .into_response(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 201 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 202 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 203 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 204 | fn delete_run(run: &mut Run, _: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 205 | run.end(Cause::Abandoned); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 206 | StatusCode::NO_CONTENT.into_response() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 207 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 208 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 209 | fn trace_run(run: &mut Run, ctx: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 210 | run.shame += 1; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 211 | let mut message = format!("{} {} {:?}\r\n", ctx.method, ctx.uri, ctx.version); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 212 | for (name, value) in &ctx.headers { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 213 | let _ = write!( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 214 | message, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 215 | "{name}: {}\r\n", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 216 | value.to_str().unwrap_or("<binary>") |
782069860236 | atagen | 2026-10-10 12:44 UTC | 217 | ); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 218 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 219 | message.push_str("\r\n"); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 220 | ( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 221 | StatusCode::OK, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 222 | [(header::CONTENT_TYPE, "message/http")], |
782069860236 | atagen | 2026-10-10 12:44 UTC | 223 | message, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 224 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 225 | .into_response() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 226 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 227 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 228 | fn millimetres(inches: f64) -> usize { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 229 | (inches * MM_PER_INCH).round() as usize |
782069860236 | atagen | 2026-10-10 12:44 UTC | 230 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 231 | |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 232 | enum InchRange { |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 233 | Satisfiable(f64, f64), |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 234 | Unsatisfiable, |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 235 | } |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 236 | |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 237 | /// `None` means the header is not a valid range, which RFC 9110 §14.2 says to ignore rather than refuse. |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 238 | fn parse_inch_range(range: &str, total: f64) -> Option<InchRange> { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 239 | let (start, end) = range.strip_prefix("inches=")?.split_once('-')?; |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 240 | let number = |text: &str| { |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 241 | text.parse::<f64>() |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 242 | .ok() |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 243 | .filter(|n| n.is_finite() && *n >= 0.0) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 244 | }; |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 245 | let (start, end) = match (start.trim(), end.trim()) { |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 246 | ("", "") => return None, |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 247 | ("", suffix) => ((total - number(suffix)?).max(0.0), total), |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 248 | (start, "") => (number(start)?, total), |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 249 | (start, end) => { |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 250 | let (start, end) = (number(start)?, number(end)?); |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 251 | if start > end { |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 252 | return None; |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 253 | } |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 254 | (start, end) |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 255 | } |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 256 | }; |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 257 | Some(if start < total { |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 258 | InchRange::Satisfiable(start, end.min(total)) |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 259 | } else { |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 260 | InchRange::Unsatisfiable |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 261 | }) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 262 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 263 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 264 | fn get_length(run: &mut Run, ctx: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 265 | let inches = if run.items.ruler { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 266 | run.current_length() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 267 | } else { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 268 | run.self_reported() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 269 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 270 | let body = shaft(millimetres(inches)); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 271 | let content_type = ( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 272 | header::CONTENT_TYPE, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 273 | "text/plain; charset=us-ascii".to_owned(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 274 | ); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 275 | let accept_ranges = (header::ACCEPT_RANGES, "inches".to_owned()); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 276 | |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 277 | if let Some(range) = ctx |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 278 | .header(header::RANGE) |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 279 | .and_then(|range| parse_inch_range(range, inches)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 280 | { |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 281 | let InchRange::Satisfiable(start, end) = range else { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 282 | return Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 283 | StatusCode::RANGE_NOT_SATISFIABLE, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 284 | "range-not-satisfiable", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 285 | format!("The requested range is not available. {inches:.2} inches are."), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 286 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 287 | .header(header::CONTENT_RANGE, format!("inches */{inches:.2}")) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 288 | .into_response(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 289 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 290 | let from = millimetres(start).min(body.len() - 1); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 291 | let to = millimetres(end).clamp(from + 1, body.len()); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 292 | let content_range = ( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 293 | header::CONTENT_RANGE, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 294 | format!("inches {start:.2}-{end:.2}/{inches:.2}"), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 295 | ); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 296 | return ( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 297 | StatusCode::PARTIAL_CONTENT, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 298 | [content_type, accept_ranges, content_range], |
782069860236 | atagen | 2026-10-10 12:44 UTC | 299 | body[from..to].to_owned(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 300 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 301 | .into_response(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 302 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 303 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 304 | if run.items.ruler { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 305 | return (StatusCode::OK, [content_type, accept_ranges], body).into_response(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 306 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 307 | let chunks: Vec<Result<Bytes, Infallible>> = body |
782069860236 | atagen | 2026-10-10 12:44 UTC | 308 | .into_bytes() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 309 | .chunks(16) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 310 | .map(|c| Ok(Bytes::copy_from_slice(c))) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 311 | .collect(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 312 | let body = Body::from_stream(futures_util::stream::iter(chunks)); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 313 | (StatusCode::OK, [content_type, accept_ranges], body).into_response() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 314 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 315 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 316 | fn get_foreskin(run: &mut Run, _: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 317 | if run.foreskin.is_none() { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 318 | return Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 319 | StatusCode::GONE, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 320 | "foreskin-gone", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 321 | "The foreskin is gone. It will not be returning.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 322 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 323 | .into_response(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 324 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 325 | (StatusCode::NO_CONTENT, [(header::ETAG, run.etag())]).into_response() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 326 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 327 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 328 | fn delete_foreskin(run: &mut Run, ctx: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 329 | respond(run.delete_foreskin(ctx.header(header::IF_MATCH))) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 330 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 331 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 332 | fn get_inventory(run: &mut Run, _: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 333 | let items: Vec<Value> = run |
782069860236 | atagen | 2026-10-10 12:44 UTC | 334 | .items |
782069860236 | atagen | 2026-10-10 12:44 UTC | 335 | .owned() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 336 | .into_iter() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 337 | .map(|item| { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 338 | json!({ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 339 | "name": item.slug(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 340 | "href": run.path(&format!("/inventory/{}", item.slug())), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 341 | }) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 342 | }) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 343 | .collect(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 344 | Json(json!({ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 345 | "items": items, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 346 | "ice_packs": run.items.ice_packs, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 347 | "supplements": run.items.supplements, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 348 | "excuses": run.excuses(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 349 | })) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 350 | .into_response() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 351 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 352 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 353 | fn owned_item(run: &Run, ctx: &Ctx) -> Result<Item, Problem> { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 354 | Item::parse(ctx.param("item")) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 355 | .filter(|&item| run.items.has(item)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 356 | .ok_or_else(|| { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 357 | Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 358 | StatusCode::NOT_FOUND, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 359 | "no-such-item", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 360 | "You do not have that.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 361 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 362 | }) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 363 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 364 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 365 | fn get_item(run: &mut Run, ctx: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 366 | match owned_item(run, ctx) { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 367 | Ok(item) => { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 368 | Json(json!({ "name": item.slug(), "description": item.description() })).into_response() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 369 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 370 | Err(problem) => problem.into_response(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 371 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 372 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 373 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 374 | fn delete_item(run: &mut Run, ctx: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 375 | match owned_item(run, ctx) { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 376 | Ok(item) => respond(run.drop_item(item)), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 377 | Err(problem) => problem.into_response(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 378 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 379 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 380 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 381 | fn get_techniques(run: &mut Run, _: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 382 | let listed: Vec<Value> = Technique::ALL |
782069860236 | atagen | 2026-10-10 12:44 UTC | 383 | .into_iter() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 384 | .map(|t| t.describe(run.forum_read())) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 385 | .collect(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 386 | Json(json!({ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 387 | "usage": format!("JELQ {}?technique=<name>[,<name>]&intensity=<1-10>", run.path("")), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 388 | "lubricant": "Declared with Content-Type.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 389 | "lubricants": LUBRICANTS, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 390 | "eq_band": [EQ_BAND.start(), EQ_BAND.end()], |
782069860236 | atagen | 2026-10-10 12:44 UTC | 391 | "techniques": listed, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 392 | })) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 393 | .into_response() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 394 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 395 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 396 | fn search_drawer(run: &mut Run, _: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 397 | respond(run.search_drawer()) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 398 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 399 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 400 | fn choose_branch(run: &mut Run, ctx: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 401 | respond(run.choose(ctx.param("choice"))) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 402 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 403 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 404 | fn get_floor(run: &mut Run, _: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 405 | Json(run.floor_view()).into_response() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 406 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 407 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 408 | fn leave_floor(run: &mut Run, _: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 409 | respond(run.advance()) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 410 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 411 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 412 | fn get_shop(run: &mut Run, _: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 413 | respond(run.shop_view().map(Json)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 414 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 415 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 416 | fn buy(run: &mut Run, ctx: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 417 | respond(run.buy(ctx.param("item"))) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 418 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 419 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 420 | fn get_portal(run: &mut Run, _: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 421 | Json(json!({ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 422 | "terms": "By connecting you agree that the hotel is not responsible for anything that happens in this room, and that it does not want to know.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 423 | "accept": { "href": run.path("/portal"), "method": "POST" }, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 424 | })) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 425 | .into_response() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 426 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 427 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 428 | fn accept_portal(run: &mut Run, _: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 429 | respond(run.accept_terms()) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 430 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 431 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 432 | fn get_elder(run: &mut Run, ctx: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 433 | respond(run.elder( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 434 | ctx.header(header::PROXY_AUTHORIZATION), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 435 | ctx.header(header::IF_MODIFIED_SINCE), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 436 | )) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 437 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 438 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 439 | fn requested_grip(ctx: &Ctx) -> Option<Grip> { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 440 | let connection = ctx.header(header::CONNECTION)?.to_ascii_lowercase(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 441 | if !connection.split(',').any(|token| token.trim() == "upgrade") { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 442 | return None; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 443 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 444 | ctx.header(header::UPGRADE)? |
782069860236 | atagen | 2026-10-10 12:44 UTC | 445 | .split(',') |
782069860236 | atagen | 2026-10-10 12:44 UTC | 446 | .find_map(|protocol| Grip::parse(protocol.trim())) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 447 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 448 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 449 | fn get_grip(run: &mut Run, ctx: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 450 | if let Some(grip) = requested_grip(ctx) { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 451 | run.set_grip(grip); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 452 | let mut response = ( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 453 | StatusCode::SWITCHING_PROTOCOLS, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 454 | [ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 455 | (header::UPGRADE, grip.name()), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 456 | (header::CONNECTION, "upgrade"), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 457 | ], |
782069860236 | atagen | 2026-10-10 12:44 UTC | 458 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 459 | .into_response(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 460 | response.extensions_mut().insert(Greeting(format!( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 461 | "You are now jelqing {}.\r\n", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 462 | grip.name() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 463 | ))); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 464 | return response; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 465 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 466 | let detail = match ctx.header(header::UPGRADE) { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 467 | Some(protocol) => { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 468 | format!("The server does not know the {protocol} grip. Grips are protocols.") |
782069860236 | atagen | 2026-10-10 12:44 UTC | 469 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 470 | None => "Grips are protocols. Change grip by upgrading the connection.".to_owned(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 471 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 472 | ( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 473 | StatusCode::OK, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 474 | [(header::UPGRADE, "overhand, underhand")], |
782069860236 | atagen | 2026-10-10 12:44 UTC | 475 | Json(json!({ "grip": run.grip.name(), "detail": detail })), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 476 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 477 | .into_response() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 478 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 479 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 480 | fn get_diagnosis(run: &mut Run, ctx: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 481 | respond(run.diagnose( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 482 | ctx.header(header::AUTHORIZATION), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 483 | ctx.header(header::IF_NONE_MATCH), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 484 | )) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 485 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 486 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 487 | fn get_footage(run: &mut Run, _: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 488 | let Some((filmed, age)) = run.footage() else { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 489 | return Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 490 | StatusCode::NOT_FOUND, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 491 | "no-footage", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 492 | "There is no footage. Yet.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 493 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 494 | .into_response(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 495 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 496 | ( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 497 | [ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 498 | ( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 499 | header::CACHE_CONTROL, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 500 | "public, max-age=31536000, immutable".to_owned(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 501 | ), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 502 | (header::AGE, age.to_string()), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 503 | (header::LAST_MODIFIED, httpdate::fmt_http_date(filmed)), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 504 | ], |
782069860236 | atagen | 2026-10-10 12:44 UTC | 505 | Json(json!({ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 506 | "detail": "A recording. It is grainy. It is unmistakably you. It has been shared.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 507 | "views": 1 + age * 37, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 508 | })), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 509 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 510 | .into_response() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 511 | } |