atagen

jelq

public

an http based roguelike rpg about jelqing

Star0
Watch0
2176d9a94bd9f6eaed0b8222251bd637ca2fb4a7
1
ssh://brewery@pls.lobotomise.me:33/atagen/jelq.git

Blame: src/http/response.rs

782069860236atagen2026-10-10 12:44 UTC1
use axum::{
782069860236atagen2026-10-10 12:44 UTC2
    Json,
782069860236atagen2026-10-10 12:44 UTC3
    http::{HeaderValue, StatusCode, header},
782069860236atagen2026-10-10 12:44 UTC4
    response::{IntoResponse, Response},
782069860236atagen2026-10-10 12:44 UTC5
};
782069860236atagen2026-10-10 12:44 UTC6
782069860236atagen2026-10-10 12:44 UTC7
use crate::game::{Outcome, Payload};
782069860236atagen2026-10-10 12:44 UTC8
782069860236atagen2026-10-10 12:44 UTC9
impl IntoResponse for Outcome {
782069860236atagen2026-10-10 12:44 UTC10
    fn into_response(self) -> Response {
8b8e83329a1datagen2026-10-12 12:01 UTC11
        let (status, payload, extra) = self.into_parts();
8b8e83329a1datagen2026-10-12 12:01 UTC12
        let mut response = match payload {
782069860236atagen2026-10-10 12:44 UTC13
            // RFC 9110 §15.3.7 obliges a 206 to be a byte range of the full representation, so it is.
8b8e83329a1datagen2026-10-12 12:01 UTC14
            Payload::Json(body) if status == StatusCode::PARTIAL_CONTENT => {
782069860236atagen2026-10-10 12:44 UTC15
                let full = body.to_string().into_bytes();
782069860236atagen2026-10-10 12:44 UTC16
                let half = full.len() / 2;
782069860236atagen2026-10-10 12:44 UTC17
                let range = format!("bytes 0-{}/{}", half.saturating_sub(1), full.len());
782069860236atagen2026-10-10 12:44 UTC18
                (
8b8e83329a1datagen2026-10-12 12:01 UTC19
                    status,
782069860236atagen2026-10-10 12:44 UTC20
                    [
782069860236atagen2026-10-10 12:44 UTC21
                        (header::CONTENT_TYPE, "application/json".to_owned()),
782069860236atagen2026-10-10 12:44 UTC22
                        (header::CONTENT_RANGE, range),
782069860236atagen2026-10-10 12:44 UTC23
                    ],
782069860236atagen2026-10-10 12:44 UTC24
                    full[..half].to_vec(),
782069860236atagen2026-10-10 12:44 UTC25
                )
782069860236atagen2026-10-10 12:44 UTC26
                    .into_response()
782069860236atagen2026-10-10 12:44 UTC27
            }
8b8e83329a1datagen2026-10-12 12:01 UTC28
            Payload::Json(body) => (status, Json(body)).into_response(),
782069860236atagen2026-10-10 12:44 UTC29
            Payload::Raw(content_type, body) => {
8b8e83329a1datagen2026-10-12 12:01 UTC30
                (status, [(header::CONTENT_TYPE, content_type)], body).into_response()
782069860236atagen2026-10-10 12:44 UTC31
            }
8b8e83329a1datagen2026-10-12 12:01 UTC32
            Payload::Empty => status.into_response(),
782069860236atagen2026-10-10 12:44 UTC33
        };
782069860236atagen2026-10-10 12:44 UTC34
        let headers = response.headers_mut();
8b8e83329a1datagen2026-10-12 12:01 UTC35
        for (name, value) in extra {
782069860236atagen2026-10-10 12:44 UTC36
            if let Ok(value) = HeaderValue::from_str(&value) {
782069860236atagen2026-10-10 12:44 UTC37
                headers.append(name, value);
782069860236atagen2026-10-10 12:44 UTC38
            }
782069860236atagen2026-10-10 12:44 UTC39
        }
782069860236atagen2026-10-10 12:44 UTC40
        response
782069860236atagen2026-10-10 12:44 UTC41
    }
782069860236atagen2026-10-10 12:44 UTC42
}
782069860236atagen2026-10-10 12:44 UTC43
782069860236atagen2026-10-10 12:44 UTC44
pub fn respond<T: IntoResponse, E: IntoResponse>(result: Result<T, E>) -> Response {
782069860236atagen2026-10-10 12:44 UTC45
    match result {
782069860236atagen2026-10-10 12:44 UTC46
        Ok(ok) => ok.into_response(),
782069860236atagen2026-10-10 12:44 UTC47
        Err(err) => err.into_response(),
782069860236atagen2026-10-10 12:44 UTC48
    }
782069860236atagen2026-10-10 12:44 UTC49
}