aboutsummaryrefslogtreecommitdiff
path: root/src/bencode.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/bencode.zig')
-rw-r--r--src/bencode.zig23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/bencode.zig b/src/bencode.zig
index 44dec0d..d09f394 100644
--- a/src/bencode.zig
+++ b/src/bencode.zig
@@ -70,6 +70,29 @@ pub const BValue = union(enum) {
.int => {},
}
}
+
+ pub fn asDict(self: BValue) !std.StringArrayHashMap(BValue) {
+ switch (self) {
+ .dict => |d| return d,
+ else => return error.WrongType,
+ }
+ }
+
+ pub fn asInt(self: BValue, comptime itype: type) !itype {
+ switch (self) {
+ .int => |i| {
+ return std.math.cast(itype, i) orelse error.Overflow;
+ },
+ else => return error.WrongType,
+ }
+ }
+
+ pub fn asString(self: BValue) ![]const u8 {
+ switch (self) {
+ .string => |s| return s,
+ else => return error.WrongType,
+ }
+ }
};
pub fn bdecodeBuf(a: std.mem.Allocator, buf: []const u8) !BValue {