diff options
author | Martin Ashby <martin@ashbysoft.com> | 2023-11-11 11:20:15 +0000 |
---|---|---|
committer | Martin Ashby <martin@ashbysoft.com> | 2023-11-11 11:20:15 +0000 |
commit | 7eff166e1f7b440392be1082e3edd0c38b92d77c (patch) | |
tree | b27da98df178cda5a5625634b8f4a27a881e0be3 /src/bencode.zig | |
parent | 2d65d9d3515a523d9cb8d242c3fc89671ae97d63 (diff) | |
download | zbt-7eff166e1f7b440392be1082e3edd0c38b92d77c.tar.gz zbt-7eff166e1f7b440392be1082e3edd0c38b92d77c.tar.bz2 zbt-7eff166e1f7b440392be1082e3edd0c38b92d77c.tar.xz zbt-7eff166e1f7b440392be1082e3edd0c38b92d77c.zip |
Add basic meta info file parsing
Diffstat (limited to 'src/bencode.zig')
-rw-r--r-- | src/bencode.zig | 23 |
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 { |