an http based roguelike rpg about jelqing
| 1 | use axum::http::{HeaderName, StatusCode}; |
| 2 | use serde_json::Value; |
| 3 | |
| 4 | pub enum Payload { |
| 5 | Empty, |
| 6 | Json(Value), |
| 7 | Raw(&'static str, Vec<u8>), |
| 8 | } |
| 9 | |
| 10 | /// A successful result of a game action, before it becomes an HTTP response. |
| 11 | pub struct Outcome { |
| 12 | pub status: StatusCode, |
| 13 | pub payload: Payload, |
| 14 | pub headers: Vec<(HeaderName, String)>, |
| 15 | } |
| 16 | |
| 17 | impl Outcome { |
| 18 | pub fn empty(status: StatusCode) -> Self { |
| 19 | Self { |
| 20 | status, |
| 21 | payload: Payload::Empty, |
| 22 | headers: Vec::new(), |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | pub fn json(status: StatusCode, body: Value) -> Self { |
| 27 | Self { |
| 28 | payload: Payload::Json(body), |
| 29 | ..Self::empty(status) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | pub fn raw(status: StatusCode, content_type: &'static str, body: impl Into<Vec<u8>>) -> Self { |
| 34 | Self { |
| 35 | payload: Payload::Raw(content_type, body.into()), |
| 36 | ..Self::empty(status) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | pub fn header(mut self, name: HeaderName, value: impl Into<String>) -> Self { |
| 41 | self.headers.push((name, value.into())); |
| 42 | self |
| 43 | } |
| 44 | } |