parser: default expect enum values with leading .
I prefer this, personally. And this is all about personal preference.
This commit is contained in:
parent
ce65dee71f
commit
f371aa281c
@ -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:
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user