Skip to content

Commit

Permalink
Update for Zig 0.11.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rutgerbrf committed Nov 1, 2023
1 parent 04252ba commit e337887
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 34 deletions.
49 changes: 32 additions & 17 deletions build.zig
Original file line number Diff line number Diff line change
@@ -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);
}
Expand Down
14 changes: 7 additions & 7 deletions src/base32.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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`.
Expand Down Expand Up @@ -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;
}
Expand All @@ -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.
Expand All @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/crc16.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down
12 changes: 6 additions & 6 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions tool/znk.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit e337887

Please sign in to comment.