Skip to content

Commit

Permalink
de: Update visitors to return errors instead of panicking
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Phan committed Oct 1, 2021
1 parent 9493fe4 commit ffa8360
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/de.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const Allocator = @import("std").mem.Allocator;

pub const de = struct {
pub const Error = error{
pub const Error = Allocator.Error || error{
Unsupported,

DuplicateField,
Expand Down
2 changes: 1 addition & 1 deletion src/de/impl/visitor/array.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn Visitor(comptime Value: type) type {
}

if (try sequenceAccess.nextElement(Child)) |_| {
@panic("expected end of sequence, found element");
return error.InvalidLength;
}

return seq;
Expand Down
2 changes: 1 addition & 1 deletion src/de/impl/visitor/enum.zig
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn Visitor(comptime T: type) type {
fn visitSlice(self: *Self, comptime Error: type, input: anytype) Error!Value {
_ = self;

return meta.stringToEnum(Value, input) orelse @panic("could not find enum value");
return meta.stringToEnum(Value, input) orelse return error.UnknownVariant;
}
};
}
2 changes: 1 addition & 1 deletion src/de/impl/visitor/int.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn Visitor(comptime T: type) type {

fn visitFloat(_: *Self, comptime Error: type, input: anytype) Error!Value {
if (math.round(input) != input or (input > math.maxInt(T) or input < math.minInt(T))) {
@panic("Failure during float-to-int cast");
return error.InvalidValue;
}

return @floatToInt(T, input);
Expand Down
5 changes: 2 additions & 3 deletions src/de/impl/visitor/slice.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ pub fn Visitor(comptime T: type) type {
errdefer list.deinit();

while (try seqAccess.nextElement(std.meta.Child(Value))) |elem| {
// TODO: change unreachable
list.append(elem) catch unreachable;
try list.append(elem);
}

return list.toOwnedSlice();
}

fn visitSlice(self: *Self, comptime Error: type, input: anytype) Error!Value {
return self.allocator.dupe(std.meta.Child(Value), input) catch unreachable;
return try self.allocator.dupe(std.meta.Child(Value), input);
}
};
}
4 changes: 2 additions & 2 deletions src/de/impl/visitor/struct.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ pub fn Visitor(comptime Value: type) type {
inline for (std.meta.fields(Value)) |field| {
if (try mapAccess.nextKey([]const u8)) |key| {
if (!std.mem.eql(u8, field.name, key)) {
@panic("incorrect key");
return error.MissingField;
}

@field(map, field.name) = try mapAccess.nextValue(field.field_type);
}
}

if (try mapAccess.nextKey([]const u8)) |_| {
@panic("expected end of map, found key");
return error.UnknownField;
}

return map;
Expand Down

0 comments on commit ffa8360

Please sign in to comment.