atagen

jelq

public

an http based roguelike rpg about jelqing

Star0
Watch0
624fe9c09c8eefdb9679b36259b76f6439318edc
1
ssh://brewery@pls.lobotomise.me:33/atagen/jelq.git
src/http/inventory.rsRust44 lines1.1 KB
1
use axum::{
2
    Json,
3
    http::StatusCode,
4
    response::{IntoResponse, Response},
5
};
6
use serde_json::json;
7
8
use super::{ctx::Ctx, response::respond};
9
use crate::{
10
    game::{Item, Run},
11
    problem::Problem,
12
};
13
14
pub(super) fn get_inventory(run: &mut Run, _: &Ctx) -> Response {
15
    Json(run.inventory_view()).into_response()
16
}
17
18
pub(super) fn owned_item(run: &Run, ctx: &Ctx) -> Result<Item, Problem> {
19
    Item::parse(ctx.param("item"))
20
        .filter(|&item| run.holds(item))
21
        .ok_or_else(|| {
22
            Problem::new(
23
                StatusCode::NOT_FOUND,
24
                "no-such-item",
25
                "You do not have that.",
26
            )
27
        })
28
}
29
30
pub(super) fn get_item(run: &mut Run, ctx: &Ctx) -> Response {
31
    match owned_item(run, ctx) {
32
        Ok(item) => {
33
            Json(json!({ "name": item.slug(), "description": item.description() })).into_response()
34
        }
35
        Err(problem) => problem.into_response(),
36
    }
37
}
38
39
pub(super) fn delete_item(run: &mut Run, ctx: &Ctx) -> Response {
40
    match owned_item(run, ctx) {
41
        Ok(item) => respond(run.drop_item(item)),
42
        Err(problem) => problem.into_response(),
43
    }
44
}