minor changes
This commit is contained in:
parent
bd07a4d053
commit
846b265e07
@ -27,11 +27,6 @@ const std = @import("std");
|
||||
const mem = std.mem;
|
||||
const debug = @import("internal.zig").debug;
|
||||
|
||||
/// Calculate and return the shard ID for a given guild ID
|
||||
pub inline fn calculateShardId(guild_id: Snowflake, shards: ?usize) u64 {
|
||||
return (guild_id.into() >> 22) % shards orelse 1;
|
||||
}
|
||||
|
||||
const Self = @This();
|
||||
|
||||
shard_details: ShardDetails,
|
||||
|
@ -1,3 +1,19 @@
|
||||
//! ISC License
|
||||
//!
|
||||
//! Copyright (c) 2024-2025 Yuzu
|
||||
//!
|
||||
//! Permission to use, copy, modify, and/or distribute this software for any
|
||||
//! purpose with or without fee is hereby granted, provided that the above
|
||||
//! copyright notice and this permission notice appear in all copies.
|
||||
//!
|
||||
//! THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
//! REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
//! AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
//! INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
//! LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
//! OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
//! PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
/// an error ought to be matched by `code` for providing the end user with sensible errors
|
||||
pub const DiscordErrorPayload = struct {
|
||||
/// cryptic error code, eg: `MISSING_PERMISSIONS`
|
||||
|
28
src/json.zig
28
src/json.zig
@ -30,7 +30,7 @@
|
||||
//! defer allocator.deinit();
|
||||
//! const result = parseIntoT(MyStruct, "{ \"key\": \"value\" }", allocator);
|
||||
//! ```
|
||||
//! repo: https://codeberg.org/yuzu/json
|
||||
//! repo: https://hg.reactionary.software/repo/zjson/
|
||||
|
||||
const std = @import("std");
|
||||
const mem = std.mem;
|
||||
@ -84,14 +84,14 @@ pub fn Either(comptime L: type, comptime R: type) type {
|
||||
/// always returns .right
|
||||
pub fn unwrap(self: @This()) R {
|
||||
// discord.zig specifics
|
||||
if (@hasField(L, "code") and @hasField(L, "message") and self.value == .left)
|
||||
std.debug.panic("Error: {d}, {s}\n", .{ self.value.left.code, self.value.left.message });
|
||||
if (@hasField(L, "code") and @hasField(L, "message") and self == .left)
|
||||
std.debug.panic("Error: {d}, {s}\n", .{ self.left.code, self.left.message });
|
||||
|
||||
// for other libraries, it'll do this
|
||||
if (self.value == .left)
|
||||
std.debug.panic("Error: {any}\n", .{self.value.left});
|
||||
if (self == .left)
|
||||
std.debug.panic("Error: {any}\n", .{self.left});
|
||||
|
||||
return self.value.right;
|
||||
return self.right;
|
||||
}
|
||||
|
||||
pub fn is(self: @This(), tag: std.meta.Tag(@This())) bool {
|
||||
@ -168,7 +168,7 @@ pub fn jsonString(str: []const u8, allocator: mem.Allocator) ParseResult([]const
|
||||
var i: usize = 0;
|
||||
while (i < string.len) {
|
||||
if (isEscapeSeq(string[i]) and i + 5 < string.len) switch (string[i + 1]) {
|
||||
0x22, 0x5C, 0x2F => |d| try characters.append(d),
|
||||
inline 0x22, 0x5C, 0x2F => |d| try characters.append(d),
|
||||
'b' => try characters.append(0x8),
|
||||
'f' => try characters.append(0xC),
|
||||
'n' => try characters.append(0xA),
|
||||
@ -439,7 +439,7 @@ pub fn jsonObject(str: []const u8, allocator: mem.Allocator) ParseResult(JsonRaw
|
||||
};
|
||||
const str4, _ = try closingCurlyBrace(str3, allocator);
|
||||
|
||||
var obj = JsonRawHashMap{};
|
||||
var obj: JsonRawHashMap = .{};
|
||||
errdefer obj.deinit(allocator);
|
||||
|
||||
for (pairs) |entry| {
|
||||
@ -1140,8 +1140,6 @@ pub fn parseInto(comptime T: type, allocator: mem.Allocator, value: JsonType) Er
|
||||
|
||||
/// meant to handle a `JsonType` value and handling the deinitialization thereof
|
||||
pub fn Owned(comptime T: type) type {
|
||||
// if (@typeInfo(Struct) != .@"struct") @compileError("expected a `struct` type");
|
||||
|
||||
return struct {
|
||||
arena: *std.heap.ArenaAllocator,
|
||||
value: T,
|
||||
@ -1156,8 +1154,6 @@ pub fn Owned(comptime T: type) type {
|
||||
|
||||
/// same as `Owned` but instead it handles 2 different values, generally `.right` is the correct one and `left` the error type
|
||||
pub fn OwnedEither(comptime L: type, comptime R: type) type {
|
||||
// if (@typeInfo(Struct) != .@"struct") @compileError("expected a `struct` type");
|
||||
|
||||
return struct {
|
||||
value: Either(L, R),
|
||||
arena: *std.heap.ArenaAllocator,
|
||||
@ -1229,7 +1225,7 @@ pub fn Record(comptime T: type) type {
|
||||
return struct {
|
||||
map: std.StringHashMapUnmanaged(T),
|
||||
pub fn toJson(allocator: mem.Allocator, value: JsonType) !@This() {
|
||||
var map: std.StringHashMapUnmanaged(T) = .{};
|
||||
var map: std.StringHashMapUnmanaged(T) = .init;
|
||||
|
||||
var iterator = value.object.iterator();
|
||||
|
||||
@ -1275,8 +1271,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() {
|
||||
// TODO: initialize this more efficiently
|
||||
var map = std.EnumMap(E, V){};
|
||||
var map: std.EnumMap(E, V) = .{};
|
||||
|
||||
var iterator = value.object.iterator();
|
||||
|
||||
@ -1284,9 +1279,10 @@ pub fn AssociativeArray(comptime E: type, comptime V: type) type {
|
||||
const k = pair.key_ptr.*;
|
||||
const v = pair.value_ptr.*;
|
||||
|
||||
errdefer allocator.free(k);
|
||||
defer allocator.free(k);
|
||||
errdefer v.deinit(allocator);
|
||||
|
||||
// eg: enum(u8) would be @"enum".tag_type where tag_type is a u8
|
||||
const int = std.fmt.parseInt(@typeInfo(E).@"enum".tag_type, k, 10) catch unreachable;
|
||||
map.put(@enumFromInt(int), try parseInto(V, allocator, v));
|
||||
}
|
||||
|
@ -91,7 +91,7 @@ heart: Heart = .{ .heartbeatInterval = 45000, .lastBeat = 0 },
|
||||
|
||||
///
|
||||
handler: GatewayDispatchEvent(*Self),
|
||||
packets: std.ArrayList(u8),
|
||||
packets: std.ArrayListUnmanaged(u8),
|
||||
inflator: zlib.Decompressor,
|
||||
|
||||
///useful for closing the conn
|
||||
@ -171,7 +171,7 @@ pub fn init(allocator: mem.Allocator, shard_id: usize, settings: struct {
|
||||
.session_id = undefined,
|
||||
.handler = settings.run,
|
||||
.log = settings.log,
|
||||
.packets = std.ArrayList(u8).init(allocator),
|
||||
.packets = .{},
|
||||
.inflator = try zlib.Decompressor.init(allocator, .{ .header = .zlib_or_gzip }),
|
||||
.bucket = Bucket.init(
|
||||
allocator,
|
||||
@ -225,13 +225,13 @@ fn readMessage(self: *Self, _: anytype) !void {
|
||||
while (try self.client.read()) |msg| { // check your intents, dumbass
|
||||
defer self.client.done(msg);
|
||||
|
||||
try self.packets.appendSlice(msg.data);
|
||||
try self.packets.appendSlice(self.allocator, msg.data);
|
||||
|
||||
// end of zlib
|
||||
if (!std.mem.endsWith(u8, msg.data, &[4]u8{ 0x00, 0x00, 0xFF, 0xFF }))
|
||||
continue;
|
||||
|
||||
const buf = try self.packets.toOwnedSlice();
|
||||
const buf = try self.packets.toOwnedSlice(self.allocator);
|
||||
const decompressed = try self.inflator.decompressAllAlloc(buf);
|
||||
defer self.allocator.free(decompressed);
|
||||
|
||||
|
@ -56,7 +56,8 @@ pub const Snowflake = enum(u64) {
|
||||
/// zjson parse
|
||||
pub fn toJson(_: 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});
|
||||
return Snowflake.fromRaw(value.string) catch
|
||||
std.debug.panic("invalid snowflake: {s}\n", .{value.string});
|
||||
unreachable;
|
||||
}
|
||||
|
||||
@ -70,7 +71,12 @@ pub const Snowflake = enum(u64) {
|
||||
try writer.print("\"{d}\"", .{snowflake.into()});
|
||||
}
|
||||
|
||||
pub fn toTimestamp(self: Snowflake) u64 {
|
||||
pub inline fn toTimestamp(self: Snowflake) u64 {
|
||||
return (self.into() >> 22) + discord_epoch;
|
||||
}
|
||||
|
||||
/// Calculate and return the shard ID for a given guild ID
|
||||
pub inline fn calculateShardId(self: Snowflake, shards: ?usize) u64 {
|
||||
return (self.into() >> 22) % shards orelse 1;
|
||||
}
|
||||
};
|
||||
|
@ -30,10 +30,8 @@ fn message_create(session: *Shard, message: Discord.Message) !void {
|
||||
var result = try session.sendMessage(message.channel_id, .{ .content = "hi :)" });
|
||||
defer result.deinit();
|
||||
|
||||
switch (result.value) {
|
||||
.left => |e| std.debug.panic("Error: {d}\r{s}\n", .{ e.code, e.message }),
|
||||
.right => |m| std.debug.print("Sent: {?s} sent by {s}\n", .{ m.content, m.author.username }),
|
||||
}
|
||||
const m = result.value.unwrap();
|
||||
std.debug.print("sent: {?s}\n", .{m.content});
|
||||
};
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user