From 6a7bcc493fca2504c85771d4bb3f018262871a97 Mon Sep 17 00:00:00 2001 From: geemili Date: Tue, 2 Jan 2024 21:11:00 -0700 Subject: [PATCH] feat: update to zig 0.11.0 --- .gitignore | 1 + build.zig | 24 +++++++++++++++++------- src/LDtk.zig | 32 ++++++++++++++------------------ 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index 03299ae..d864d9e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /zig-cache/ +/zig-out/ diff --git a/build.zig b/build.zig index db66c26..2281036 100644 --- a/build.zig +++ b/build.zig @@ -1,17 +1,27 @@ const std = @import("std"); pub fn build(b: *std.build.Builder) void { + const target = b.standardTargetOptions(.{}); // Standard release options allow the person running `zig build` to select // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. - const mode = b.standardReleaseOptions(); + const optimize = b.standardOptimizeOption(.{}); - const lib = b.addStaticLibrary("zig-ldtk", "src/main.zig"); - lib.setBuildMode(mode); - lib.install(); + const lib = b.addStaticLibrary(.{ + .name = "zig-ldtk", + .root_source_file = .{ .path = "src/main.zig" }, + .target = target, + .optimize = optimize, + }); + b.installArtifact(lib); - const main_tests = b.addTest("src/main.zig"); - main_tests.setBuildMode(mode); + const main_tests = b.addTest(.{ + .root_source_file = .{ .path = "src/main.zig" }, + .optimize = optimize, + .target = target, + }); + + const main_tests_run = b.addRunArtifact(main_tests); const test_step = b.step("test", "Run library tests"); - test_step.dependOn(&main_tests.step); + test_step.dependOn(&main_tests_run.step); } diff --git a/src/LDtk.zig b/src/LDtk.zig index 74ed3b0..3c7b48b 100644 --- a/src/LDtk.zig +++ b/src/LDtk.zig @@ -25,22 +25,19 @@ const std = @import("std"); alloc: std.mem.Allocator, root: Root, -parser: std.json.Parser, -value_tree: std.json.ValueTree, +parsed: std.json.Parsed(std.json.Value), pub fn parse(alloc: std.mem.Allocator, ldtk_file: []const u8) !@This() { var this: @This() = undefined; this.alloc = alloc; - this.parser = std.json.Parser.init(alloc, false); - this.value_tree = try this.parser.parse(ldtk_file); - this.root = try Root.fromJSON(alloc, this.value_tree.root); + this.parsed = try std.json.parseFromSlice(std.json.Value, this.alloc, ldtk_file, .{}); + this.root = try Root.fromJSON(alloc, this.parsed.value); return this; } pub fn deinit(this: *@This()) void { this.root.deinit(this.alloc); - this.value_tree.deinit(); - this.parser.deinit(); + this.parsed.deinit(); } /// 1. LDtk Json root @@ -176,7 +173,6 @@ pub const Level = struct { }; } - pub fn fromJSONMany(alloc: std.mem.Allocator, levels_opt: ?std.json.Value) ![]Level { const levels = array(levels_opt) orelse return error.InvalidLevels; var ldtk_levels = try std.ArrayList(Level).initCapacity(alloc, levels.items.len); @@ -255,7 +251,7 @@ pub const LayerInstance = struct { grid_list.appendAssumeCapacity(integer(int) orelse return error.InvalidInt); } - break :grid grid_list.toOwnedSlice(); + break :grid try grid_list.toOwnedSlice(); } } break :grid &[0]i64{}; @@ -327,7 +323,7 @@ const TileInstance = struct { pub fn fromJSON(tile_opt: ?std.json.Value) !TileInstance { const tile = object(tile_opt) orelse return error.InvalidTileInstance; - const f = @intToEnum(FlipBits, integer(tile.get("f")) orelse return error.InvalidFlipBits); + const f = @as(FlipBits, @enumFromInt(integer(tile.get("f")) orelse return error.InvalidFlipBits)); const px = pos_from_value(tile.get("px")) orelse return error.InvalidPx; const src = pos_from_value(tile.get("src")) orelse return error.InvalidSrc; const t = integer(tile.get("t")) orelse return error.InvalidT; @@ -622,7 +618,7 @@ const EnumValueDefinition = struct { pub fn object(value_opt: ?std.json.Value) ?std.json.ObjectMap { const value = value_opt orelse return null; return switch (value) { - .Object => |obj| obj, + .object => |obj| obj, else => null, }; } @@ -631,7 +627,7 @@ pub fn object(value_opt: ?std.json.Value) ?std.json.ObjectMap { pub fn array(value_opt: ?std.json.Value) ?std.json.Array { const value = value_opt orelse return null; return switch (value) { - .Array => |arr| arr, + .array => |arr| arr, else => null, }; } @@ -640,7 +636,7 @@ pub fn array(value_opt: ?std.json.Value) ?std.json.Array { pub fn string(value_opt: ?std.json.Value) ?[]const u8 { const value = value_opt orelse return null; return switch (value) { - .String => |str| str, + .string => |str| str, else => null, }; } @@ -660,7 +656,7 @@ pub fn string_list(alloc: std.mem.Allocator, array_opt: ?std.json.Value) ![][]co pub fn boolean(value_opt: ?std.json.Value) ?bool { const value = value_opt orelse return null; return switch (value) { - .Bool => |b| b, + .bool => |b| b, else => null, }; } @@ -669,7 +665,7 @@ pub fn boolean(value_opt: ?std.json.Value) ?bool { pub fn integer(value_opt: ?std.json.Value) ?i64 { const value = value_opt orelse return null; return switch (value) { - .Integer => |int| int, + .integer => |int| int, else => null, }; } @@ -678,9 +674,9 @@ pub fn integer(value_opt: ?std.json.Value) ?i64 { fn float(value_opt: ?std.json.Value) ?f64 { const value = value_opt orelse return null; return switch (value) { - .Float => |float| float, + .float => |f| f, // Integers are valid floats - .Integer => |int| @intToFloat(f64, int), + .integer => |int| @as(f64, @floatFromInt(int)), else => null, }; } @@ -693,7 +689,7 @@ fn float(value_opt: ?std.json.Value) ?f64 { pub fn enum_from_value(comptime T: type, value_opt: ?std.json.Value) ?T { const value = value_opt orelse return null; return switch (value) { - .String => |str| std.meta.stringToEnum(T, str), + .string => |str| std.meta.stringToEnum(T, str), else => null, }; }