use std.( c, c.write, os.errors, str.(?) ) const STDOUT: i32 = 1 fn print*(msg: &str) unit = unsafe write(STDOUT, as[_](msg.ptr), msg.len) fn print*(msg: str) unit = print(&msg) fn println*() unit = print("\n") fn println*(msg: &str) unit = print("{msg}\n") fn println*(msg: str) unit = println(&msg) type Result*[T] = std.result.Result[T, Error] type Error* { PermissionDenied NotFound Interrupted BadFileNumber AlreadyExists InvalidArgument InvalidData(cause: &str) Other(code: i32) } fn Error.from-os-error*(code: i32) Error = match code { errors.EPERM -> Error.PermissionDenied errors.ENOENT -> Error.NotFound errors.EINTR -> Error.Interrupted errors.EBADF -> Error.BadFileNumber errors.EEXIST -> Error.AlreadyExists errors.EINVAL -> Error.InvalidArgument code -> Error.Other(code) } // TODO: cross-platform errno fn Error.last-os-error*() Error = Error.from-os-error(unsafe (c.__errno_location().0)) fn fmt*(err: &Error, buf: &mut StrBuf) unit = match err { Error.PermissionDenied -> buf.push("Permission denied") Error.NotFound -> buf.push("Not found") Error.Interrupted -> buf.push("Interrupted") Error.BadFileNumber -> buf.push("Bad file number") Error.AlreadyExists -> buf.push("Already exists") Error.InvalidArgument -> buf.push("Invalid argument") Error.InvalidData(cause) -> { buf.push("Invalid data: ") buf.push(cause) } Error.Other(code) -> buf.push("Other({code})") }