diff --git a/examples/reify.zig b/examples/reify.zig index 29a17a4..cee5213 100644 --- a/examples/reify.zig +++ b/examples/reify.zig @@ -32,7 +32,7 @@ const source = \\ # and a trailing newline for good measure \\ > \\tuple: [ no, 127 ] - \\enume: third + \\enume: .third \\taggart: \\ first: string a thing \\list: diff --git a/src/parser.zig b/src/parser.zig index 394251a..500b42a 100644 --- a/src/parser.zig +++ b/src/parser.zig @@ -93,6 +93,15 @@ pub const Options = struct { // option to change this. null_scalars: []const []const u8 = &.{ "null", "nil", "None" }, + // Only used by the parseTo family of functions. + // Choose whether to strip the leading `.` off of expected enum values. By default, + // `.enum_field` will be parsed into the enum field `enum_field`, which makes them + // look like source code enum literals. Any enum value missing the leading `.` will + // result in a conversion error. If set to false, no preprocessing will be done + // and enum values will be converted from the literal scalar/string. These two styles + // cannot be mixed in a single document. + expect_enum_dot: bool = true, + // Only used by the parseTo family of functions. // Perform ASCII-case-insensitive comparisons for scalars (i.e. `TRUE` in a document // will match `true` in the boolean scalars. Unicode case folding is not currently diff --git a/src/parser/value.zig b/src/parser/value.zig index f169a26..bfbc4e8 100644 --- a/src/parser/value.zig +++ b/src/parser/value.zig @@ -244,7 +244,14 @@ pub const Value = union(enum) { switch (self) { inline .scalar, .string => |str, tag| { if (tag == .string and !options.coerce_strings) return error.BadValue; - if (std.meta.stringToEnum(T, str)) |value| return value; + const name = if (options.expect_enum_dot) blk: { + if (str.len > 0 and str[0] == '.') + break :blk str[1..] + else + return error.BadValue; + } else str; + + if (std.meta.stringToEnum(T, name)) |value| return value; if (options.allow_numeric_enums) { const parsed = std.fmt.parseInt(@typeInfo(T).Enum.tag_type, str, 10) catch return error.BadValue;