From db3ad5f45e707a04eaa51aa657995abe43ce967a Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Wed, 29 Jan 2025 15:32:34 +0100 Subject: [PATCH] feat: support matching and extracting enums --- src/cbor.zig | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/cbor.zig b/src/cbor.zig index c922a1d..f20b1f6 100644 --- a/src/cbor.zig +++ b/src/cbor.zig @@ -522,6 +522,21 @@ fn matchFloatValue(comptime T: type, iter: *[]const u8, val: T) Error!bool { return if (try matchFloat(T, iter, &v)) v == val else false; } +pub fn matchEnum(comptime T: type, iter_: *[]const u8, val: *T) Error!bool { + var iter = iter_.*; + var str: []const u8 = undefined; + if (try matchString(&iter, &str)) if (std.meta.stringToEnum(T, str)) |val_| { + val.* = val_; + iter_.* = iter; + return true; + }; + return false; +} + +fn matchEnumValue(comptime T: type, iter: *[]const u8, val: T) Error!bool { + return matchStringValue(iter, @tagName(val)); +} + fn skipString(iter: *[]const u8, minor: u5) Error!void { const len: usize = @intCast(try decodePInt(iter, minor)); if (iter.len < len) @@ -657,6 +672,7 @@ pub fn matchValue(iter: *[]const u8, value: anytype) Error!bool { .Array => |info| if (info.child == u8) matchStringValue(iter, &value) else matchArray(iter, value, info), .Float => return matchFloatValue(T, iter, value), .ComptimeFloat => matchFloatValue(f64, iter, value), + .Enum => matchEnumValue(T, iter, value), else => @compileError("cannot match value type '" ++ @typeName(T) ++ "' to cbor stream"), }; } @@ -855,6 +871,7 @@ fn Extractor(comptime T: type) type { return false; }, .Float => return matchFloat(T, iter, self.dest), + .Enum => return matchEnum(T, iter, self.dest), else => extractError(T), } }