Something went wrong. Try again.
xburner is an experimental HTTP and gRPC load generator written in Zig.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168const std = @import("std");
const whitespace = " \t\r\n";
pub const ParseError = error{ InvalidAssignment, InvalidString, InvalidBoolean, InvalidInteger,};
pub const SchemaError = error{ MissingEnvironmentVariable,};
pub const Assignment = struct { key: []const u8, value: []const u8,};
pub fn parseStringValue(raw: []const u8) ParseError![]const u8 { const value = std.mem.trim(u8, raw, whitespace); if (value.len < 2 or value[0] != '"' or value[value.len - 1] != '"') return error.InvalidString; return value[1 .. value.len - 1];}
pub fn parseString(allocator: std.mem.Allocator, raw: []const u8) (std.mem.Allocator.Error || ParseError)![]const u8 { const value = std.mem.trim(u8, raw, whitespace); if (value.len < 2 or value[0] != '"' or value[value.len - 1] != '"') return error.InvalidString; return std.zig.string_literal.parseAlloc(allocator, value) catch return error.InvalidString;}
pub fn resolveEnvironmentPlaceholders( allocator: std.mem.Allocator, env_lookup: ?*const std.StringHashMapUnmanaged([]const u8), input: []const u8,) (std.mem.Allocator.Error || SchemaError)![]const u8 { var output: std.ArrayList(u8) = .empty; var cursor: usize = 0; var changed = false;
while (std.mem.indexOfPos(u8, input, cursor, "${")) |open_index| { const close_index = std.mem.indexOfScalarPos(u8, input, open_index + 2, '}') orelse break; const key = input[open_index + 2 .. close_index];
if (!changed) { changed = true; try output.appendSlice(allocator, input[0..open_index]); } else { try output.appendSlice(allocator, input[cursor..open_index]); }
if (key.len == 0 or std.mem.indexOfScalar(u8, key, '.') != null) { try output.appendSlice(allocator, input[open_index .. close_index + 1]); } else { const env_value = if (env_lookup) |map| map.get(key) orelse std.process.getEnvVarOwned(allocator, key) catch |err| switch (err) { error.EnvironmentVariableNotFound, error.InvalidWtf8, => return error.MissingEnvironmentVariable, error.OutOfMemory => return error.OutOfMemory, } else std.process.getEnvVarOwned(allocator, key) catch |err| switch (err) { error.EnvironmentVariableNotFound, error.InvalidWtf8, => return error.MissingEnvironmentVariable, error.OutOfMemory => return error.OutOfMemory, }; try output.appendSlice(allocator, env_value); }
cursor = close_index + 1; }
if (!changed) return input;
try output.appendSlice(allocator, input[cursor..]); return try output.toOwnedSlice(allocator);}
pub fn parseBool(raw: []const u8) ParseError!bool { const value = std.mem.trim(u8, raw, whitespace); if (std.mem.eql(u8, value, "true")) return true; if (std.mem.eql(u8, value, "false")) return false; return error.InvalidBoolean;}
pub fn parseU32(raw: []const u8) ParseError!u32 { const value = std.mem.trim(u8, raw, whitespace); return std.fmt.parseInt(u32, value, 10) catch return error.InvalidInteger;}
pub fn splitAssignment(line: []const u8) ParseError!Assignment { const eq_index = findTopLevelChar(line, '=') orelse return error.InvalidAssignment; const key = std.mem.trim(u8, line[0..eq_index], whitespace); const value = std.mem.trim(u8, line[eq_index + 1 ..], whitespace); if (key.len == 0 or value.len == 0) return error.InvalidAssignment; return .{ .key = key, .value = value };}
fn findTopLevelChar(line: []const u8, needle: u8) ?usize { var in_string = false; var escape = false; var brace_depth: usize = 0;
for (line, 0..) |char, index| { if (in_string) { if (escape) { escape = false; continue; } switch (char) { '\\' => escape = true, '"' => in_string = false, else => {}, } continue; }
switch (char) { '"' => in_string = true, '{' => brace_depth += 1, '}' => { if (brace_depth > 0) brace_depth -= 1; }, else => {}, }
if (brace_depth == 0 and char == needle) return index; }
return null;}
pub fn stripComment(raw_line: []const u8) []const u8 { var in_string = false; var escape = false; var brace_depth: usize = 0;
for (raw_line, 0..) |char, index| { if (in_string) { if (escape) { escape = false; continue; } switch (char) { '\\' => escape = true, '"' => in_string = false, else => {}, } continue; }
switch (char) { '"' => in_string = true, '{' => brace_depth += 1, '}' => { if (brace_depth > 0) brace_depth -= 1; }, '#' => if (brace_depth == 0) return raw_line[0..index], else => {}, } }
return raw_line;}