| 1 | const std = @import("std"); |
| 2 | const string = []const u8; |
| 3 | const extras = @import("./lib.zig"); |
| 4 | |
| 5 | pub fn stringToEnum(comptime E: type, str: ?string) ?E { |
| 6 | return std.meta.stringToEnum(E, str orelse return null); |
| 7 | } |
| 8 | |
| 9 | test { |
| 10 | try std.testing.expect(stringToEnum(enum { A, B }, "A") == .A); |
| 11 | } |
| 12 | test { |
| 13 | try std.testing.expect(stringToEnum(enum { A, B }, "B") == .B); |
| 14 | } |
| 15 | test { |
| 16 | try std.testing.expect(stringToEnum(enum { A, B }, "C") == null); |
| 17 | } |
| 18 | test { |
| 19 | try std.testing.expect(stringToEnum(enum { A, B }, null) == null); |
| 20 | } |