artificer/api/schemas/task.zig

46 lines
1.2 KiB
Zig

// zig fmt: off
const std = @import("std");
const Store = @import("../store.zig");
const json_utils = @import("../json_utils.zig");
const EnumStringUtils = @import("../enum_string_utils.zig").EnumStringUtils;
const Task = @This();
pub const Type = enum {
monsters,
resources,
crafts,
const Utils = EnumStringUtils(Type, .{
.{ "monsters" , Type.monsters },
.{ "resources", Type.resources },
.{ "crafts" , Type.crafts },
});
pub const fromString = Utils.fromString;
pub const toString = Utils.toString;
};
pub const Code = std.BoundedArray(u8, 32);
code: Code,
type: Type,
total: u64,
pub fn parse(store: *Store, obj: std.json.ObjectMap) !Task {
_ = store;
const code = try json_utils.getStringRequired(obj, "code");
const task_type = try json_utils.getStringRequired(obj, "type");
const total = try json_utils.getPositiveIntegerRequired(obj, "total");
return Task{
.code = try Code.fromSlice(code),
.type = Type.fromString(task_type) orelse return error.InvalidTaskType,
.total = total
};
}
pub fn parseAndAppend(store: *Store, obj: std.json.ObjectMap) !Store.Id {
return store.tasks.appendOrUpdate(try parse(store, obj));
}