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:
@@ -40,8 +40,9 @@ export fn readbyte(b: *buf) (i32 | void) = {
|
||||
|
||||
// Distinct alias so `(str | linerr)` has two variant types the
|
||||
// tagged-union machinery can keep apart at the tag level. The error
|
||||
// variant carries a short description; callers compare with errors.equal
|
||||
// or just inspect by length.
|
||||
// variant carries a short description; callers inspect by length.
|
||||
// Placeholder until bufio graduates to io.stream + named-void tags
|
||||
// (eof / closed / underread).
|
||||
type linerr = str;
|
||||
|
||||
// readline — Hare-style fallible line read (name transliterated from
|
||||
|
||||
@@ -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;
|
||||
|
||||
15
lib/io/io.ww
15
lib/io/io.ww
@@ -5,14 +5,23 @@
|
||||
// value of read/write/close — Hare-shaped tagged unions instead of
|
||||
// errno-style integer sentinels.
|
||||
|
||||
// eof — read past the end of the stream. NAMED-void so it's a
|
||||
// distinct variant tag from `void` (which would be "no result yet").
|
||||
// eof — read past the end of the stream. Hare uses the `done`
|
||||
// singleton for EOF; ww doesn't have `done` yet so we ship a
|
||||
// named-void variant tag.
|
||||
export type eof = void;
|
||||
|
||||
// closed — operation attempted on a stream that has already been
|
||||
// closed. NAMED-void; same shape, different tag.
|
||||
// closed. ww-specific: Hare's io collapses this into the wider
|
||||
// errors union, but our stream vtable has no handle-ownership
|
||||
// semantics, so a distinct tag is honest. Named void.
|
||||
export type closed = void;
|
||||
|
||||
// underread — an I/O handle hit eof partway through a fixed-size
|
||||
// read. Payload is the byte count actually delivered. Mirrors
|
||||
// Hare's `io::underread = !size`; ww uses i32 because the
|
||||
// underlying buffer-length type is i32 today.
|
||||
export type underread = !i32;
|
||||
|
||||
export type stream = struct {
|
||||
ctx: *void,
|
||||
read: fn(s: *stream, buf: []u8) (i32 | eof | closed),
|
||||
|
||||
Reference in New Issue
Block a user