diff --git a/src/main.zig b/src/main.zig index 2bf12ca..7515cf4 100644 --- a/src/main.zig +++ b/src/main.zig @@ -195,6 +195,19 @@ fn startRecirculation(allocator: std.mem.Allocator, id_token: []const u8, serial return result.status == .ok; } +fn removeNewlines(comptime input: []const u8) []const u8 { + comptime { + var result: [input.len]u8 = undefined; + var i: usize = 0; + for (input) |c| { + result[i] = if (c == '\n') ' ' else c; + i += 1; + } + const final = result; + return &final; + } +} + /// Queries the device shadow to get current recirculation status fn getRecirculationStatus(allocator: std.mem.Allocator, id_token: []const u8, serial_number: []const u8) !json.Parsed(json.Value) { var client = http.Client{ .allocator = allocator }; @@ -213,17 +226,7 @@ fn getRecirculationStatus(allocator: std.mem.Allocator, id_token: []const u8, se \\} ; - var query_clean_buf: [query.len]u8 = undefined; - // Remove newlines for JSON payload - const query_replacements = std.mem.replace( - u8, - query, - "\n", - " ", - query_clean_buf[0..], - ); - const query_clean = query_clean_buf[0 .. query.len - ("\n".len * query_replacements)]; - + const query_clean = comptime removeNewlines(query); const body = try std.fmt.allocPrint(allocator, \\{{"query":"{s}","variables":{{"heater_serial_number":"{s}"}}}} , .{ query_clean, serial_number });