Uploading partial rewrite

This commit is contained in:
Concelare 2024-12-04 19:19:55 +00:00
parent ef20c3bd7d
commit 442b182ad6
4 changed files with 20 additions and 52 deletions

View File

@ -32,6 +32,11 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});
const json_parse = b.dependency("zjson", .{
.target = target,
.optimize = optimize,
});
const marin = b.addExecutable(.{
.name = "marin",
.root_source_file = b.path("src/test.zig"),
@ -46,12 +51,14 @@ pub fn build(b: *std.Build) void {
dzig.addImport("zlib", zlib.module("zlib"));
dzig.addImport("zmpl", zmpl.module("zmpl"));
dzig.addImport("deque", deque.module("zig-deque"));
dzig.addImport("json", json_parse.module("zjson"));
marin.root_module.addImport("discord.zig", dzig);
marin.root_module.addImport("ws", websocket.module("websocket"));
marin.root_module.addImport("zlib", zlib.module("zlib"));
marin.root_module.addImport("zmpl", zmpl.module("zmpl"));
marin.root_module.addImport("deque", deque.module("zig-deque"));
marin.root_module.addImport("json", json_parse.module("zjson"));
//b.installArtifact(marin);

View File

@ -33,12 +33,16 @@
},
.zmpl = .{
.url = "https://github.com/jetzig-framework/zmpl/archive/refs/heads/main.zip",
.hash = "1220798a4647e3b0766aad653830a2601e11c567ba6bfe83e526eb91d04a6c45f7d8",
.hash = "1220ed127f38fa51df53a85b3cc2030a7555e34058db7fd374ebaef817abb43d35f7",
},
.websocket = .{
.url = "https://github.com/yuzudev/websocket.zig/archive/refs/heads/master.zip",
.hash = "12207c03624f9f5a1c444bde3d484a9b1e927a902067fded98364b714de412d318e0",
},
.zjson = .{
.url = "git+https://codeberg.org/yuzu/json#322d90128e3a78b7639c7eb849aeee027613034a",
.hash = "12209277a5606fc3d1c6491198b787ce977ccbfc9371efb4e295a2b410d231c67e2e",
},
},
.paths = .{
"build.zig",

View File

@ -3,59 +3,16 @@ const Discord = @import("types.zig");
const std = @import("std");
const mem = std.mem;
const Snowflake = @import("shared.zig").Snowflake;
const Parser = @import("json");
pub fn parseUser(_: mem.Allocator, obj: *zmpl.Data.Object) std.fmt.ParseIntError!Discord.User {
const avatar_decoration_data_obj = obj.getT(.object, "avatar_decoration_data");
const user = Discord.User{
.clan = null,
.id = try Snowflake.fromRaw(obj.getT(.string, "id").?),
.bot = obj.getT(.boolean, "bot") orelse false,
.username = obj.getT(.string, "username").?,
.accent_color = if (obj.getT(.integer, "accent_color")) |ac| @as(isize, @intCast(ac)) else null,
// note: for selfbots this can be typed with an enu.?,
.flags = if (obj.getT(.integer, "flags")) |fs| @as(isize, @intCast(fs)) else null,
// also for selfbot.?,
.email = obj.getT(.string, "email"),
.avatar = obj.getT(.string, "avatar"),
.locale = obj.getT(.string, "locale"),
.system = obj.getT(.boolean, "system"),
.banner = obj.getT(.string, "banner"),
.verified = obj.getT(.boolean, "verified"),
.global_name = obj.getT(.string, "global_name"),
.mfa_enabled = obj.getT(.boolean, "mfa_enabled"),
.public_flags = if (obj.getT(.integer, "public_flags")) |pfs| @as(isize, @intCast(pfs)) else null,
.premium_type = if (obj.getT(.integer, "premium_type")) |pfs| @as(Discord.PremiumTypes, @enumFromInt(pfs)) else null,
.discriminator = obj.getT(.string, "discriminator").?,
.avatar_decoration_data = if (avatar_decoration_data_obj) |add| Discord.AvatarDecorationData{
.asset = add.getT(.string, "asset").?,
.sku_id = try Snowflake.fromRaw(add.getT(.string, "sku_id").?),
} else null,
};
return user;
pub fn parseUser(allocator: mem.Allocator, obj: []const u8) std.fmt.ParseIntError!Discord.User {
const user= try Parser.parse(Discord.User, allocator, obj);
return user.value;
}
pub fn parseMember(_: mem.Allocator, obj: *zmpl.Data.Object) std.fmt.ParseIntError!Discord.Member {
const avatar_decoration_data_member_obj = obj.getT(.object, "avatar_decoration_data");
const member = Discord.Member{
.deaf = obj.getT(.boolean, "deaf"),
.mute = obj.getT(.boolean, "mute"),
.pending = obj.getT(.boolean, "pending"),
.user = null,
.nick = obj.getT(.string, "nick"),
.avatar = obj.getT(.string, "avatar"),
.roles = &[0][]const u8{},
.joined_at = obj.getT(.string, "joined_at").?,
.premium_since = obj.getT(.string, "premium_since"),
.permissions = obj.getT(.string, "permissions"),
.communication_disabled_until = obj.getT(.string, "communication_disabled_until"),
.flags = @as(isize, @intCast(obj.getT(.integer, "flags").?)),
.avatar_decoration_data = if (avatar_decoration_data_member_obj) |addm| Discord.AvatarDecorationData{
.asset = addm.getT(.string, "asset").?,
.sku_id = try Snowflake.fromRaw(addm.getT(.string, "sku_id").?),
} else null,
};
return member;
pub fn parseMember(allocator: mem.Allocator, obj: []const u8) std.fmt.ParseIntError!Discord.Member {
const member = try Parser.parse(Discord.Member, allocator, obj);
return member.value;
}
/// caller must free the received referenced_message if any

View File

@ -12,6 +12,7 @@ const http = std.http;
// todo use this to read compressed messages
const zlib = @import("zlib");
const zmpl = @import("zmpl");
const json_parse = @import("json");
const Parser = @import("parser.zig");
const Self = @This();
@ -83,7 +84,6 @@ log: Log = .no,
pub const JsonResolutionError = std.fmt.ParseIntError || std.fmt.ParseFloatError || json.ParseFromValueError || json.ParseError(json.Scanner);
/// caller must free the data
fn parseJson(self: *Self, raw: []const u8) JsonResolutionError!zmpl.Data {
var data = zmpl.Data.init(self.allocator);
try data.fromJson(raw);