From 48ab100e132cb9bd828cb7c416df9a26ea90b589 Mon Sep 17 00:00:00 2001 From: utox39 Date: Tue, 7 Oct 2025 09:50:06 +0200 Subject: [PATCH] refactor(config): replace the deprecated readToEndAlloc with the new Reader --- src/config.zig | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/config.zig b/src/config.zig index ab15185..3609dac 100644 --- a/src/config.zig +++ b/src/config.zig @@ -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 }); }