an http based roguelike rpg about jelqing
Blame: src/http/response.rs
782069860236 | atagen | 2026-10-10 12:44 UTC | 1 | use axum::{ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 2 | Json, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 3 | http::{HeaderValue, StatusCode, header}, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 4 | response::{IntoResponse, Response}, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 5 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 6 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 7 | use crate::game::{Outcome, Payload}; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 8 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 9 | impl IntoResponse for Outcome { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 10 | fn into_response(self) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 11 | let mut response = match self.payload { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 12 | // RFC 9110 §15.3.7 obliges a 206 to be a byte range of the full representation, so it is. |
782069860236 | atagen | 2026-10-10 12:44 UTC | 13 | Payload::Json(body) if self.status == StatusCode::PARTIAL_CONTENT => { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 14 | let full = body.to_string().into_bytes(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 15 | let half = full.len() / 2; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 16 | let range = format!("bytes 0-{}/{}", half.saturating_sub(1), full.len()); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 17 | ( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 18 | self.status, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 19 | [ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 20 | (header::CONTENT_TYPE, "application/json".to_owned()), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 21 | (header::CONTENT_RANGE, range), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 22 | ], |
782069860236 | atagen | 2026-10-10 12:44 UTC | 23 | full[..half].to_vec(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 24 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 25 | .into_response() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 26 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 27 | Payload::Json(body) => (self.status, Json(body)).into_response(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 28 | Payload::Raw(content_type, body) => { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 29 | (self.status, [(header::CONTENT_TYPE, content_type)], body).into_response() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 30 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 31 | Payload::Empty => self.status.into_response(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 32 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 33 | let headers = response.headers_mut(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 34 | for (name, value) in self.headers { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 35 | if let Ok(value) = HeaderValue::from_str(&value) { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 36 | headers.append(name, value); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 37 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 38 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 39 | response |
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 | pub fn respond<T: IntoResponse, E: IntoResponse>(result: Result<T, E>) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 44 | match result { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 45 | Ok(ok) => ok.into_response(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 46 | Err(err) => err.into_response(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 47 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 48 | } |