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

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");