omw to nuke zjson step #2

This commit is contained in:
ajpd 2025-04-11 17:22:41 -05:00
parent 6c52977189
commit f74c1479bd
4 changed files with 37 additions and 2 deletions

View File

@ -310,7 +310,7 @@ pub const GatewayPayload = @import("structures/types.zig").GatewayPayload;
// END USING NAMESPACE
pub const Shard = @import("shard.zig");
pub const zjson = @import("json.zig");
pub const zjson = @compileError("Deprecated, use std.json instead.");
pub const Internal = @import("internal.zig");
const GatewayDispatchEvent = Internal.GatewayDispatchEvent;

View File

@ -381,7 +381,7 @@ pub fn close(self: *Self, code: ShardSocketCloseCodes, reason: []const u8) Close
.reason = reason, //[]const u8
}) catch {
// log that the connection died, but don't stop the bot
self.logif("Shard {d} closed with error: {s} # {d}", .{self.id, reason, @intFromEnum(code)});
std.debug.print("Shard {d} closed with error: {s} # {d}\n", .{self.id, reason, @intFromEnum(code)});
};
}

View File

@ -153,6 +153,28 @@ pub const SelectMenu = union(MessageComponentTypes) {
SelectMenuUsersAndRoles: SelectMenuUsersAndRoles,
SelectMenuChannels: SelectMenuChannels,
pub fn jsonParse(allocator: std.mem.Allocator, src: []const u8) !@This() {
const value = try std.json.innerParse(std.json.Value, allocator, src, .{
.max_value_len = 0x100,
});
if (!value != .object) @panic("coulnd't match against non-object type");
switch (value.object.get("type") orelse @panic("couldn't find property `type`")) {
.integer => |num| return switch (@as(MessageComponentTypes, @enumFromInt(num))) {
.SelectMenu => .{ .SelectMenu = try std.json.parseFromValue(SelectMenuString, allocator, value, .{.max_value_len = 0x100})},
.SelectMenuUsers => .{ .SelectMenuUsers = try std.json.parseFromValue(SelectMenuUsers, allocator, value, .{.max_value_len = 0x100})},
.SelectMenuRoles => .{ .SelectMenuRoles = try std.json.parseFromValue(SelectMenuRoles, allocator, value, .{.max_value_len = 0x100})},
.SelectMenuUsersAndRoles => .{ .SelectMenuUsersAndRoles = try std.json.parseFromValue(SelectMenuUsersAndRoles, allocator, value, .{.max_value_len = 0x100})},
.SelectMenuChannels => .{ .SelectMenuChannels = try std.json.parseFromValue(SelectMenuChannels, allocator, value, .{.max_value_len = 0x100})},
},
else => @panic("got type but couldn't match against non enum member `type`"),
}
return try MessageComponent.json(allocator, value);
}
// legacy
pub fn json(allocator: std.mem.Allocator, value: zjson.JsonType) !@This() {
if (!value.is(.object))
@panic("coulnd't match against non-object type");

View File

@ -54,6 +54,7 @@ pub const Snowflake = enum(u64) {
}
/// zjson parse
/// legacy
pub fn json(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
if (value.is(.string))
return Snowflake.fromRaw(value.string) catch
@ -61,6 +62,18 @@ pub const Snowflake = enum(u64) {
unreachable;
}
/// std.json parse
pub fn jsonParse(allocator: std.mem.Allocator, src: []const u8) !@This() {
const value = try std.json.parseFromSlice(std.json.Value, allocator, src, .{
.max_value_len = 0x100
});
if (value != .string)
return Snowflake.fromRaw(value.string) catch
std.debug.panic("invalid snowflake: {s}\n", .{value.string});
unreachable;
}
/// print
pub fn format(self: Snowflake, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
try writer.print("{d}", .{self.into()});