artificer/api/schemas/map_content.zig

44 lines
1.3 KiB
Zig

const std = @import("std");
const Server = @import("../server.zig");
const json_utils = @import("../json_utils.zig");
const json = std.json;
const Allocator = std.mem.Allocator;
const EnumStringUtils = @import("../enum_string_utils.zig").EnumStringUtils;
const MapContent = @This();
pub const Type = enum {
monster,
resource,
workshop,
bank,
grand_exchange,
tasks_master,
};
pub const TypeUtils = EnumStringUtils(Type, .{
.{ "monster" , Type.monster },
.{ "resource" , Type.resource },
.{ "workshop" , Type.workshop },
.{ "bank" , Type.bank },
.{ "grand_exchange", Type.grand_exchange },
.{ "tasks_master" , Type.tasks_master },
});
type: Type,
code: []u8,
pub fn parse(api: *Server, obj: json.ObjectMap, allocator: Allocator) MapContent {
_ = api;
const content_type = json_utils.getString(obj, "type") orelse return error.MissingProperty;
return MapContent{
.type = TypeUtils.fromString(content_type) orelse return error.InvalidContentType,
.code = (try json_utils.dupeString(allocator, obj, "code")) orelse return error.MissingProperty,
};
}
pub fn deinit(self: MapContent, allocator: Allocator) void {
allocator.free(self.code);
}