refactor(config): replace the deprecated readToEndAlloc with the new Reader
This commit is contained in:
@@ -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" });
|
const config_abs_path = try std.mem.concat(allocator, u8, &.{ home, "/.config/zigfetch/config.json" });
|
||||||
defer allocator.free(config_abs_path);
|
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,
|
error.FileNotFound => return null,
|
||||||
else => return err,
|
else => return err,
|
||||||
};
|
};
|
||||||
defer file.close();
|
defer config_file.close();
|
||||||
|
|
||||||
const data = try file.readToEndAlloc(allocator, std.math.maxInt(usize));
|
const file_size = (try config_file.stat()).size;
|
||||||
defer allocator.free(data);
|
|
||||||
|
|
||||||
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 });
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user