move newline removal to comptime

This commit is contained in:
Emil Lerch 2025-12-09 12:57:26 -08:00
parent 7849c5456b
commit d76ace8670
Signed by: lobo
GPG key ID: A7B62D657EF764F8

View file

@ -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 });