refactor: move TypedInt to a module
This commit is contained in:
parent
6d86807e21
commit
2ee92d2548
3 changed files with 38 additions and 17 deletions
28
src/TypedInt.zig
Normal file
28
src/TypedInt.zig
Normal 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");
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
const std = @import("std");
|
||||
const builtin = @import("builtin");
|
||||
const cbor = @import("cbor");
|
||||
const TypedInt = @import("TypedInt");
|
||||
const file_type_config = @import("file_type_config");
|
||||
const Allocator = std.mem.Allocator;
|
||||
const ArrayList = std.ArrayList;
|
||||
|
|
@ -1755,20 +1756,4 @@ pub fn to_ref(self: *Self) Ref {
|
|||
return @enumFromInt(@intFromPtr(self));
|
||||
}
|
||||
|
||||
pub const Ref = enum(usize) {
|
||||
_,
|
||||
|
||||
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;
|
||||
}
|
||||
};
|
||||
pub const Ref = TypedInt.Tagged(usize, "BREF");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue