feat: add support for matching of non-u8 slices
This commit is contained in:
parent
03ab65ee31
commit
bda7980757
1 changed files with 17 additions and 2 deletions
19
src/cbor.zig
19
src/cbor.zig
|
|
@ -911,7 +911,7 @@ pub fn matchValue(iter: *[]const u8, value: anytype) Error!bool {
|
||||||
.pointer => |info| switch (info.size) {
|
.pointer => |info| switch (info.size) {
|
||||||
.one => matchValue(iter, value.*),
|
.one => matchValue(iter, value.*),
|
||||||
.many, .c => matchError(T),
|
.many, .c => matchError(T),
|
||||||
.slice => if (info.child == u8) matchStringValue(iter, value) else matchArray(iter, value, info),
|
.slice => if (info.child == u8) matchStringValue(iter, value) else matchSlice(iter, value),
|
||||||
},
|
},
|
||||||
.optional => if (value) |v| matchValue(iter, v) else matchNull(iter),
|
.optional => if (value) |v| matchValue(iter, v) else matchNull(iter),
|
||||||
.@"struct" => |info| if (info.is_tuple)
|
.@"struct" => |info| if (info.is_tuple)
|
||||||
|
|
@ -919,7 +919,7 @@ pub fn matchValue(iter: *[]const u8, value: anytype) Error!bool {
|
||||||
// TODO: Add case for matching struct here
|
// TODO: Add case for matching struct here
|
||||||
else
|
else
|
||||||
matchError(T),
|
matchError(T),
|
||||||
.array => |info| if (info.child == u8) matchStringValue(iter, &value) else matchArray(iter, value, info),
|
.array => |info| if (info.child == u8) matchStringValue(iter, &value) else matchSlice(iter, &value),
|
||||||
.float => matchFloatValue(T, iter, value),
|
.float => matchFloatValue(T, iter, value),
|
||||||
.comptime_float => matchFloatValue(f64, iter, value),
|
.comptime_float => matchFloatValue(f64, iter, value),
|
||||||
.@"enum" => matchEnumValue(T, iter, value),
|
.@"enum" => matchEnumValue(T, iter, value),
|
||||||
|
|
@ -1030,6 +1030,21 @@ fn matchArray(iter_: *[]const u8, arr: anytype, info: anytype) Error!bool {
|
||||||
return n == 0;
|
return n == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn matchSlice(iter_: *[]const u8, arr: anytype) Error!bool {
|
||||||
|
var iter = iter_.*;
|
||||||
|
const n = decodeArrayHeader(&iter) catch |e| switch (e) {
|
||||||
|
error.InvalidArrayType => return false,
|
||||||
|
error.InvalidPIntType => return e,
|
||||||
|
error.TooShort => return e,
|
||||||
|
};
|
||||||
|
if (n != arr.len) return false;
|
||||||
|
for (arr) |elem| {
|
||||||
|
if (!try matchValue(&iter, elem)) return false;
|
||||||
|
}
|
||||||
|
iter_.* = iter;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
fn matchArrayScalar(iter: *[]const u8, arr: anytype) Error!bool {
|
fn matchArrayScalar(iter: *[]const u8, arr: anytype) Error!bool {
|
||||||
var i: usize = 0;
|
var i: usize = 0;
|
||||||
var n = try decodeArrayHeader(iter);
|
var n = try decodeArrayHeader(iter);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue