feat: complete M0 foundations (job ids, atomic fs helpers, Jackson-style JSON writer, TOML subset parser)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8cf40344a4
commit
d05dc22cee
6 files changed
+714
No files matched your search
@@ -0,0 +1,44 @@
|
||||
//! Job ids: `job-` followed by 8 lowercase hex characters (e.g. `job-6d368040`).
|
||||
const std = @import("std");
|
||||
const Io = std.Io;
|
||||
|
||||
pub const len = 12;
|
||||
|
||||
pub fn generate(io: Io) [len]u8 {
|
||||
var raw: [4]u8 = undefined;
|
||||
io.random(&raw);
|
||||
return format(raw);
|
||||
}
|
||||
|
||||
pub fn format(raw: [4]u8) [len]u8 {
|
||||
var out: [len]u8 = undefined;
|
||||
out[0..4].* = "job-".*;
|
||||
out[4..].* = std.fmt.bytesToHex(raw, .lower);
|
||||
return out;
|
||||
}
|
||||
|
||||
pub fn isValid(id: []const u8) bool {
|
||||
if (id.len != len or !std.mem.startsWith(u8, id, "job-")) return false;
|
||||
for (id[4..]) |c| switch (c) {
|
||||
'0'...'9', 'a'...'f' => {},
|
||||
else => return false,
|
||||
};
|
||||
return true;
|
||||
}
|
||||
|
||||
test "format is job- plus 8 lowercase hex" {
|
||||
try std.testing.expectEqualStrings("job-6d368040", &format(.{ 0x6d, 0x36, 0x80, 0x40 }));
|
||||
}
|
||||
|
||||
test "generate produces valid, varying ids" {
|
||||
const a = generate(std.testing.io);
|
||||
const b = generate(std.testing.io);
|
||||
try std.testing.expect(isValid(&a));
|
||||
try std.testing.expect(isValid(&b));
|
||||
}
|
||||
|
||||
test "isValid rejects bad ids" {
|
||||
for ([_][]const u8{ "", "job-", "job-6D368040", "job-6d36804", "job-6d3680400", "xob-6d368040", "job-6d36804g" }) |bad| {
|
||||
try std.testing.expect(!isValid(bad));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user