authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-08-24 02:37:00 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-08-24 02:37:00 -07:00
logb8b264d1de2ce1421d815cbc05cf8a662e627b3b
tree45b1e30a9b4797d4885ab845e003fd534ef7f373
parent02fb7045dc16c791436f7f4127c8a072e7acdff8

start using nio and nfs


4 files changed, 40 insertions(+), 18 deletions(-)

json.zig+14-6
...@@ -3,20 +3,21 @@ const string = []const u8;...@@ -3,20 +3,21 @@ const string = []const u8;
3const extras = @import("extras");3const extras = @import("extras");
4const tracer = @import("tracer");4const tracer = @import("tracer");
5const intrusive_parser = @import("intrusive-parser");5const intrusive_parser = @import("intrusive-parser");
6const nio = @import("nio");
67
7pub const Error = error{ OutOfMemory, EndOfStream, MalformedJson };8pub const Error = error{ OutOfMemory, EndOfStream, MalformedJson };
8pub const ObjectHashMap = std.AutoArrayHashMapUnmanaged(StringIndex, ValueIndex);9pub const ObjectHashMap = std.AutoArrayHashMapUnmanaged(StringIndex, ValueIndex);
910
10pub fn parse(alloc: std.mem.Allocator, path: string, inreader: anytype, options: Parser.Options) (@TypeOf(inreader).Error || Error)!Document {11pub fn parse(alloc: std.mem.Allocator, path: string, inreadable: anytype, options: Parser.Options) (Instance(@TypeOf(inreadable)).ReadError || Error)!Document {
11 const t = tracer.trace(@src(), "", .{});12 const t = tracer.trace(@src(), "", .{});
12 defer t.end();13 defer t.end();
1314
14 _ = path;15 _ = path;
1516
16 var p = try Parser.init(alloc, inreader.any(), options);17 var p = try Parser.init(alloc, inreadable.anyReadable(), options);
17 defer p.deinit();18 defer p.deinit();
1819
19 const root = try parseElementPrecise(alloc, &p, @TypeOf(inreader).Error || Error);20 const root = try parseElementPrecise(alloc, &p, Instance(@TypeOf(inreadable)).ReadError || Error);
20 if (p.parser.avail() > 0) return error.MalformedJson;21 if (p.parser.avail() > 0) return error.MalformedJson;
21 const data = try p.parser.data.toOwnedSlice(alloc);22 const data = try p.parser.data.toOwnedSlice(alloc);
2223
...@@ -26,9 +27,16 @@ pub fn parse(alloc: std.mem.Allocator, path: string, inreader: anytype, options:...@@ -26,9 +27,16 @@ pub fn parse(alloc: std.mem.Allocator, path: string, inreader: anytype, options:
26 };27 };
27}28}
2829
30fn Instance(T: type) type {
31 return switch (@typeInfo(T)) {
32 .pointer => |info| info.child,
33 else => T,
34 };
35}
36
29pub fn parseFromSlice(alloc: std.mem.Allocator, path: string, input: string, options: Parser.Options) !Document {37pub fn parseFromSlice(alloc: std.mem.Allocator, path: string, input: string, options: Parser.Options) !Document {
30 var fbs = std.io.fixedBufferStream(input);38 var fbs: nio.FixedBufferStream(string) = .init(input);
31 return parse(alloc, path, fbs.reader(), options);39 return parse(alloc, path, &fbs, options);
32}40}
3341
34fn parseElementPrecise(alloc: std.mem.Allocator, p: *Parser, comptime E: type) E!ValueIndex {42fn parseElementPrecise(alloc: std.mem.Allocator, p: *Parser, comptime E: type) E!ValueIndex {
...@@ -253,7 +261,7 @@ pub const Parser = struct {...@@ -253,7 +261,7 @@ pub const Parser = struct {
253 support_trailing_commas: bool,261 support_trailing_commas: bool,
254 maximum_depth: u16,262 maximum_depth: u16,
255263
256 pub fn init(allocator: std.mem.Allocator, any: std.io.AnyReader, options: Options) !Parser {264 pub fn init(allocator: std.mem.Allocator, any: nio.AnyReadable, options: Options) !Parser {
257 var p: Parser = .{265 var p: Parser = .{
258 .parser = intrusive_parser.Parser.init(allocator, any, @intFromEnum(Value.Tag.string)),266 .parser = intrusive_parser.Parser.init(allocator, any, @intFromEnum(Value.Tag.string)),
259 .support_trailing_commas = options.support_trailing_commas,267 .support_trailing_commas = options.support_trailing_commas,
licenses.txt+8
...@@ -1,10 +1,18 @@...@@ -1,10 +1,18 @@
1MIT:1MIT:
2= https://spdx.org/licenses/MIT2= https://spdx.org/licenses/MIT
3- This3- This
4- git https://github.com/nektro/zig-errno
4- git https://github.com/nektro/zig-extras5- git https://github.com/nektro/zig-extras
6- git https://github.com/nektro/zig-libc
7- git https://github.com/nektro/zig-sys-libc
5- git https://github.com/nektro/zig-tracer8- git https://github.com/nektro/zig-tracer
6- git https://github.com/nst/JSONTestSuite9- git https://github.com/nst/JSONTestSuite
710
8MPL-2.0:11MPL-2.0:
9= https://spdx.org/licenses/MPL-2.012= https://spdx.org/licenses/MPL-2.0
10- git https://github.com/nektro/zig-intrusive-parser13- git https://github.com/nektro/zig-intrusive-parser
14- git https://github.com/nektro/zig-nfs
15- git https://github.com/nektro/zig-nio
16
17Unspecified:
18- system_lib c
test.zig+15-12
...@@ -1,12 +1,15 @@...@@ -1,12 +1,15 @@
1const std = @import("std");1const std = @import("std");
2const json = @import("json");2const json = @import("json");
3const build_options = @import("build_options");3const build_options = @import("build_options");
4const nfs = @import("nfs");
5const nio = @import("nio");
6
4const JSONTestSuite_root = build_options.JSONTestSuite_root;7const JSONTestSuite_root = build_options.JSONTestSuite_root;
58
6fn parse_full(buffer: []const u8) !void {9fn parse_full(buffer: []const u8) !void {
7 const alloc = std.testing.allocator;10 const alloc = std.testing.allocator;
8 var fbs = std.io.fixedBufferStream(buffer);11 var fbs: nio.FixedBufferStream([]const u8) = .init(buffer);
9 var doc = try json.parse(alloc, "", fbs.reader(), .{ .support_trailing_commas = true, .maximum_depth = 100 });12 var doc = try json.parse(alloc, "", &fbs, .{ .support_trailing_commas = true, .maximum_depth = 100 });
10 defer doc.deinit(alloc);13 defer doc.deinit(alloc);
11}14}
1215
...@@ -260,11 +263,11 @@ test {...@@ -260,11 +263,11 @@ test {
260 );263 );
261}264}
262265
263fn expectPass(path: []const u8) !void {266fn expectPass(path: [:0]const u8) !void {
264 const alloc = std.testing.allocator;267 const alloc = std.testing.allocator;
265 var file = try std.fs.cwd().openFile(path, .{});268 const file = try nfs.cwd().openFile(path, .{});
266 defer file.close();269 defer file.close();
267 var doc = try json.parse(alloc, path, file.reader(), .{ .support_trailing_commas = false, .maximum_depth = 100 });270 var doc = try json.parse(alloc, path, file, .{ .support_trailing_commas = false, .maximum_depth = 100 });
268 defer doc.deinit(alloc);271 defer doc.deinit(alloc);
269}272}
270273
...@@ -366,11 +369,11 @@ test { try expectPass(JSONTestSuite_root ++ "/y_structure_true_in_array.json");...@@ -366,11 +369,11 @@ test { try expectPass(JSONTestSuite_root ++ "/y_structure_true_in_array.json");
366test { try expectPass(JSONTestSuite_root ++ "/y_structure_whitespace_array.json"); }369test { try expectPass(JSONTestSuite_root ++ "/y_structure_whitespace_array.json"); }
367// zig fmt: on370// zig fmt: on
368371
369fn expectFail(path: []const u8) !void {372fn expectFail(path: [:0]const u8) !void {
370 const alloc = std.testing.allocator;373 const alloc = std.testing.allocator;
371 var file = try std.fs.cwd().openFile(path, .{});374 const file = try nfs.cwd().openFile(path, .{});
372 defer file.close();375 defer file.close();
373 var doc = json.parse(alloc, path, file.reader(), .{ .support_trailing_commas = false, .maximum_depth = 100 }) catch return;376 var doc = json.parse(alloc, path, file, .{ .support_trailing_commas = false, .maximum_depth = 100 }) catch return;
374 defer doc.deinit(alloc);377 defer doc.deinit(alloc);
375 try std.testing.expect(false);378 try std.testing.expect(false);
376}379}
...@@ -568,8 +571,8 @@ test { try expectFail(JSONTestSuite_root ++ "/n_structure_whitespace_U+2060_word...@@ -568,8 +571,8 @@ test { try expectFail(JSONTestSuite_root ++ "/n_structure_whitespace_U+2060_word
568571
569fn expectCanonical(buffer: []const u8) !void {572fn expectCanonical(buffer: []const u8) !void {
570 const alloc = std.testing.allocator;573 const alloc = std.testing.allocator;
571 var fbs = std.io.fixedBufferStream(buffer);574 var fbs: nio.FixedBufferStream([]const u8) = .init(buffer);
572 var doc = try json.parse(alloc, "", fbs.reader(), .{ .support_trailing_commas = true, .maximum_depth = 100 });575 var doc = try json.parse(alloc, "", &fbs, .{ .support_trailing_commas = true, .maximum_depth = 100 });
573 defer doc.deinit(alloc);576 defer doc.deinit(alloc);
574 doc.acquire();577 doc.acquire();
575 defer doc.release();578 defer doc.release();
...@@ -589,10 +592,10 @@ test { try expectCanonical("[7,8,9]"); }...@@ -589,10 +592,10 @@ test { try expectCanonical("[7,8,9]"); }
589592
590test {593test {
591 const alloc = std.testing.allocator;594 const alloc = std.testing.allocator;
592 var fbs = std.io.fixedBufferStream(595 var fbs: nio.FixedBufferStream([]const u8) = .init(
593 \\["abc",456,"ghi",{"foo":"bar"}]596 \\["abc",456,"ghi",{"foo":"bar"}]
594 );597 );
595 var doc = try json.parse(alloc, "", fbs.reader(), .{ .support_trailing_commas = true, .maximum_depth = 100 });598 var doc = try json.parse(alloc, "", &fbs, .{ .support_trailing_commas = true, .maximum_depth = 100 });
596 defer doc.deinit(alloc);599 defer doc.deinit(alloc);
597 doc.acquire();600 doc.acquire();
598 defer doc.release();601 defer doc.release();
zig.mod+3
...@@ -7,8 +7,11 @@ dependencies:...@@ -7,8 +7,11 @@ dependencies:
7 - src: git https://github.com/nektro/zig-extras7 - src: git https://github.com/nektro/zig-extras
8 - src: git https://github.com/nektro/zig-tracer8 - src: git https://github.com/nektro/zig-tracer
9 - src: git https://github.com/nektro/zig-intrusive-parser9 - src: git https://github.com/nektro/zig-intrusive-parser
10 - src: git https://github.com/nektro/zig-nio
1011
11root_dependencies:12root_dependencies:
13 - src: git https://github.com/nektro/zig-nio
14 - src: git https://github.com/nektro/zig-nfs
12 - src: git https://github.com/nst/JSONTestSuite commit-984defc2deaa653cb73cd29f4144a720ec9efe7c15 - src: git https://github.com/nst/JSONTestSuite commit-984defc2deaa653cb73cd29f4144a720ec9efe7c
13 id: bebdygynna6kk8zbbprkva6yd28x9wmf57vtzxjn16 id: bebdygynna6kk8zbbprkva6yd28x9wmf57vtzxjn
14 license: MIT17 license: MIT