24 lines
635 B
Zig
24 lines
635 B
Zig
const std = @import("std");
|
|
const json_utils = @import("json_utils.zig");
|
|
const Server = @import("./server.zig");
|
|
const ItemId = Server.ItemId;
|
|
const json = std.json;
|
|
|
|
const ItemSlot = @This();
|
|
|
|
id: ItemId,
|
|
quantity: u64,
|
|
|
|
pub fn parse(api: *Server, slot_obj: json.ObjectMap) !?ItemSlot {
|
|
const code = try json_utils.getStringRequired(slot_obj, "code");
|
|
if (code.len == 0) return null;
|
|
|
|
const quantity = try json_utils.getIntegerRequired(slot_obj, "quantity");
|
|
if (quantity < 0) return error.InvalidQuantity;
|
|
|
|
return ItemSlot{
|
|
.id = try api.getItemId(code),
|
|
.quantity = @intCast(quantity),
|
|
};
|
|
}
|