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