refactor(config): replace the deprecated readToEndAlloc with the new Reader

This commit is contained in:
utox39
2025-10-07 09:50:06 +02:00
parent 1363bad02d
commit 48ab100e13

View File

@@ -68,14 +68,20 @@ pub fn readConfigFile(allocator: std.mem.Allocator) !?std.json.Parsed(Config) {
const config_abs_path = try std.mem.concat(allocator, u8, &.{ home, "/.config/zigfetch/config.json" });
defer allocator.free(config_abs_path);
const file = std.fs.openFileAbsolute(config_abs_path, .{ .mode = .read_only }) catch |err| switch (err) {
const config_file = std.fs.openFileAbsolute(config_abs_path, .{ .mode = .read_only }) catch |err| switch (err) {
error.FileNotFound => return null,
else => return err,
};
defer file.close();
defer config_file.close();
const data = try file.readToEndAlloc(allocator, std.math.maxInt(usize));
defer allocator.free(data);
const file_size = (try config_file.stat()).size;
return try std.json.parseFromSlice(Config, allocator, data, .{ .allocate = .alloc_always });
var file_buf = try allocator.alloc(u8, file_size);
var reader = std.fs.File.Reader.init(config_file, file_buf);
const read = try reader.read(file_buf);
const config_data = file_buf[0..read];
defer allocator.free(config_data);
return try std.json.parseFromSlice(Config, allocator, config_data, .{ .allocate = .alloc_always });
}