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)
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);
};
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);
};
validate.utf8(name.?) catch return err_ctx.returnf(
"name is invalid utf8: {s}",
.{name.?},
error.InvalidRecord,
);
validate.utf8(gecos.?) catch return err_ctx.returnf(
"gecos is invalid utf8: {s}",
.{gecos.?},
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;
validate.utf8(name.?) catch
return err_ctx.returnf("name is invalid utf8: {s}", .{name.?}, error.InvalidRecord);
validate.utf8(gecos.?) catch
return err_ctx.returnf("gecos is invalid utf8: {s}", .{gecos.?}, 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);
const user = User{
.uid = uid,

View File

@ -66,32 +66,24 @@ fn execute(
const groupFname = result.argFlag("--group") orelse "./group";
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
var errc = ErrCtx{};
var passwdFile = fs.cwd().openFile(
passwdFname,
.{ .mode = .read_only },
) catch |err| return fail(errc.wrapf("open {s}", .{passwdFname}), stderr, err);
var passwdFile = fs.cwd().openFile(passwdFname, .{ .mode = .read_only }) catch |err|
return fail(errc.wrapf("open {s}", .{passwdFname}), stderr, err);
defer passwdFile.close();
var groupFile = fs.cwd().openFile(groupFname, .{ .mode = .read_only }) catch |err|
return fail(errc.wrapf("open {s}", .{groupFname}), stderr, err);
defer groupFile.close();
var users = User.fromReader(
allocator,
&errc,
passwdFile.reader(),
) catch |err| return fail(errc.wrap("read users"), stderr, err);
var users = User.fromReader(allocator, &errc, passwdFile.reader()) catch |err|
return fail(errc.wrap("read users"), stderr, err);
defer for (users) |*user| user.deinit(allocator);
defer allocator.free(users);
var groups = Group.fromReader(
allocator,
groupFile.reader(),
) catch |err| return fail(errc.wrap("read groups"), stderr, err);
var groups = Group.fromReader(allocator, groupFile.reader()) catch |err|
return fail(errc.wrap("read groups"), stderr, err);
defer for (groups) |*group| group.deinit(allocator);
defer allocator.free(groups);