From 2964c918cc49f781e98240e0c2244d17bd7a138a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Fri, 18 Feb 2022 20:35:42 +0200 Subject: [PATCH] remove user.zig dependency on shell.zig --- src/user.zig | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/user.zig b/src/user.zig index a5c42f3..9cbc6e6 100644 --- a/src/user.zig +++ b/src/user.zig @@ -1,5 +1,4 @@ const std = @import("std"); -const shell = @import("shell.zig"); const Allocator = std.mem.Allocator; @@ -27,18 +26,26 @@ pub const User = struct { // UserWriter accepts a naive User struct and returns a PackedUser pub const UserWriter = struct { - allocator: Allocator, - pub fn init(allocator: Allocator) UserWriter { + // shellIndexFnType is a signature for a function that accepts a shell + // string and returns it's index in the global shell section. Passing a + // function makes tests easier, and removes the Shell dependency of this + // module. + const shellIndexFnType = fn ([]const u8) ?u10; + allocator: Allocator, + shellIndexFn: shellIndexFnType, + + pub fn init(allocator: Allocator, shellIndexFn: shellIndexFnType) UserWriter { return UserWriter{ .allocator = allocator, + .shellIndexFn = shellIndexFn, }; } - pub fn fromUser(user: User, shellw: shell.ShellWriter) !PackedUser { + pub fn fromUser(self: *UserWriter, user: User) !PackedUser { var shell_here: u1 = undefined; var shell_len_or_place: u6 = undefined; - if (shellw.getIndex(user.shell)) |idx| { + if (self.shellIndexFn(user.shell)) |idx| { shell_here = false; shell_len_or_place = idx; } else { @@ -58,7 +65,6 @@ pub const UserWriter = struct { .gecos_len = undefined, }; - _ = shellw; return puser; } };