From e33788715ce08f9410290dcea9ca0ddb26692eed Mon Sep 17 00:00:00 2001 From: Rutger Broekhoff Date: Wed, 1 Nov 2023 16:48:02 +0100 Subject: Update for Zig 0.11.0 --- build.zig | 49 ++++++++++++++++++++++++++++++++----------------- src/base32.zig | 14 +++++++------- src/crc16.zig | 4 ++-- src/main.zig | 12 ++++++------ tool/znk.zig | 4 ++-- 5 files changed, 49 insertions(+), 34 deletions(-) diff --git a/build.zig b/build.zig index 412408b..947511b 100644 --- a/build.zig +++ b/build.zig @@ -1,41 +1,56 @@ const std = @import("std"); pub fn build(b: *std.build.Builder) !void { - const mode = b.standardReleaseOptions(); const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); - const lib_tests = b.addTest("src/main.zig"); - lib_tests.setBuildMode(mode); + const lib_tests = b.addTest(.{ + .root_source_file = .{ .path = "src/main.zig" }, + .target = target, + .optimize = optimize, + }); + const run_lib_tests = b.addRunArtifact(lib_tests); const test_step = b.step("test", "Run library tests"); - test_step.dependOn(&lib_tests.step); + test_step.dependOn(&run_lib_tests.step); const znk_version = "0.2.1"; const znk_options = b.addOptions(); znk_options.addOption([]const u8, "version", znk_version); - const znk_tests = b.addTest("tool/znk.zig"); - znk_tests.setBuildMode(mode); - znk_tests.addPackagePath("nkeys", "src/main.zig"); + const nkeys_module = b.addModule("nkeys", .{ + .source_file = .{ .path = "src/main.zig" }, + }); + + const znk_tests = b.addTest(.{ + .root_source_file = .{ .path = "tool/znk.zig" }, + .target = target, + .optimize = optimize, + }); + znk_tests.addModule("nkeys", nkeys_module); znk_tests.addOptions("build_options", znk_options); + const run_znk_tests = b.addRunArtifact(znk_tests); const znk_test_step = b.step("test-znk", "Run znk tests"); - znk_test_step.dependOn(&znk_tests.step); - - const znk = b.addExecutable("znk", "tool/znk.zig"); - znk.setBuildMode(mode); - znk.setTarget(target); - znk.addPackagePath("nkeys", "src/main.zig"); + znk_test_step.dependOn(&run_znk_tests.step); + + const znk = b.addExecutable(.{ + .name = "znk", + .root_source_file = .{ .path = "tool/znk.zig" }, + .target = target, + .optimize = optimize, + }); + znk.addModule("nkeys", nkeys_module); znk.addOptions("build_options", znk_options); - const znk_install = b.addInstallArtifact(znk); + b.installArtifact(znk); const znk_step = b.step("znk", "Build znk"); - znk_step.dependOn(&znk_install.step); + znk_step.dependOn(b.getInstallStep()); - const znk_run_cmd = znk.run(); - znk_run_cmd.step.dependOn(&znk_install.step); + const znk_run_cmd = b.addRunArtifact(znk); + znk_run_cmd.step.dependOn(b.getInstallStep()); if (b.args) |args| { znk_run_cmd.addArgs(args); } diff --git a/src/base32.zig b/src/base32.zig index 4e79a73..bce6254 100644 --- a/src/base32.zig +++ b/src/base32.zig @@ -66,8 +66,8 @@ pub const Encoder = struct { fn frontBits(self: *const Self) ?u5 { const index = self.index orelse return null; const bits = self.buffer[index]; - if (self.bit_off >= 4) return @truncate(u5, bits << (self.bit_off - 3)); - return @truncate(u5, bits >> (3 - self.bit_off)); + if (self.bit_off >= 4) return @as(u5, @truncate(bits << (self.bit_off - 3))); + return @as(u5, @truncate(bits >> (3 - self.bit_off))); } /// Get the bits of `self.buffer[self.index]` with the maximum amount specified by the `bits` parameter, @@ -85,7 +85,7 @@ pub const Encoder = struct { fn backBits(self: *const Self, bits: u3) u5 { std.debug.assert(bits <= 5); if (bits == 0 or self.index == null) return 0; - return @truncate(u5, self.buffer[self.index.?] >> (7 - bits + 1)); + return @as(u5, @truncate(self.buffer[self.index.?] >> (7 - bits + 1))); } /// Get the next 5-bit integer, read from `self.buffer`. @@ -172,10 +172,10 @@ pub const Decoder = struct { /// Get a character from the buffer. fn decodeChar(c: u8) DecodeError!u5 { if (c >= 'A' and c <= 'Z') { - return @truncate(u5, c - @as(u8, 'A')); + return @as(u5, @truncate(c - @as(u8, 'A'))); } else if (c >= '2' and c <= '9') { // '2' -> 26 - return @truncate(u5, c - @as(u8, '2') + 26); + return @as(u5, @truncate(c - @as(u8, '2') + 26)); } else { return error.CorruptInput; } @@ -200,7 +200,7 @@ pub const Decoder = struct { // Calculate how many bits of the decoded remain when we've written part of it to the buffer. const c_remaining_len = 5 - buf_write_len; // Write (the first) part of the decoded character to the buffer. - self.buf |= (@as(u8, c) << 3) >> @truncate(u3, self.buf_len); + self.buf |= (@as(u8, c) << 3) >> @as(u3, @truncate(self.buf_len)); self.buf_len += buf_write_len; if (self.buf_len == 8) { // The buffer is full, we can return a byte. @@ -210,7 +210,7 @@ pub const Decoder = struct { if (buf_write_len != 5) { // We didn't write the entire decoded character to the buffer. // Write the remaining part to the beginning of the buffer. - self.buf = @as(u8, c) << @truncate(u3, buf_write_len + 3); + self.buf = @as(u8, c) << @as(u3, @truncate(buf_write_len + 3)); } return ret; } diff --git a/src/crc16.zig b/src/crc16.zig index 3ea5a15..1257ea2 100644 --- a/src/crc16.zig +++ b/src/crc16.zig @@ -7,7 +7,7 @@ const crc16tab: [256]u16 = tab: { const poly: u32 = 0x1021; var table: [256]u16 = undefined; - for (table) |*crc, i| { + for (&table, 0..) |*crc, i| { crc.* = @as(u16, i) << 8; var j = 0; while (j < 8) : (j += 1) { @@ -25,7 +25,7 @@ const crc16tab: [256]u16 = tab: { pub fn update(crc: u16, with_data: []const u8) u16 { var new_crc = crc; for (with_data) |b| { - new_crc = (new_crc << 8) ^ crc16tab[@truncate(u8, new_crc >> 8) ^ b]; + new_crc = (new_crc << 8) ^ crc16tab[@as(u8, @truncate(new_crc >> 8)) ^ b]; } return new_crc; } diff --git a/src/main.zig b/src/main.zig index 6f68295..18c86d1 100644 --- a/src/main.zig +++ b/src/main.zig @@ -393,7 +393,7 @@ pub fn areKeySectionContentsValid(contents: []const u8) bool { return true; } -pub fn findKeySection(line_it: *std.mem.SplitIterator(u8)) ?[]const u8 { +pub fn findKeySection(line_it: *std.mem.SplitIterator(u8, .scalar)) ?[]const u8 { while (true) { const opening_line = line_it.next() orelse return null; if (!isKeySectionBarrier(opening_line, true)) continue; @@ -409,12 +409,12 @@ pub fn findKeySection(line_it: *std.mem.SplitIterator(u8)) ?[]const u8 { } pub fn parseDecoratedJwt(contents: []const u8) []const u8 { - var line_it = mem.split(u8, contents, "\n"); + var line_it = mem.splitScalar(u8, contents, '\n'); return findKeySection(&line_it) orelse return contents; } pub fn parseDecoratedNkey(contents: []const u8) NoNkeySeedFoundError!SeedKeyPair { - var line_it = mem.split(u8, contents, "\n"); + var line_it = mem.splitScalar(u8, contents, '\n'); var seed: ?[]const u8 = null; if (findKeySection(&line_it) != null) seed = findKeySection(&line_it); @@ -444,8 +444,8 @@ fn isValidCredsNkey(text: []const u8) bool { fn findNkey(text: []const u8) ?[]const u8 { var line_it = std.mem.split(u8, text, "\n"); while (line_it.next()) |line| { - for (line) |c, i| { - if (!ascii.isSpace(c)) { + for (line, 0..) |c, i| { + if (!ascii.isWhitespace(c)) { if (isValidCredsNkey(line[i..])) return line[i..]; break; } @@ -554,7 +554,7 @@ test "different key types" { test "validation" { const roles = @typeInfo(Role).Enum.fields; - inline for (roles) |field, i| { + inline for (roles, 0..) |field, i| { const role = @field(Role, field.name); const next_role = next: { const next_field_i = if (i == roles.len - 1) 0 else i + 1; diff --git a/tool/znk.zig b/tool/znk.zig index fcd03ff..554f191 100644 --- a/tool/znk.zig +++ b/tool/znk.zig @@ -38,7 +38,7 @@ const usage = pub fn main() anyerror!void { var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{}){}; - defer std.debug.assert(!general_purpose_allocator.deinit()); + defer std.debug.assert(general_purpose_allocator.deinit() == .ok); const gpa = general_purpose_allocator.allocator(); var arena_instance = std.heap.ArenaAllocator.init(gpa); @@ -475,7 +475,7 @@ fn PrefixKeyGenerator(comptime EntropyReaderType: type) type { fn toUpper(allocator: Allocator, slice: []const u8) ![]u8 { const result = try allocator.alloc(u8, slice.len); - for (slice) |c, i| result[i] = ascii.toUpper(c); + for (slice, 0..) |c, i| result[i] = ascii.toUpper(c); return result; } -- cgit v1.2.3