const std = @import("std"); const lex = @import("lex.zig"); const types = @import("types.zig"); const whitespace = " \t\r\n"; pub const ParseError = lex.ParseError; pub const SchemaError = lex.SchemaError || error{ UnsupportedProtocol, UnsupportedExecutionMode, InvalidMethod, InvalidDuration, InvalidByteSize, InvalidRatio, RatioOutOfRange, InvalidCookieJarScope, InvalidThresholdMetric, InvalidThresholdOperator, InvalidThresholdValue, UnsupportedWasmBackend, UnsupportedHttpVersion, InvalidThresholdExpression, MixedThresholdExpressionOperators, }; pub fn parseProtocol(raw: []const u8) SchemaError!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; } pub fn parseExecutionMode(raw: []const u8) SchemaError!types.Execution.Mode { const value = std.mem.trim(u8, raw, whitespace); if (std.mem.eql(u8, value, "\"constant-vus\"")) return .constant_vus; if (std.mem.eql(u8, value, "\"constant-arrival-rate\"")) return .constant_arrival_rate; if (std.mem.eql(u8, value, "\"ramping-vus\"")) return .ramping_vus; return error.UnsupportedExecutionMode; } pub fn parseWasmBackend(raw: []const u8) SchemaError!types.WasmBackend { const value = std.mem.trim(u8, raw, whitespace); if (std.mem.eql(u8, value, "\"wamr\"")) return .wamr; if (std.mem.eql(u8, value, "\"wasm3\"")) return .wasm3; return error.UnsupportedWasmBackend; } pub fn parseHttpVersion(raw: []const u8) SchemaError!types.HttpVersion { const value = std.mem.trim(u8, raw, whitespace); if (std.mem.eql(u8, value, "\"auto\"")) return .auto; if (std.mem.eql(u8, value, "\"1.1\"")) return .http11; if (std.mem.eql(u8, value, "\"2\"")) return .http2; return error.UnsupportedHttpVersion; } pub fn parseMethod(raw: []const u8) SchemaError!types.HttpMethod { const value = std.mem.trim(u8, raw, whitespace); if (std.mem.eql(u8, value, "\"GET\"")) return .GET; if (std.mem.eql(u8, value, "\"POST\"")) return .POST; if (std.mem.eql(u8, value, "\"PUT\"")) return .PUT; if (std.mem.eql(u8, value, "\"DELETE\"")) return .DELETE; return error.InvalidMethod; } pub fn parseCookieJarScope(raw: []const u8) (ParseError || SchemaError)!types.CookieJarScope { const value = std.mem.trim(u8, raw, whitespace); if (value.len < 2 or value[0] != '"' or value[value.len - 1] != '"') return error.InvalidString; if (std.mem.eql(u8, value, "\"vu\"")) return .vu; if (std.mem.eql(u8, value, "\"iteration\"")) return .iteration; return error.InvalidCookieJarScope; } pub fn parseThresholdMetric(raw: []const u8) (ParseError || SchemaError)!types.ThresholdMetric { const value = std.mem.trim(u8, raw, whitespace); return parseThresholdMetricLiteral(if (value.len >= 2 and value[0] == '"' and value[value.len - 1] == '"') value[1 .. value.len - 1] else value); } pub fn parseThresholdMetricLiteral(value: []const u8) SchemaError!types.ThresholdMetric { if (std.mem.eql(u8, value, "http.request.duration.p50")) return .http_request_duration_p50; if (std.mem.eql(u8, value, "http.request.duration.p50")) return .http_request_duration_p50; if (std.mem.eql(u8, value, "http.request.duration.p90")) return .http_request_duration_p90; if (std.mem.eql(u8, value, "http.request.duration.p99")) return .http_request_duration_p99; if (std.mem.eql(u8, value, "http.error_rate")) return .http_error_rate; if (std.mem.eql(u8, value, "http.transport.error_total")) return .http_transport_error_total; if (std.mem.eql(u8, value, "http.transport.error_rate")) return .http_transport_error_rate; if (std.mem.eql(u8, value, "http.transport.timeout_total")) return .http_transport_timeout_total; if (std.mem.eql(u8, value, "http.transport.timeout_rate")) return .http_transport_timeout_rate; if (std.mem.eql(u8, value, "http.transport.connect_timeout_total")) return .http_transport_connect_timeout_total; if (std.mem.eql(u8, value, "http.transport.connect_timeout_rate")) return .http_transport_connect_timeout_rate; if (std.mem.eql(u8, value, "http.transport.io_timeout_total")) return .http_transport_io_timeout_total; if (std.mem.eql(u8, value, "http.transport.io_timeout_rate")) return .http_transport_io_timeout_rate; if (std.mem.eql(u8, value, "check.failed_total")) return .check_failed_total; if (std.mem.eql(u8, value, "check.pass_rate")) return .check_pass_rate; if (std.mem.eql(u8, value, "executor.scheduler_lag.avg")) return .scheduler_lag_avg; if (std.mem.eql(u8, value, "executor.scheduler_lag.max")) return .scheduler_lag_max; if (std.mem.eql(u8, value, "iteration.dropped")) return .iteration_dropped; if (std.mem.eql(u8, value, "iteration.success_rate")) return .iteration_success_rate; return error.InvalidThresholdMetric; } pub fn parseThresholdOperator(raw: []const u8) (ParseError || SchemaError)!types.ThresholdOperator { const literal = try lex.parseStringValue(raw); if (std.mem.eql(u8, literal, "<")) return .lt; if (std.mem.eql(u8, literal, "<=")) return .lte; if (std.mem.eql(u8, literal, ">")) return .gt; if (std.mem.eql(u8, literal, ">=")) return .gte; if (std.mem.eql(u8, literal, "==")) return .eq; return error.InvalidThresholdOperator; } pub fn parseThresholdValue(metric: types.ThresholdMetric, raw_value: []const u8) SchemaError!types.ThresholdValue { return switch (metric.valueKind()) { .duration => .{ .duration = try parseDurationLiteral(raw_value) }, .ratio => .{ .ratio = try parseRatioLiteral(raw_value) }, .integer => .{ .integer = try parseU64Literal(raw_value) }, .float => .{ .float = try parseFloatLiteral(raw_value) }, }; } pub fn parseThresholdExpression( allocator: std.mem.Allocator, expression: []const u8, ) (std.mem.Allocator.Error || SchemaError)!struct { mode: types.ThresholdExpressionMode, clauses: []const types.ThresholdClause, } { const trimmed = std.mem.trim(u8, expression, whitespace); if (trimmed.len == 0) return error.InvalidThresholdExpression; const has_and = std.mem.indexOf(u8, trimmed, "&&") != null; const has_or = std.mem.indexOf(u8, trimmed, "||") != null; if (has_and and has_or) return error.MixedThresholdExpressionOperators; const delimiter = if (has_and) "&&" else if (has_or) "||" else null; const mode: types.ThresholdExpressionMode = if (has_or) .any_of else .all_of; var clauses = std.ArrayList(types.ThresholdClause).empty; defer clauses.deinit(allocator); if (delimiter) |sep| { var iter = std.mem.splitSequence(u8, trimmed, sep); while (iter.next()) |part| { try clauses.append(allocator, try parseThresholdClause(std.mem.trim(u8, part, whitespace))); } } else { try clauses.append(allocator, try parseThresholdClause(trimmed)); } if (clauses.items.len == 0) return error.InvalidThresholdExpression; return .{ .mode = mode, .clauses = try clauses.toOwnedSlice(allocator), }; } fn parseThresholdClause(clause: []const u8) SchemaError!types.ThresholdClause { const operator_and_index = findThresholdOperator(clause) orelse return error.InvalidThresholdExpression; const metric_text = std.mem.trim(u8, clause[0..operator_and_index.index], whitespace); const value_text = std.mem.trim(u8, clause[operator_and_index.index + operator_and_index.literal.len ..], whitespace); if (metric_text.len == 0 or value_text.len == 0) return error.InvalidThresholdExpression; const metric = try parseThresholdMetricLiteral(metric_text); return .{ .metric = metric, .op = operator_and_index.op, .value = try parseThresholdValue(metric, value_text), }; } fn findThresholdOperator(clause: []const u8) ?struct { index: usize, literal: []const u8, op: types.ThresholdOperator, } { const operators = [_]struct { literal: []const u8, op: types.ThresholdOperator, }{ .{ .literal = "<=", .op = .lte }, .{ .literal = ">=", .op = .gte }, .{ .literal = "==", .op = .eq }, .{ .literal = "<", .op = .lt }, .{ .literal = ">", .op = .gt }, }; for (operators) |candidate| { if (std.mem.indexOf(u8, clause, candidate.literal)) |index| { return .{ .index = index, .literal = candidate.literal, .op = candidate.op, }; } } return null; } pub fn parseDurationField( allocator: std.mem.Allocator, env_lookup: ?*const std.StringHashMapUnmanaged([]const u8), raw: []const u8, ) (std.mem.Allocator.Error || ParseError || SchemaError)!types.Duration { const parsed = try lex.parseString(allocator, raw); const literal = try lex.resolveEnvironmentPlaceholders(allocator, env_lookup, parsed); return parseDurationLiteral(literal); } pub fn parseDurationLiteral(value: []const u8) SchemaError!types.Duration { const units = [_]struct { suffix: []const u8, scale: u64, }{ .{ .suffix = "ns", .scale = 1 }, .{ .suffix = "us", .scale = std.time.ns_per_us }, .{ .suffix = "ms", .scale = std.time.ns_per_ms }, .{ .suffix = "h", .scale = 60 * 60 * std.time.ns_per_s }, .{ .suffix = "m", .scale = 60 * std.time.ns_per_s }, .{ .suffix = "s", .scale = std.time.ns_per_s }, }; for (units) |unit| { if (!std.mem.endsWith(u8, value, unit.suffix)) continue; const magnitude_text = value[0 .. value.len - unit.suffix.len]; if (magnitude_text.len == 0) return error.InvalidDuration; const magnitude = std.fmt.parseInt(u64, magnitude_text, 10) catch return error.InvalidDuration; const nanos = std.math.mul(u64, magnitude, unit.scale) catch return error.InvalidDuration; return .{ .nanos = nanos }; } return error.InvalidDuration; } pub fn parseByteSizeLiteral(value: []const u8) SchemaError!types.ByteSize { const units = [_]struct { suffix: []const u8, scale: u64, }{ .{ .suffix = "GiB", .scale = 1024 * 1024 * 1024 }, .{ .suffix = "MiB", .scale = 1024 * 1024 }, .{ .suffix = "KiB", .scale = 1024 }, .{ .suffix = "GB", .scale = 1000 * 1000 * 1000 }, .{ .suffix = "MB", .scale = 1000 * 1000 }, .{ .suffix = "KB", .scale = 1000 }, .{ .suffix = "B", .scale = 1 }, }; for (units) |unit| { if (!std.mem.endsWith(u8, value, unit.suffix)) continue; const magnitude_text = value[0 .. value.len - unit.suffix.len]; if (magnitude_text.len == 0) return error.InvalidByteSize; const magnitude = std.fmt.parseInt(u64, magnitude_text, 10) catch return error.InvalidByteSize; const bytes = std.math.mul(u64, magnitude, unit.scale) catch return error.InvalidByteSize; return .{ .bytes = bytes }; } return error.InvalidByteSize; } pub fn parseRatioLiteral(value: []const u8) SchemaError!f64 { const ratio = parseFloatLiteral(value) catch return error.InvalidRatio; if (ratio < 0 or ratio > 1) return error.RatioOutOfRange; return ratio; } pub fn parseFloatLiteral(value: []const u8) SchemaError!f64 { return std.fmt.parseFloat(f64, value) catch return error.InvalidThresholdValue; } pub fn parseU64Literal(value: []const u8) SchemaError!u64 { return std.fmt.parseInt(u64, value, 10) catch return error.InvalidThresholdValue; }