refactor: move TypedInt to a module

This commit is contained in:
CJ van den Berg 2026-01-22 15:34:19 +01:00
parent 6d86807e21
commit 2ee92d2548
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
3 changed files with 38 additions and 17 deletions

View file

@ -434,11 +434,19 @@ pub fn build_exe(
.root_source_file = b.path("src/lsp_types.zig"), .root_source_file = b.path("src/lsp_types.zig"),
}); });
const TypedInt_mod = b.createModule(.{
.root_source_file = b.path("src/TypedInt.zig"),
.imports = &.{
.{ .name = "cbor", .module = cbor_mod },
},
});
const Buffer_mod = b.createModule(.{ const Buffer_mod = b.createModule(.{
.root_source_file = b.path("src/buffer/Buffer.zig"), .root_source_file = b.path("src/buffer/Buffer.zig"),
.imports = &.{ .imports = &.{
.{ .name = "cbor", .module = cbor_mod }, .{ .name = "cbor", .module = cbor_mod },
.{ .name = "thespian", .module = thespian_mod }, .{ .name = "thespian", .module = thespian_mod },
.{ .name = "TypedInt", .module = TypedInt_mod },
.{ .name = "vaxis", .module = vaxis_mod }, .{ .name = "vaxis", .module = vaxis_mod },
.{ .name = "file_type_config", .module = file_type_config_mod }, .{ .name = "file_type_config", .module = file_type_config_mod },
}, },

28
src/TypedInt.zig Normal file
View file

@ -0,0 +1,28 @@
pub fn Tagged(T: type, tag: []const u8) type {
return enum(T) {
_,
pub const TAG = tag;
pub fn cborEncode(self: @This(), writer: *Writer) Writer.Error!void {
const value: T = @intFromEnum(self);
try cbor.writeValue(writer, .{ TAG, value });
}
pub fn cborExtract(self: *@This(), iter: *[]const u8) cbor.Error!bool {
var value: T = 0;
if (try cbor.matchValue(iter, .{ TAG, cbor.extract(&value) })) {
self.* = @enumFromInt(value);
return true;
}
return false;
}
pub fn format(self: @This(), writer: anytype) !void {
return writer.print("{s}:{d}", .{ TAG, @intFromEnum(self) });
}
};
}
const Writer = @import("std").Io.Writer;
const cbor = @import("cbor");

View file

@ -1,6 +1,7 @@
const std = @import("std"); const std = @import("std");
const builtin = @import("builtin"); const builtin = @import("builtin");
const cbor = @import("cbor"); const cbor = @import("cbor");
const TypedInt = @import("TypedInt");
const file_type_config = @import("file_type_config"); const file_type_config = @import("file_type_config");
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const ArrayList = std.ArrayList; const ArrayList = std.ArrayList;
@ -1755,20 +1756,4 @@ pub fn to_ref(self: *Self) Ref {
return @enumFromInt(@intFromPtr(self)); return @enumFromInt(@intFromPtr(self));
} }
pub const Ref = enum(usize) { pub const Ref = TypedInt.Tagged(usize, "BREF");
_,
pub fn cborEncode(self: @This(), writer: *std.Io.Writer) std.io.Writer.Error!void {
const value: usize = @intFromEnum(self);
try cbor.writeValue(writer, .{ "BREF", value });
}
pub fn cborExtract(self: *@This(), iter: *[]const u8) cbor.Error!bool {
var value: usize = 0;
if (try cbor.matchValue(iter, .{ "BREF", cbor.extract(&value) })) {
self.* = @enumFromInt(value);
return true;
}
return false;
}
};