add ErrContext

This commit is contained in:
Motiejus Jakštys 2022-06-12 13:27:12 +03:00
parent 12561d9f3a
commit 9b803b84d2
3 changed files with 81 additions and 2 deletions

76
src/ErrContext.zig Normal file
View File

@ -0,0 +1,76 @@
// ErrContext is a way to pass friendly error messages to the user.
// Preallocates memory.
const std = @import("std");
const mem = std.mem;
const math = std.math;
const BoundedArray = std.BoundedArray;
const capacity = 1 << 16; // 64K
const ErrContext = @This();
buf: BoundedArray(u8, capacity) =
BoundedArray(u8, capacity).init(0) catch unreachable,
overflow: bool = false,
pub fn write(self: *ErrContext, bytes: []const u8) error{Overflow}!usize {
const can_add = capacity - self.buf.len;
if (can_add == 0) return error.Overflow;
self.overflow = bytes.len > can_add;
self.buf.appendSliceAssumeCapacity(bytes[0..math.min(bytes.len, can_add)]);
// not adding the final zero is ok, because it needs
// to be ignored in the iterator anyway.
_ = self.buf.append(0) catch null;
if (self.overflow)
return error.Overflow;
return bytes.len;
}
const Writer = std.io.Writer(*ErrContext, error{Overflow}, write);
pub fn writer(self: *ErrContext) Writer {
return Writer{ .context = self };
}
pub fn iterator(self: *const ErrContext) mem.SplitIterator(u8) {
const slice = self.buf.constSlice();
const last_byte = if (slice[slice.len - 1] == '0') slice.len - 1 else slice.len;
return mem.split(u8, slice[0..last_byte], "\x00");
}
const testing = std.testing;
test "basics" {
var ctx = ErrContext{};
var wr = ctx.writer();
try wr.writeAll("0" ** 10);
try wr.writeAll("1" ** 10);
try testing.expectError(error.Overflow, wr.writeAll("3" ** capacity));
try testing.expectError(error.Overflow, wr.writeAll("foo"));
var it = ctx.iterator();
try testing.expectEqualSlices(u8, it.next().?, "0" ** 10);
try testing.expectEqualSlices(u8, it.next().?, "1" ** 10);
const long = it.next().?;
try testing.expectEqual(@as(usize, capacity - 2 - 20), long.len);
try testing.expectEqual(it.next(), null);
try testing.expect(ctx.overflow);
}
test "almost overflow" {
var ctx = ErrContext{};
var wr = ctx.writer();
try wr.writeAll("0" ** (capacity - 2));
try testing.expectError(error.Overflow, wr.writeAll("11"));
var it = ctx.iterator();
try testing.expectEqualSlices(u8, it.next().?, "0" ** (capacity - 2));
try testing.expectEqualSlices(u8, it.next().?, "1");
try testing.expectEqual(it.next(), null);
}

View File

@ -4,6 +4,7 @@ test "turbonss test suite" {
_ = @import("compress.zig"); _ = @import("compress.zig");
_ = @import("Corpus.zig"); _ = @import("Corpus.zig");
_ = @import("DB.zig"); _ = @import("DB.zig");
_ = @import("ErrContext.zig");
_ = @import("Group.zig"); _ = @import("Group.zig");
_ = @import("header.zig"); _ = @import("header.zig");
_ = @import("libnss.zig"); _ = @import("libnss.zig");
@ -11,7 +12,7 @@ test "turbonss test suite" {
_ = @import("PackedUser.zig"); _ = @import("PackedUser.zig");
_ = @import("padding.zig"); _ = @import("padding.zig");
_ = @import("shell.zig"); _ = @import("shell.zig");
_ = @import("unix2db/main.zig");
_ = @import("User.zig"); _ = @import("User.zig");
_ = @import("validate.zig"); _ = @import("validate.zig");
_ = @import("unix2db/main.zig");
} }

View File

@ -114,6 +114,8 @@ test "invalid argument" {
} }
test "smoke test" { test "smoke test" {
if (true) return error.SkipZigTest;
const allocator = testing.allocator; const allocator = testing.allocator;
var stderr = ArrayList(u8).init(allocator); var stderr = ArrayList(u8).init(allocator);
defer stderr.deinit(); defer stderr.deinit();
@ -122,7 +124,7 @@ test "smoke test" {
defer corpus.deinit(); defer corpus.deinit();
var tmp = testing.tmpDir(.{}); var tmp = testing.tmpDir(.{});
//errdefer tmp.cleanup(); errdefer tmp.cleanup();
const tmp_path = blk: { const tmp_path = blk: {
const relative_path = try fs.path.join(allocator, &[_][]const u8{ const relative_path = try fs.path.join(allocator, &[_][]const u8{