From 7eff166e1f7b440392be1082e3edd0c38b92d77c Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Sat, 11 Nov 2023 11:20:15 +0000 Subject: Add basic meta info file parsing --- src/bencode.zig | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/bencode.zig') 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 { -- cgit v1.2.3-ZIG