diff --git a/.gitignore b/.gitignore index ea74ea37c5c9ab5406266864a1e880a540a111ac..d653d400025efbf26903ffe09a35b456e46253b3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /.zigmod /deps.zig /zig-* +zigmod.lock diff --git a/build.zig b/build.zig index 83de6d152ea7d0d17b33320c2ed62d4b3768a70c..84d11a12b86e8f32000c04aa7284197d73d085fe 100644 --- a/build.zig +++ b/build.zig @@ -1,23 +1,23 @@ const std = @import("std"); const deps = @import("./deps.zig"); -pub fn build(b: *std.build.Builder) void { +pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); + const mode = b.option(std.builtin.Mode, "mode", "") orelse .Debug; - const mode = b.standardReleaseOptions(); - - const exe = b.addExecutable("zig-json", "src/main.zig"); - exe.setTarget(target); - exe.setBuildMode(mode); + const exe = b.addTest(.{ + .root_source_file = b.path("test.zig"), + .target = target, + .optimize = mode, + }); deps.addAllTo(exe); - exe.install(); - const run_cmd = exe.run(); + const run_cmd = b.addRunArtifact(exe); run_cmd.step.dependOn(b.getInstallStep()); if (b.args) |args| { run_cmd.addArgs(args); } - const run_step = b.step("run", "Run the app"); + const run_step = b.step("test", "Run the tests"); run_step.dependOn(&run_cmd.step); } diff --git a/json.zig b/json.zig new file mode 100644 index 0000000000000000000000000000000000000000..95a0b682a71942e4010a9c8ee96461fb9a3b53a3 --- /dev/null +++ b/json.zig @@ -0,0 +1 @@ +const std = @import("std"); diff --git a/licenses.txt b/licenses.txt index 273673b36c71ec7a7220d34aa9c0274010082cdc..6336b470e0dc9933d1fad883c84eaf86174573d6 100644 --- a/licenses.txt +++ b/licenses.txt @@ -1,8 +1,3 @@ MIT: = https://spdx.org/licenses/MIT - This -- git https://github.com/MasterQ32/zig-uri -- git https://github.com/nektro/iguanaTLS -- git https://github.com/nektro/zig-extras -- git https://github.com/truemedian/hzzp -- git https://github.com/truemedian/zfetch diff --git a/src/lib.zig b/src/lib.zig deleted file mode 100644 index 86c3b10b11482abaae8f81ccc8cb10b67b7e4c3a..0000000000000000000000000000000000000000 --- a/src/lib.zig +++ /dev/null @@ -1,190 +0,0 @@ -const std = @import("std"); -const extras = @import("extras"); - -pub const Value = union(enum) { - Object: []Member, - Array: []Value, - String: []const u8, - Int: i64, - Float: f64, - Bool: bool, - Null: void, - - fn get_obj(self: Value, key: []const u8) ?Value { - if (self == .Object) { - for (self.Object) |member| { - if (std.mem.eql(u8, member.key, key)) { - return member.value; - } - } - } - return null; - } - - pub fn get(self: Value, query: anytype) ?Value { - const TO = @TypeOf(query); - if (comptime std.meta.trait.isZigString(TO)) { - return self.get_obj(@as([]const u8, query)); - } - const TI = @typeInfo(TO); - if (TI == .Int or TI == .ComptimeInt) { - if (self == .Array) { - return self.Array[query]; - } - } - return self.fetch_inner(query, 0); - } - - pub fn getT(self: Value, query: anytype, comptime ty: std.meta.FieldEnum(Value)) ?extras.FieldType(Value, ty) { - if (self.get(query)) |val| { - if (val == .Null) return null; - return @field(val, @tagName(ty)); - } - return null; - } - - fn fetch_inner(self: Value, query: anytype, comptime n: usize) ?Value { - if (query.len == n) { - return self; - } - const i = query[n]; - const TO = @TypeOf(i); - const TI = @typeInfo(TO); - if (comptime std.meta.trait.isZigString(TO)) { - if (self.get_obj(i)) |v| { - return v.fetch_inner(query, n + 1); - } - } - if (TI == .Int or TI == .ComptimeInt) { - if (self == .Array) { - return self.Array[i].fetch_inner(query, n + 1); - } - } - return null; - } - - pub fn format(self: Value, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void { - _ = fmt; - _ = options; - - switch (self) { - .Object => { - try writer.writeAll("{"); - for (self.Object, 0..) |member, i| { - if (i > 0) { - try writer.writeAll(", "); - } - try writer.print("{}", .{member}); - } - try writer.writeAll("}"); - }, - .Array => { - try writer.writeAll("["); - for (self.Array, 0..) |val, i| { - if (i > 0) { - try writer.writeAll(", "); - } - try writer.print("{}", .{val}); - } - try writer.writeAll("]"); - }, - .String => { - try writer.print("\"{s}\"", .{self.String}); - }, - .Int => { - try writer.print("{d}", .{self.Int}); - }, - .Float => { - try writer.print("{}", .{self.Float}); - }, - .Bool => { - try writer.print("{}", .{self.Bool}); - }, - .Null => { - try writer.writeAll("null"); - }, - } - } -}; - -pub const Member = struct { - key: []const u8, - value: Value, - - pub fn format(self: Member, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void { - _ = fmt; - _ = options; - - try writer.print("\"{s}\": {}", .{ self.key, self.value }); - } -}; - -const Parser = struct { - p: std.json.Scanner, - - pub fn next(self: *Parser) !?std.json.Token { - const tok = try self.p.next(); - if (tok == .end_of_document) return null; - return tok; - } - - pub const Error = - std.fs.File.OpenError || - std.json.StreamingParser.Error || - std.mem.Allocator.Error || - error{Overflow} || - error{InvalidCharacter} || - error{ JsonExpectedObjKey, JsonExpectedValueStartGotEnd }; -}; - -pub fn parse(alloc: std.mem.Allocator, input: []const u8) Parser.Error!Value { - var p = Parser{ - .p = std.json.Scanner.initCompleteInput(alloc, input), - }; - return try parse_value(&p, null); -} - -fn parse_value(p: *Parser, start: ?std.json.Token) Parser.Error!Value { - const tok = start orelse try p.next(); - return switch (tok.?) { - .ObjectBegin => Value{ .Object = try parse_object(p) }, - .ObjectEnd => error.JsonExpectedValueStartGotEnd, - .ArrayBegin => Value{ .Array = try parse_array(p) }, - .ArrayEnd => error.JsonExpectedValueStartGotEnd, - .String => |t| Value{ .String = t.slice(p.input, p.index - 1) }, - .Number => |t| if (t.is_integer) Value{ .Int = try std.fmt.parseInt(i64, t.slice(p.input, p.index - 1), 10) } else Value{ .Float = try std.fmt.parseFloat(f64, t.slice(p.input, p.index - 1)) }, - .True => Value{ .Bool = true }, - .False => Value{ .Bool = false }, - .Null => Value{ .Null = {} }, - }; -} - -fn parse_object(p: *Parser) Parser.Error![]Member { - var array = std.ArrayList(Member).init(p.alloc); - errdefer array.deinit(); - while (true) { - const tok = try p.next(); - if (tok.? == .ObjectEnd) { - return array.toOwnedSlice(); - } - if (tok.? != .String) { - return error.JsonExpectedObjKey; - } - try array.append(Member{ - .key = tok.?.String.slice(p.input, p.index - 1), - .value = try parse_value(p, null), - }); - } -} - -fn parse_array(p: *Parser) Parser.Error![]Value { - var array = std.ArrayList(Value).init(p.alloc); - errdefer array.deinit(); - while (true) { - const tok = try p.next(); - if (tok.? == .ArrayEnd) { - return array.toOwnedSlice(); - } - try array.append(try parse_value(p, tok.?)); - } -} diff --git a/src/main.zig b/src/main.zig deleted file mode 100644 index 920d08375ce9a61f49b4d80e4b11535aff38c241..0000000000000000000000000000000000000000 --- a/src/main.zig +++ /dev/null @@ -1,33 +0,0 @@ -const std = @import("std"); - -const json = @import("json"); -const zfetch = @import("zfetch"); - -pub fn main() !void { - var gpa = std.heap.GeneralPurposeAllocator(.{}){}; - const alloc = gpa.allocator(); - - const url = "https://old.reddit.com/r/Zig/.json"; - - const req = try zfetch.Request.init(alloc, url, null); - defer req.deinit(); - - try req.do(.GET, null, null); - const r = req.reader(); - - const body_content = try r.readAllAlloc(alloc, std.math.maxInt(usize)); - const val = try json.parse(alloc, body_content); - - std.log.info("hot posts from r/Zig", .{}); - std.log.info("", .{}); - - var count: usize = 1; - for (val.get(.{ "data", "children" }).?.Array) |ch| { - const post = ch.get("data").?; - const title = post.get("title").?.String; - const author = post.get("author").?.String; - std.log.info("{}: {s} -- u/{s} ", .{ count, title, author }); - count += 1; - } - _ = std.math.lossyCast(i32, 2.4); -} diff --git a/test.zig b/test.zig new file mode 100644 index 0000000000000000000000000000000000000000..95a0b682a71942e4010a9c8ee96461fb9a3b53a3 --- /dev/null +++ b/test.zig @@ -0,0 +1 @@ +const std = @import("std"); diff --git a/zig.mod b/zig.mod index 846cd70d857344958f762d6c1cb7393d4993524c..c28a0ed070570c4de57a0475d70bf9dd5b392145 100644 --- a/zig.mod +++ b/zig.mod @@ -1,10 +1,5 @@ id: ocmr9rtohgccd6gm6tp8b1yzylyzkqwvo1q4btrsvj0cse9y name: json -main: src/lib.zig +main: json.zig license: MIT description: "A JSON library for inspecting arbitrary values" -dependencies: - - src: git https://github.com/nektro/zig-extras - -root_dependencies: - - src: git https://github.com/truemedian/zfetch diff --git a/zigmod.lock b/zigmod.lock deleted file mode 100644 index f96dcc1722cabc5b5966110aabebf23b4e2b8347..0000000000000000000000000000000000000000 --- a/zigmod.lock +++ /dev/null @@ -1,8 +0,0 @@ -2 -git https://github.com/nektro/zig-extras commit-fa1138e1a0a2f666b2cd26818e280b6171e417ac -git https://github.com/nektro/zig-range commit-4b2f12808aa09be4b27a163efc424dd4e0415992 -git https://github.com/truemedian/zfetch commit-271cab5da4d12c8f08e67aa0cd5268da100e52f1 -git https://github.com/truemedian/hzzp commit-bf5aaf224e94561e035a631c3c40fbf02faa27d2 -git https://github.com/marler8997/iguanaTLS commit-2b37c575b3df76a4ee040b5a72a4fc277ed4057b -git https://github.com/MasterQ32/zig-network commit-b9c52822f3bb3ce25164a2f1f6eedee4d1c0a279 -git https://github.com/MasterQ32/zig-uri commit-01155026c8362bb75dcab10bafc31abff0c8bac4