1
Fork 0

code style

main
Motiejus Jakštys 2022-06-22 21:06:20 +03:00
parent a4d5be6fab
commit 7d73667250
2 changed files with 18 additions and 43 deletions

View File

@ -66,37 +66,20 @@ fn fromLine(allocator: Allocator, err_ctx: *ErrCtx, line: []const u8) error{ Inv
if (it.next() != null) if (it.next() != null)
return error.InvalidRecord; return error.InvalidRecord;
const uid = fmt.parseInt(u32, uids.?, 10) catch { const uid = fmt.parseInt(u32, uids.?, 10) catch
return err_ctx.returnf("bad uid: {s}", .{uids.?}, error.InvalidRecord); return err_ctx.returnf("bad uid: {s}", .{uids.?}, error.InvalidRecord);
};
const gid = fmt.parseInt(u32, gids.?, 10) catch { const gid = fmt.parseInt(u32, gids.?, 10) catch
return err_ctx.returnf("bad uid: {s}", .{gids.?}, error.InvalidRecord); return err_ctx.returnf("bad uid: {s}", .{gids.?}, error.InvalidRecord);
};
validate.utf8(name.?) catch return err_ctx.returnf( validate.utf8(name.?) catch
"name is invalid utf8: {s}", return err_ctx.returnf("name is invalid utf8: {s}", .{name.?}, error.InvalidRecord);
.{name.?}, validate.utf8(gecos.?) catch
error.InvalidRecord, return err_ctx.returnf("gecos is invalid utf8: {s}", .{gecos.?}, error.InvalidRecord);
); validate.utf8(home.?) catch
validate.utf8(gecos.?) catch return err_ctx.returnf( return err_ctx.returnf("home is invalid utf8: {s}", .{home.?}, error.InvalidRecord);
"gecos is invalid utf8: {s}", validate.utf8(shell.?) catch
.{gecos.?}, return err_ctx.returnf("shell is invalid utf8: {s}", .{shell.?}, error.InvalidRecord);
error.InvalidRecord,
);
validate.utf8(home.?) catch return err_ctx.returnf(
"home is invalid utf8: {s}",
.{home.?},
error.InvalidRecord,
);
validate.utf8(shell.?) catch return err_ctx.returnf(
"shell is invalid utf8: {s}",
.{shell.?},
error.InvalidRecord,
);
if (err_ctx.dirty)
return error.InvalidRecord;
const user = User{ const user = User{
.uid = uid, .uid = uid,

View File

@ -66,32 +66,24 @@ fn execute(
const groupFname = result.argFlag("--group") orelse "./group"; const groupFname = result.argFlag("--group") orelse "./group";
const outFile = result.argFlag("--output") orelse "./db.turbo"; const outFile = result.argFlag("--output") orelse "./db.turbo";
// to catch a specific file.OpenError, hold thumbs for // to catch an error set file.OpenError, wait for
// https://github.com/ziglang/zig/issues/2473 // https://github.com/ziglang/zig/issues/2473
var errc = ErrCtx{}; var errc = ErrCtx{};
var passwdFile = fs.cwd().openFile( var passwdFile = fs.cwd().openFile(passwdFname, .{ .mode = .read_only }) catch |err|
passwdFname, return fail(errc.wrapf("open {s}", .{passwdFname}), stderr, err);
.{ .mode = .read_only },
) catch |err| return fail(errc.wrapf("open {s}", .{passwdFname}), stderr, err);
defer passwdFile.close(); defer passwdFile.close();
var groupFile = fs.cwd().openFile(groupFname, .{ .mode = .read_only }) catch |err| var groupFile = fs.cwd().openFile(groupFname, .{ .mode = .read_only }) catch |err|
return fail(errc.wrapf("open {s}", .{groupFname}), stderr, err); return fail(errc.wrapf("open {s}", .{groupFname}), stderr, err);
defer groupFile.close(); defer groupFile.close();
var users = User.fromReader( var users = User.fromReader(allocator, &errc, passwdFile.reader()) catch |err|
allocator, return fail(errc.wrap("read users"), stderr, err);
&errc,
passwdFile.reader(),
) catch |err| return fail(errc.wrap("read users"), stderr, err);
defer for (users) |*user| user.deinit(allocator); defer for (users) |*user| user.deinit(allocator);
defer allocator.free(users); defer allocator.free(users);
var groups = Group.fromReader( var groups = Group.fromReader(allocator, groupFile.reader()) catch |err|
allocator, return fail(errc.wrap("read groups"), stderr, err);
groupFile.reader(),
) catch |err| return fail(errc.wrap("read groups"), stderr, err);
defer for (groups) |*group| group.deinit(allocator); defer for (groups) |*group| group.deinit(allocator);
defer allocator.free(groups); defer allocator.free(groups);