authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-06-02 17:01:56 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-06-02 17:01:56 -07:00
logfba9092061e7b3ff6180d96f6edd2ad413082f80
treea1c26661c5ba9a2f67219f9495e749c629e4f3d8
parent2793e74b8fefef535b951221cdb440c4986f4af3

cleanup errors v2


1 files changed, 5 insertions(+), 4 deletions(-)

json.zig+5-4
...@@ -3,7 +3,7 @@ const string = []const u8;...@@ -3,7 +3,7 @@ const string = []const u8;
3const extras = @import("extras");3const extras = @import("extras");
4const tracer = @import("tracer");4const tracer = @import("tracer");
55
6const Error = std.mem.Allocator.Error || error{ MalformedJson, EndOfStream };6const Error = error{ OutOfMemory, EndOfStream, MalformedJson };
7const ObjectHashMap = std.AutoArrayHashMapUnmanaged(StringIndex, ValueIndex);7const ObjectHashMap = std.AutoArrayHashMapUnmanaged(StringIndex, ValueIndex);
88
9pub fn parse(alloc: std.mem.Allocator, path: string, inreader: anytype, options: Parser.Options) (@TypeOf(inreader).Error || Error)!Document {9pub fn parse(alloc: std.mem.Allocator, path: string, inreader: anytype, options: Parser.Options) (@TypeOf(inreader).Error || Error)!Document {
...@@ -28,7 +28,8 @@ pub fn parse(alloc: std.mem.Allocator, path: string, inreader: anytype, options:...@@ -28,7 +28,8 @@ pub fn parse(alloc: std.mem.Allocator, path: string, inreader: anytype, options:
28 std.debug.assert(try p.addArray(alloc, &.{}) == .empty_array);28 std.debug.assert(try p.addArray(alloc, &.{}) == .empty_array);
29 std.debug.assert(try p.addObject(alloc, &ObjectHashMap{}) == .empty_object);29 std.debug.assert(try p.addObject(alloc, &ObjectHashMap{}) == .empty_object);
3030
31 const root = parseElement(alloc, &p) catch |err| return @errorCast(err);31 const root_err: (@TypeOf(inreader).Error || Error)!ValueIndex = @errorCast(parseElement(alloc, &p));
32 const root = try root_err;
32 if (p.avail() > 0) return error.MalformedJson;33 if (p.avail() > 0) return error.MalformedJson;
33 const data = try p.extras.toOwnedSlice(alloc);34 const data = try p.extras.toOwnedSlice(alloc);
3435
...@@ -351,10 +352,10 @@ const Parser = struct {...@@ -351,10 +352,10 @@ const Parser = struct {
351352
352 pub fn shift(p: *Parser) !u21 {353 pub fn shift(p: *Parser) !u21 {
353 try p.peekAmt(1) orelse return error.EndOfStream;354 try p.peekAmt(1) orelse return error.EndOfStream;
354 const len = try std.unicode.utf8ByteSequenceLength(p.slice()[0]);355 const len = std.unicode.utf8ByteSequenceLength(p.slice()[0]) catch return error.MalformedJson;
355 try p.peekAmt(len) orelse return error.EndOfStream;356 try p.peekAmt(len) orelse return error.EndOfStream;
356 defer p.idx += len;357 defer p.idx += len;
357 return std.unicode.utf8Decode(p.slice()[0..len]);358 return std.unicode.utf8Decode(p.slice()[0..len]) catch return error.MalformedJson;
358 }359 }
359360
360 pub fn shiftBytesN(p: *Parser, comptime n: usize) ![n]u8 {361 pub fn shiftBytesN(p: *Parser, comptime n: usize) ![n]u8 {