const 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; }