prefect server in zig
at main 41 lines 1.6 kB view raw
1const std = @import("std"); 2const mem = std.mem; 3 4/// Extract ID from path between prefix and suffix 5/// e.g., extractId("/flow_runs/abc/set_state", "/flow_runs/", "/set_state") -> "abc" 6pub fn extractId(target: []const u8, prefix: []const u8, suffix: []const u8) ?[]const u8 { 7 if (!mem.startsWith(u8, target, prefix)) return null; 8 if (!mem.endsWith(u8, target, suffix)) return null; 9 const start = prefix.len; 10 const end = target.len - suffix.len; 11 if (start >= end) return null; 12 return target[start..end]; 13} 14 15/// Extract ID from path after prefix (handles both 32 and 36 char UUIDs) 16/// e.g., extractIdAfter("/flow_runs/abc-def", "/flow_runs/") -> "abc-def" 17pub fn extractIdAfter(target: []const u8, prefix: []const u8) ?[]const u8 { 18 if (!mem.startsWith(u8, target, prefix)) return null; 19 const rest = target[prefix.len..]; 20 const id_end = mem.indexOf(u8, rest, "?") orelse rest.len; 21 if (id_end >= 36) { 22 return rest[0..36]; 23 } else if (id_end >= 32) { 24 return rest[0..32]; 25 } 26 return null; 27} 28 29/// Generate a random run name (e.g., "happy-panda") 30pub fn generateRunName(alloc: std.mem.Allocator) []const u8 { 31 const adjectives = [_][]const u8{ "happy", "quick", "brave", "calm", "eager" }; 32 const nouns = [_][]const u8{ "panda", "tiger", "eagle", "dolphin", "falcon" }; 33 34 var rng_buf: [2]u8 = undefined; 35 std.crypto.random.bytes(&rng_buf); 36 37 const adj = adjectives[rng_buf[0] % adjectives.len]; 38 const noun = nouns[rng_buf[1] % nouns.len]; 39 40 return std.fmt.allocPrint(alloc, "{s}-{s}", .{ adj, noun }) catch "unnamed-run"; 41}