From 7d73667250520c3fb0b3b8d3d98c5b5336fd316c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Wed, 22 Jun 2022 21:06:20 +0300 Subject: [PATCH] code style --- src/User.zig | 37 ++++++++++--------------------------- src/unix2db/main.zig | 24 ++++++++---------------- 2 files changed, 18 insertions(+), 43 deletions(-) diff --git a/src/User.zig b/src/User.zig index 56e0e97..372843a 100644 --- a/src/User.zig +++ b/src/User.zig @@ -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, diff --git a/src/unix2db/main.zig b/src/unix2db/main.zig index 08f6018..79a26e7 100644 --- a/src/unix2db/main.zig +++ b/src/unix2db/main.zig @@ -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);