atagen

jelq

public

an http based roguelike rpg about jelqing

Star0
Watch0
6619c458c23edaf053ae371f8aa0df1e866febb6
1
ssh://brewery@pls.lobotomise.me:33/atagen/jelq.git
src/game/outcome.rsRust60 lines1.5 KB
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
    status: StatusCode,
13
    payload: Payload,
14
    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
45
    pub fn status(&self) -> StatusCode {
46
        self.status
47
    }
48
49
    pub(super) fn retold(mut self, status: StatusCode, detail: &str) -> Self {
50
        self.status = status;
51
        if let Payload::Json(body) = &mut self.payload {
52
            body["detail"] = Value::from(detail);
53
        }
54
        self
55
    }
56
57
    pub fn into_parts(self) -> (StatusCode, Payload, Vec<(HeaderName, String)>) {
58
        (self.status, self.payload, self.headers)
59
    }
60
}