feat: add duration parser, ISO timestamps, job state machine; add AGENTS.md
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9e5c3e3af1
commit
8cf40344a4
6 files changed
+261
-16
No files matched your search
@@ -0,0 +1,37 @@
|
||||
//! Duration parser for values like `30m`, `12h`, `7d`, `2w` (case-insensitive unit).
|
||||
//! `M` is minutes, not months, matching barn's `DurationParser`.
|
||||
const std = @import("std");
|
||||
|
||||
pub const Error = error{InvalidDuration};
|
||||
|
||||
/// Parses `^\d+[mhdwMHDW]$` and returns the duration in seconds.
|
||||
pub fn parseSeconds(text: []const u8) Error!u64 {
|
||||
if (text.len < 2) return error.InvalidDuration;
|
||||
const unit = text[text.len - 1];
|
||||
const digits = text[0 .. text.len - 1];
|
||||
for (digits) |c| if (!std.ascii.isDigit(c)) return error.InvalidDuration;
|
||||
const n = std.fmt.parseInt(u64, digits, 10) catch return error.InvalidDuration;
|
||||
const mult: u64 = switch (std.ascii.toLower(unit)) {
|
||||
'm' => 60,
|
||||
'h' => 60 * 60,
|
||||
'd' => 24 * 60 * 60,
|
||||
'w' => 7 * 24 * 60 * 60,
|
||||
else => return error.InvalidDuration,
|
||||
};
|
||||
return std.math.mul(u64, n, mult) catch error.InvalidDuration;
|
||||
}
|
||||
|
||||
test "parses each unit" {
|
||||
try std.testing.expectEqual(@as(u64, 30 * 60), try parseSeconds("30m"));
|
||||
try std.testing.expectEqual(@as(u64, 12 * 3600), try parseSeconds("12H"));
|
||||
try std.testing.expectEqual(@as(u64, 7 * 86400), try parseSeconds("7d"));
|
||||
try std.testing.expectEqual(@as(u64, 2 * 604800), try parseSeconds("2W"));
|
||||
try std.testing.expectEqual(@as(u64, 60), try parseSeconds("1M"));
|
||||
}
|
||||
|
||||
test "rejects invalid input" {
|
||||
for ([_][]const u8{ "", "m", "10", "10s", "-5m", "1.5h", "5 m", "abc" }) |bad| {
|
||||
try std.testing.expectError(error.InvalidDuration, parseSeconds(bad));
|
||||
}
|
||||
try std.testing.expectError(error.InvalidDuration, parseSeconds("99999999999999999999w"));
|
||||
}
|
||||
Reference in new issue
Block a user