parser.Options: split truthy/falsy scalars into separate fields

This makes overriding the defaults of just one of truthy or falsy more
ergonomic. Previously, when overriding the truthy scalars, the user
would also have to specify all of the falsy scalars as well.
This commit is contained in:
torque 2023-12-01 22:33:14 -08:00
parent dbf2762982
commit ea52c99fee
Signed by: torque
SSH Key Fingerprint: SHA256:nCrXefBNo6EbjNSQhv0nXmEg/VuNq3sMF5b8zETw3Tk
2 changed files with 6 additions and 8 deletions

View File

@ -80,10 +80,8 @@ pub const Options = struct {
// an error if the destination is a boolean type. By default, these comparisons are
// case-sensitive. See the `case_insensitive_scalar_coersion` option to change
// this.
boolean_scalars: struct { truthy: []const []const u8, falsy: []const []const u8 } = .{
.truthy = &.{ "true", "True", "yes", "on" },
.falsy = &.{ "false", "False", "no", "off" },
},
truthy_boolean_scalars: []const []const u8 = &.{ "true", "True", "yes", "on" },
falsy_boolean_scalars: []const []const u8 = &.{ "false", "False", "no", "off" },
// Only used by the parseTo family of functions.
// A list of strings. Scalars in the doucment that match any of the values listed

View File

@ -82,14 +82,14 @@ pub const Value = union(enum) {
inline .scalar, .string => |str, tag| {
if (tag == .string and !options.coerce_strings) return error.BadValue;
if (options.case_insensitive_scalar_coersion) {
for (options.boolean_scalars.truthy) |check|
for (options.truthy_boolean_scalars) |check|
if (std.ascii.eqlIgnoreCase(str, check)) return true;
for (options.boolean_scalars.falsy) |check|
for (options.falsy_boolean_scalars) |check|
if (std.ascii.eqlIgnoreCase(str, check)) return false;
} else {
for (options.boolean_scalars.truthy) |check|
for (options.truthy_boolean_scalars) |check|
if (std.mem.eql(u8, str, check)) return true;
for (options.boolean_scalars.falsy) |check|
for (options.falsy_boolean_scalars) |check|
if (std.mem.eql(u8, str, check)) return false;
}