chore: switch toJson -> json
This commit is contained in:
parent
e8fbc72485
commit
798721d59d
20
src/json.zig
20
src/json.zig
@ -977,8 +977,8 @@ pub fn parseInto(comptime T: type, allocator: mem.Allocator, value: JsonType) Er
|
||||
return try parseInto(optionalInfo.child, allocator, value); // optional
|
||||
},
|
||||
.@"union" => |unionInfo| {
|
||||
if (std.meta.hasFn(T, "toJson")) {
|
||||
return try T.toJson(allocator, value);
|
||||
if (std.meta.hasFn(T, "json")) {
|
||||
return try T.json(allocator, value);
|
||||
}
|
||||
|
||||
var result: ?T = null;
|
||||
@ -1027,8 +1027,8 @@ pub fn parseInto(comptime T: type, allocator: mem.Allocator, value: JsonType) Er
|
||||
return result.?;
|
||||
},
|
||||
.@"enum" => {
|
||||
if (std.meta.hasFn(T, "toJson"))
|
||||
return try T.toJson(allocator, value);
|
||||
if (std.meta.hasFn(T, "json"))
|
||||
return try T.json(allocator, value);
|
||||
|
||||
switch (value) {
|
||||
.string => return std.meta.stringToEnum(T, value.string).?, // useful for parsing a name into enum T
|
||||
@ -1038,8 +1038,8 @@ pub fn parseInto(comptime T: type, allocator: mem.Allocator, value: JsonType) Er
|
||||
},
|
||||
.@"struct" => |structInfo| {
|
||||
var r: T = undefined;
|
||||
if (std.meta.hasFn(T, "toJson"))
|
||||
return try T.toJson(allocator, value);
|
||||
if (std.meta.hasFn(T, "json"))
|
||||
return try T.json(allocator, value);
|
||||
|
||||
if (!value.is(.object))
|
||||
@panic("tried to cast a non-object into: " ++ @typeName(T));
|
||||
@ -1128,8 +1128,8 @@ pub fn parseInto(comptime T: type, allocator: mem.Allocator, value: JsonType) Er
|
||||
else => return error.TypeMismatch, // may only cast string, array
|
||||
},
|
||||
else => {
|
||||
if (std.meta.hasFn(T, "toJson"))
|
||||
return T.toJson(allocator, value);
|
||||
if (std.meta.hasFn(T, "json"))
|
||||
return T.json(allocator, value);
|
||||
return error.TypeMismatch; // unsupported type
|
||||
},
|
||||
},
|
||||
@ -1224,7 +1224,7 @@ pub fn parseRight(comptime L: type, comptime R: type, child_allocator: mem.Alloc
|
||||
pub fn Record(comptime T: type) type {
|
||||
return struct {
|
||||
map: std.StringHashMapUnmanaged(T),
|
||||
pub fn toJson(allocator: mem.Allocator, value: JsonType) !@This() {
|
||||
pub fn json(allocator: mem.Allocator, value: JsonType) !@This() {
|
||||
var map: std.StringHashMapUnmanaged(T) = .init;
|
||||
|
||||
var iterator = value.object.iterator();
|
||||
@ -1270,7 +1270,7 @@ pub fn AssociativeArray(comptime E: type, comptime V: type) type {
|
||||
|
||||
return struct {
|
||||
map: std.EnumMap(E, V),
|
||||
pub fn toJson(allocator: mem.Allocator, value: JsonType) !@This() {
|
||||
pub fn json(allocator: mem.Allocator, value: JsonType) !@This() {
|
||||
var map: std.EnumMap(E, V) = .{};
|
||||
|
||||
var iterator = value.object.iterator();
|
||||
|
@ -153,7 +153,7 @@ pub const SelectMenu = union(MessageComponentTypes) {
|
||||
SelectMenuUsersAndRoles: SelectMenuUsersAndRoles,
|
||||
SelectMenuChannels: SelectMenuChannels,
|
||||
|
||||
pub fn toJson(allocator: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
pub fn json(allocator: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
if (!value.is(.object))
|
||||
@panic("coulnd't match against non-object type");
|
||||
|
||||
@ -211,7 +211,7 @@ pub const MessageComponent = union(MessageComponentTypes) {
|
||||
SelectMenuUsersAndRoles: SelectMenuUsersAndRoles,
|
||||
SelectMenuChannels: SelectMenuChannels,
|
||||
|
||||
pub fn toJson(allocator: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
pub fn json(allocator: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
if (!value.is(.object))
|
||||
@panic("coulnd't match against non-object type");
|
||||
|
||||
|
@ -43,7 +43,7 @@ pub const UserFlags = packed struct {
|
||||
return @bitCast(raw);
|
||||
}
|
||||
|
||||
pub fn toJson(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
pub fn json(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
return @bitCast(value.number.cast(u34));
|
||||
}
|
||||
|
||||
@ -83,7 +83,7 @@ pub const PremiumUsageFlags = packed struct {
|
||||
return @bitCast(raw);
|
||||
}
|
||||
|
||||
pub fn toJson(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
pub fn json(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
return @bitCast(value.number.cast(u8));
|
||||
}
|
||||
|
||||
@ -102,7 +102,7 @@ pub const PurchasedFlags = packed struct {
|
||||
return @bitCast(raw);
|
||||
}
|
||||
|
||||
pub fn toJson(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
pub fn json(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
return @bitCast(value.number.cast(u8));
|
||||
}
|
||||
|
||||
@ -122,7 +122,7 @@ pub const MemberFlags = packed struct {
|
||||
return @bitCast(raw);
|
||||
}
|
||||
|
||||
pub fn toJson(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
pub fn json(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
return @bitCast(value.number.cast(u16));
|
||||
}
|
||||
|
||||
@ -198,7 +198,7 @@ pub const ChannelFlags = packed struct {
|
||||
return @bitCast(raw);
|
||||
}
|
||||
|
||||
pub fn toJson(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
pub fn json(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
return @bitCast(value.number.cast(u32));
|
||||
}
|
||||
|
||||
@ -224,7 +224,7 @@ pub const RoleFlags = packed struct {
|
||||
return @bitCast(raw);
|
||||
}
|
||||
|
||||
pub fn toJson(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
pub fn json(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
return @bitCast(value.number.cast(u2));
|
||||
}
|
||||
|
||||
@ -242,7 +242,7 @@ pub const AttachmentFlags = packed struct {
|
||||
return @bitCast(raw);
|
||||
}
|
||||
|
||||
pub fn toJson(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
pub fn json(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
return @bitCast(value.number.cast(u8));
|
||||
}
|
||||
|
||||
@ -263,7 +263,7 @@ pub const SkuFlags = packed struct {
|
||||
return @bitCast(raw);
|
||||
}
|
||||
|
||||
pub fn toJson(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
pub fn json(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
return @bitCast(value.number.cast(u16));
|
||||
}
|
||||
|
||||
@ -288,7 +288,7 @@ pub const MessageFlags = packed struct {
|
||||
return @bitCast(raw);
|
||||
}
|
||||
|
||||
pub fn toJson(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
pub fn json(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
return @bitCast(value.number.cast(u16));
|
||||
}
|
||||
|
||||
@ -328,7 +328,7 @@ pub const ActivityFlags = packed struct {
|
||||
return @bitCast(raw);
|
||||
}
|
||||
|
||||
pub fn toJson(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
pub fn json(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
return @bitCast(value.number.cast(u16));
|
||||
}
|
||||
|
||||
@ -366,7 +366,7 @@ pub const ApplicationFlags = packed struct {
|
||||
return @bitCast(raw);
|
||||
}
|
||||
|
||||
pub fn toJson(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
pub fn json(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
return @bitCast(value.number.cast(u32));
|
||||
}
|
||||
|
||||
@ -577,7 +577,7 @@ pub const SystemChannelFlags = packed struct {
|
||||
return @bitCast(raw);
|
||||
}
|
||||
|
||||
pub fn toJson(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
pub fn json(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
return @bitCast(value.number.cast(u8));
|
||||
}
|
||||
|
||||
@ -622,7 +622,7 @@ pub const ChannelTypes = packed struct {
|
||||
return @bitCast(raw);
|
||||
}
|
||||
|
||||
pub fn toJson(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
pub fn json(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
return @bitCast(value.number.cast(u32));
|
||||
}
|
||||
|
||||
@ -949,7 +949,7 @@ pub const BitwisePermissionFlags = packed struct {
|
||||
return @bitCast(raw);
|
||||
}
|
||||
|
||||
pub fn toJson(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
pub fn json(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
return @bitCast(value.number.cast(u64));
|
||||
}
|
||||
|
||||
@ -1296,7 +1296,7 @@ pub const GatewayIntents = packed struct {
|
||||
return @bitCast(raw);
|
||||
}
|
||||
|
||||
pub fn toJson(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
pub fn json(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
return @bitCast(value.number.cast(u32));
|
||||
}
|
||||
///
|
||||
|
@ -54,7 +54,7 @@ pub const Snowflake = enum(u64) {
|
||||
}
|
||||
|
||||
/// zjson parse
|
||||
pub fn toJson(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
pub fn json(_: std.mem.Allocator, value: zjson.JsonType) !@This() {
|
||||
if (value.is(.string))
|
||||
return Snowflake.fromRaw(value.string) catch
|
||||
std.debug.panic("invalid snowflake: {s}\n", .{value.string});
|
||||
|
Loading…
x
Reference in New Issue
Block a user