parser: default expect enum values with leading .

I prefer this, personally. And this is all about personal preference.
This commit is contained in:
torque 2023-10-22 16:48:45 -07:00
parent ce65dee71f
commit f371aa281c
Signed by: torque
SSH Key Fingerprint: SHA256:nCrXefBNo6EbjNSQhv0nXmEg/VuNq3sMF5b8zETw3Tk
3 changed files with 18 additions and 2 deletions

View File

@ -32,7 +32,7 @@ const source =
\\ # and a trailing newline for good measure \\ # and a trailing newline for good measure
\\ > \\ >
\\tuple: [ no, 127 ] \\tuple: [ no, 127 ]
\\enume: third \\enume: .third
\\taggart: \\taggart:
\\ first: string a thing \\ first: string a thing
\\list: \\list:

View File

@ -93,6 +93,15 @@ pub const Options = struct {
// option to change this. // option to change this.
null_scalars: []const []const u8 = &.{ "null", "nil", "None" }, 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. // Only used by the parseTo family of functions.
// Perform ASCII-case-insensitive comparisons for scalars (i.e. `TRUE` in a document // 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 // will match `true` in the boolean scalars. Unicode case folding is not currently

View File

@ -244,7 +244,14 @@ pub const Value = union(enum) {
switch (self) { switch (self) {
inline .scalar, .string => |str, tag| { inline .scalar, .string => |str, tag| {
if (tag == .string and !options.coerce_strings) return error.BadValue; 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) { if (options.allow_numeric_enums) {
const parsed = std.fmt.parseInt(@typeInfo(T).Enum.tag_type, str, 10) catch const parsed = std.fmt.parseInt(@typeInfo(T).Enum.tag_type, str, 10) catch
return error.BadValue; return error.BadValue;