aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHimbeer <himbeer@disroot.org>2024-07-25 11:24:33 +0200
committerHimbeer <himbeer@disroot.org>2024-07-25 11:25:30 +0200
commit397153c8db86e91b10f21318ca8eef29b59fd7f7 (patch)
treee8d66694ec7cdbccc19af0069068c4a509dae5c4
parentad3da793235643a46d019ebc77b9a085c67a6b81 (diff)
sysexchange: Handle invalid and user-provided Status codes
Fixes #52.
-rw-r--r--src/lib/sysexchange.zig17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/lib/sysexchange.zig b/src/lib/sysexchange.zig
index d6405b9..b8cd62d 100644
--- a/src/lib/sysexchange.zig
+++ b/src/lib/sysexchange.zig
@@ -6,6 +6,10 @@ const std = @import("std");
const trap = @import("trap.zig");
+pub const UserError = error{
+ UserError,
+};
+
pub const Status = enum(usize) {
success = 0,
hart_id_out_of_range,
@@ -70,7 +74,12 @@ pub const Status = enum(usize) {
is_a_container,
provide_not_supported,
remove_not_supported,
+ user_error = user_range_start,
unknown = std.math.maxInt(usize),
+ _,
+
+ pub const user_range_start = 0xffff0000_00000000;
+ pub const user_range_end = 0xffffffff_00000000;
pub fn fromError(err: anyerror) Status {
return switch (err) {
@@ -136,6 +145,7 @@ pub const Status = enum(usize) {
error.IsAContainer => .is_a_container,
error.ProvideNotSupported => .provide_not_supported,
error.RemoveNotSupported => .remove_not_supported,
+ error.UserError => .user_error,
else => .unknown,
};
}
@@ -207,9 +217,16 @@ pub const Status = enum(usize) {
.is_a_container => error.IsAContainer,
.provide_not_supported => error.ProvideNotSupported,
.remove_not_supported => error.RemoveNotSupported,
+ .user_error => error.UserError,
.unknown => error.Unknown,
+ _ => if (self.isUserError()) error.UserError else error.Unknown,
};
}
+
+ pub inline fn isUserError(self: Status) bool {
+ const status = @intFromEnum(self);
+ return status > user_range_start and status < user_range_end;
+ }
};
pub fn Result(comptime T: type) type {