feat: more explicit error handling

This commit is contained in:
CJ van den Berg 2025-03-22 21:43:58 +01:00
parent 37021a266b
commit 32505ed465
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -16,6 +16,10 @@ pub const Error = error{
InvalidType, InvalidType,
TooShort, TooShort,
OutOfMemory, OutOfMemory,
InvalidFloatType,
InvalidArrayType,
InvalidPIntType,
JsonIncompatibleType,
}; };
pub const JsonEncodeError = (Error || error{ pub const JsonEncodeError = (Error || error{
@ -310,7 +314,7 @@ pub fn decodeType(iter: *[]const u8) error{TooShort}!CborType {
return .{ .type = type_, .minor = bits.minor, .major = bits.major }; return .{ .type = type_, .minor = bits.minor, .major = bits.major };
} }
fn decodeUIntLengthRecurse(iter: *[]const u8, length: usize, acc: u64) !u64 { fn decodeUIntLengthRecurse(iter: *[]const u8, length: usize, acc: u64) error{TooShort}!u64 {
if (iter.len < 1) if (iter.len < 1)
return error.TooShort; return error.TooShort;
const v: u8 = iter.*[0]; const v: u8 = iter.*[0];
@ -327,14 +331,14 @@ fn decodeUIntLength(iter: *[]const u8, length: usize) !u64 {
return decodeUIntLengthRecurse(iter, length, 0); return decodeUIntLengthRecurse(iter, length, 0);
} }
fn decodePInt(iter: *[]const u8, minor: u5) !u64 { fn decodePInt(iter: *[]const u8, minor: u5) error{ TooShort, InvalidPIntType }!u64 {
if (minor < 24) return minor; if (minor < 24) return minor;
return switch (minor) { return switch (minor) {
24 => decodeUIntLength(iter, 1), // 1 byte 24 => decodeUIntLength(iter, 1), // 1 byte
25 => decodeUIntLength(iter, 2), // 2 byte 25 => decodeUIntLength(iter, 2), // 2 byte
26 => decodeUIntLength(iter, 4), // 4 byte 26 => decodeUIntLength(iter, 4), // 4 byte
27 => decodeUIntLength(iter, 8), // 8 byte 27 => decodeUIntLength(iter, 8), // 8 byte
else => error.InvalidType, else => error.InvalidPIntType,
}; };
} }
@ -347,16 +351,16 @@ pub fn decodeMapHeader(iter: *[]const u8) Error!usize {
if (t.type == cbor_magic_null) if (t.type == cbor_magic_null)
return 0; return 0;
if (t.major != 5) if (t.major != 5)
return error.InvalidType; return error.InvalidMapType;
return @intCast(try decodePInt(iter, t.minor)); return @intCast(try decodePInt(iter, t.minor));
} }
pub fn decodeArrayHeader(iter: *[]const u8) Error!usize { pub fn decodeArrayHeader(iter: *[]const u8) error{ TooShort, InvalidArrayType, InvalidPIntType }!usize {
const t = try decodeType(iter); const t = try decodeType(iter);
if (t.type == cbor_magic_null) if (t.type == cbor_magic_null)
return 0; return 0;
if (t.major != 4) if (t.major != 4)
return error.InvalidType; return error.InvalidArrayType;
return @intCast(try decodePInt(iter, t.minor)); return @intCast(try decodePInt(iter, t.minor));
} }
@ -461,7 +465,7 @@ fn decodeFloat(comptime T: type, iter_: *[]const u8, t: CborType) Error!T {
v = @floatCast(f); v = @floatCast(f);
iter = iter[8..]; iter = iter[8..];
}, },
else => return error.InvalidType, else => return error.InvalidFloatType,
} }
iter_.* = iter; iter_.* = iter;
return v; return v;
@ -786,7 +790,7 @@ fn matchJsonObject(iter_: *[]const u8, obj: *json.ObjectMap) !bool {
if (t.type == cbor_magic_null) if (t.type == cbor_magic_null)
return true; return true;
if (t.major != 5) if (t.major != 5)
return error.InvalidType; return error.NotAnObject;
const ret = try decodeJsonObject(&iter, t.minor, obj); const ret = try decodeJsonObject(&iter, t.minor, obj);
if (ret) iter_.* = iter; if (ret) iter_.* = iter;
return ret; return ret;
@ -972,7 +976,7 @@ pub fn JsonStreamWriter(comptime Writer: type) type {
3 => w.write(try decodeString(iter, t.minor)), // string 3 => w.write(try decodeString(iter, t.minor)), // string
4 => jsonWriteArray(w, iter, t.minor), // array 4 => jsonWriteArray(w, iter, t.minor), // array
5 => jsonWriteMap(w, iter, t.minor), // map 5 => jsonWriteMap(w, iter, t.minor), // map
else => error.InvalidType, else => error.JsonIncompatibleType,
}; };
} }
}; };