const std = @import("std"); const lex = @import("lex.zig"); const config_template = @import("template.zig"); const types = @import("types.zig"); const whitespace = " \t\r\n"; pub const ParseError = lex.ParseError || error{ InvalidInlineTable, InvalidHeaderKey, }; pub const SchemaError = lex.SchemaError || error{ UnknownThresholdScopeField, UnsupportedProtocol, }; pub fn parseHeadersInlineTable( allocator: std.mem.Allocator, env_lookup: ?*const std.StringHashMapUnmanaged([]const u8), raw: []const u8, ) (std.mem.Allocator.Error || ParseError || SchemaError)![]const types.HeaderTemplate { const value = std.mem.trim(u8, raw, whitespace); if (value.len < 2 or value[0] != '{' or value[value.len - 1] != '}') return error.InvalidInlineTable; const inner = std.mem.trim(u8, value[1 .. value.len - 1], whitespace); if (inner.len == 0) return &.{}; var headers: std.ArrayList(types.HeaderTemplate) = .empty; var start: usize = 0; var in_string = false; var escape = false; for (inner, 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, ',' => { const part = std.mem.trim(u8, inner[start..index], whitespace); if (part.len == 0) return error.InvalidInlineTable; try headers.append(allocator, try parseHeaderEntry(allocator, env_lookup, part)); start = index + 1; }, else => {}, } } const tail = std.mem.trim(u8, inner[start..], whitespace); if (tail.len == 0) return error.InvalidInlineTable; try headers.append(allocator, try parseHeaderEntry(allocator, env_lookup, tail)); return try headers.toOwnedSlice(allocator); } pub fn mergeDefaultHeaders( allocator: std.mem.Allocator, defaults: []const types.HeaderTemplate, overrides: []const types.HeaderTemplate, ) std.mem.Allocator.Error![]const types.HeaderTemplate { if (defaults.len == 0) return overrides; if (overrides.len == 0) return defaults; var merged_len: usize = overrides.len; for (defaults) |header| { if (!headerListContainsKey(overrides, header.key)) merged_len += 1; } var merged = try allocator.alloc(types.HeaderTemplate, merged_len); var index: usize = 0; for (defaults) |header| { if (headerListContainsKey(overrides, header.key)) continue; merged[index] = header; index += 1; } for (overrides) |header| { merged[index] = header; index += 1; } return merged; } pub fn parseThresholdScopeInlineTable( allocator: std.mem.Allocator, env_lookup: ?*const std.StringHashMapUnmanaged([]const u8), raw: []const u8, ) (std.mem.Allocator.Error || ParseError || SchemaError)!types.ThresholdScope { const value = std.mem.trim(u8, raw, whitespace); if (value.len < 2 or value[0] != '{' or value[value.len - 1] != '}') return error.InvalidInlineTable; const inner = std.mem.trim(u8, value[1 .. value.len - 1], whitespace); if (inner.len == 0) return .{}; var scope: types.ThresholdScope = .{}; var start: usize = 0; var in_string = false; var escape = false; for (inner, 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, ',' => { const part = std.mem.trim(u8, inner[start..index], whitespace); if (part.len == 0) return error.InvalidInlineTable; try applyThresholdScopeEntry(allocator, env_lookup, &scope, part); start = index + 1; }, else => {}, } } const tail = std.mem.trim(u8, inner[start..], whitespace); if (tail.len == 0) return error.InvalidInlineTable; try applyThresholdScopeEntry(allocator, env_lookup, &scope, tail); return scope; } fn parseHeaderEntry( allocator: std.mem.Allocator, env_lookup: ?*const std.StringHashMapUnmanaged([]const u8), entry: []const u8, ) (std.mem.Allocator.Error || ParseError || SchemaError)!types.HeaderTemplate { const assignment = try lex.splitAssignment(entry); const key = std.mem.trim(u8, assignment.key, whitespace); if (key.len == 0) return error.InvalidHeaderKey; return .{ .key = try allocator.dupe(u8, key), .value = try config_template.parseTemplateString(allocator, env_lookup, assignment.value), }; } fn headerListContainsKey(headers: []const types.HeaderTemplate, key: []const u8) bool { for (headers) |header| { if (std.ascii.eqlIgnoreCase(header.key, key)) return true; } return false; } fn applyThresholdScopeEntry( allocator: std.mem.Allocator, env_lookup: ?*const std.StringHashMapUnmanaged([]const u8), scope: *types.ThresholdScope, entry: []const u8, ) (std.mem.Allocator.Error || ParseError || SchemaError)!void { const assignment = try lex.splitAssignment(entry); const key = std.mem.trim(u8, assignment.key, whitespace); if (std.mem.eql(u8, key, "request")) { scope.request = try config_template.parseTemplateString(allocator, env_lookup, assignment.value); return; } if (std.mem.eql(u8, key, "scenario")) { scope.scenario = try config_template.parseTemplateString(allocator, env_lookup, assignment.value); return; } if (std.mem.eql(u8, key, "protocol")) { scope.protocol = try parseProtocol(assignment.value); return; } return error.UnknownThresholdScopeField; } fn parseProtocol(raw: []const u8) error{UnsupportedProtocol}!types.Protocol { const value = std.mem.trim(u8, raw, whitespace); if (std.mem.eql(u8, value, "\"http\"")) return .http; if (std.mem.eql(u8, value, "\"grpc\"")) return .grpc; return error.UnsupportedProtocol; }