atagen

jelq

public

an http based roguelike rpg about jelqing

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