lib+test: unify errors to Hare named-void tagged-union
Five tags from ref/hare/errors/common.ha — invalid, noaccess, noentry, exists, unsupported — all !void. lib/io keeps eof/closed as plain void (no Hare analogue for ww's singleton-style done) and adds underread. Drops errors.equal/isnil and the old str-sentinel surface.
This commit is contained in:
@@ -1,30 +1,25 @@
|
||||
// errors — error type (a string) and a few sentinels. Plan 9 model:
|
||||
// the empty string means OK, a non-empty string is the message.
|
||||
// errors — domain-agnostic error types. Mirrors ref/hare/errors/.
|
||||
//
|
||||
// Named-void tagged-union variants, so `(T | errors.invalid | ...)`
|
||||
// composes with every other module's error surface at the tag level.
|
||||
// Per-domain errors (io.eof, io.closed, io.underread, strconv.overflow,
|
||||
// ...) live in their own modules; this module ships only the generic
|
||||
// conditions Hare's errors:: exports.
|
||||
//
|
||||
// Subset shipped today; add more from Hare's errors/common.ha as callers
|
||||
// need them.
|
||||
|
||||
type error = str;
|
||||
// A function was called with an invalid combination of arguments.
|
||||
export type invalid = !void;
|
||||
|
||||
def eof: error = "eof";
|
||||
def underread: error = "short read";
|
||||
def underwrite: error = "short write";
|
||||
def closed: error = "closed";
|
||||
def invalid: error = "invalid argument";
|
||||
def noaccess: error = "permission denied";
|
||||
def noentry: error = "not found";
|
||||
def exists: error = "already exists";
|
||||
// The user does not have permission to use this resource.
|
||||
export type noaccess = !void;
|
||||
|
||||
export fn isnil(e: error) bool = {
|
||||
return e.len == 0;
|
||||
};
|
||||
// An entry was requested which does not exist.
|
||||
export type noentry = !void;
|
||||
|
||||
// equal — compare an error against a sentinel (or any other error).
|
||||
// Pure byte equality. (Was `errors.is` before `is` became a keyword
|
||||
// for tagged-union type-tests; rename matches bytes.equal / strings.equal.)
|
||||
export fn equal(e: error, want: error) bool = {
|
||||
if (e.len != want.len) { return false; };
|
||||
let i: i32 = 0;
|
||||
for (i < e.len) {
|
||||
if (e[i] != want[i]) { return false; };
|
||||
i += 1;
|
||||
};
|
||||
return true;
|
||||
};
|
||||
// An attempt was made to create a resource which already exists.
|
||||
export type exists = !void;
|
||||
|
||||
// The requested operation is not supported.
|
||||
export type unsupported = !void;
|
||||
|
||||
Reference in New Issue
Block a user