diff options
Diffstat (limited to 'src/storage/user.zig')
| -rw-r--r-- | src/storage/user.zig | 36 |
1 files changed, 32 insertions, 4 deletions
diff --git a/src/storage/user.zig b/src/storage/user.zig index c981661..4170fd1 100644 --- a/src/storage/user.zig +++ b/src/storage/user.zig @@ -3,16 +3,44 @@ const Storage = @import("root.zig"); const Self = @This(); -pub const UserInfo = struct { +pub const Info = struct { name: []const u8, full_name: []const u8, birthday: []const u8, hash: []const u8, is_admin: bool, + + pub fn clone(self: *const @This(), allocator: std.mem.Allocator) !@This() { + var clone_self: @This() = undefined; + var i: usize = 0; + + inline for (std.meta.fields(@This())) |field_info| { + if (field_info.type == []const u8) { + @field(clone_self, field_info.name) = try allocator.dupe( + u8, + @field(self, field_info.name), + ); + errdefer allocator.free(@field(clone_self, field_info.name)); + } else { + @field(clone_self, field_info.name) = @field(self, field_info.name); + } + i += 1; + } + + return clone_self; + } + + pub fn deinit(self: *const @This(), allocator: std.mem.Allocator) void { + inline for (std.meta.fields(@This())) |field_info| { + if (field_info.type == []const u8) { + allocator.free(@field(self, field_info.name)); + } + } + } }; dir: std.fs.Dir, -info: UserInfo, +info: Info, arena: std.heap.ArenaAllocator, pub fn open( @@ -34,7 +62,7 @@ pub fn open( defer file.close(); const content = try file.readToEndAlloc(allocator, std.math.maxInt(usize)); - const info = try std.json.parseFromSliceLeaky(UserInfo, allocator, content, .{}); + const info = try std.json.parseFromSliceLeaky(Info, allocator, content, .{}); return .{ .dir = dir, @@ -79,7 +107,7 @@ pub fn new( hash_buf ); - const info: UserInfo = .{ + const info: Info = .{ .name = name, .full_name = full_name, .birthday = birthday, |