formatting

This commit is contained in:
Motiejus Jakštys 2022-02-25 13:59:35 +02:00 committed by Motiejus Jakštys
parent eed4f4e234
commit b0d74c0cad

View File

@ -7,9 +7,9 @@ const ArrayList = std.ArrayList;
const math = std.math; const math = std.math;
const mem = std.mem; const mem = std.mem;
// ShellIndexProto is a function prototype that, given a shell's index (in // ShellIndexFn is a function prototype that, given a shell's index (in
// global shell section), will return a shell string. Matches ShelReader.get. // global shell section), will return a shell string. Matches ShelReader.get.
const ShellIndexProto = fn (u6) []const u8; const ShellIndexFn = fn (u6) []const u8;
// User is a convenient public struct for record construction and // User is a convenient public struct for record construction and
// serialization. Iterator can help retrieve these records. // serialization. Iterator can help retrieve these records.
@ -55,10 +55,6 @@ pub const PackedUser = struct {
return @as(u32, self.name_len) + 1; return @as(u32, self.name_len) + 1;
} }
fn gecosLen(self: *const Inner) usize {
return self.gecos_len;
}
fn gecosStart(self: *const Inner) usize { fn gecosStart(self: *const Inner) usize {
if (self.name_is_a_suffix) { if (self.name_is_a_suffix) {
return self.homeLen(); return self.homeLen();
@ -67,6 +63,10 @@ pub const PackedUser = struct {
} }
} }
fn gecosLen(self: *const Inner) usize {
return self.gecos_len;
}
fn maybeShellStart(self: *const Inner) usize { fn maybeShellStart(self: *const Inner) usize {
assert(self.shell_here); assert(self.shell_here);
return self.gecosStart() + self.gecosLen(); return self.gecosStart() + self.gecosLen();
@ -85,7 +85,6 @@ pub const PackedUser = struct {
if (self.shell_here) { if (self.shell_here) {
result += self.shellLen(); result += self.shellLen();
} }
return result; return result;
} }
}; };
@ -143,7 +142,11 @@ pub const PackedUser = struct {
// packTo packs the User record and copies it to the given byte slice. The // packTo packs the User record and copies it to the given byte slice. The
// slice must have at least maxRecordSize() bytes available. // slice must have at least maxRecordSize() bytes available.
// The slice is passed as a pointer, so it can be mutated. // The slice is passed as a pointer, so it can be mutated.
pub fn packTo(buf: *[]u8, user: User, shellIndexFn: shellIndexFnType) error{InvalidRecord}!void { pub fn packTo(
buf: *[]u8,
user: User,
shellIndexFn: shellIndexFnType,
) error{InvalidRecord}!void {
// function arguments are consts. We need to mutate the underlying // function arguments are consts. We need to mutate the underlying
// slice, so passing it via pointer instead. // slice, so passing it via pointer instead.
const home_len = try downCast(u6, user.home.len - 1); const home_len = try downCast(u6, user.home.len - 1);
@ -236,26 +239,26 @@ pub const PackedUser = struct {
return self.userdata[gecos_pos .. gecos_pos + gecos_len]; return self.userdata[gecos_pos .. gecos_pos + gecos_len];
} }
pub fn shell(self: *const PackedUser, shellIndex: ShellIndexProto) []const u8 { pub fn shell(self: *const PackedUser, idxFn: ShellIndexFn) []const u8 {
if (self.inner.shell_here) { if (self.inner.shell_here) {
const shell_pos = self.inner.maybeShellStart(); const shell_pos = self.inner.maybeShellStart();
const shell_len = self.inner.shellLen(); const shell_len = self.inner.shellLen();
return self.userdata[shell_pos .. shell_pos + shell_len]; return self.userdata[shell_pos .. shell_pos + shell_len];
} }
return shellIndex(self.inner.shell_len_or_idx); return idxFn(self.inner.shell_len_or_idx);
} }
}; };
pub fn userIterator(section: []const u8, shellIndex: ShellIndexProto) Iterator { pub fn userIterator(section: []const u8, idxFn: ShellIndexFn) Iterator {
return Iterator{ return Iterator{
.section = section, .section = section,
.shellIndex = shellIndex, .shellIndex = idxFn,
}; };
} }
pub const Iterator = struct { pub const Iterator = struct {
section: ?[]const u8, section: ?[]const u8,
shellIndex: ShellIndexProto, shellIndex: ShellIndexFn,
pub fn next(it: *Iterator) ?PackedUser { pub fn next(it: *Iterator) ?PackedUser {
if (it.section) |section| { if (it.section) |section| {