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:
parent
dbf2762982
commit
ea52c99fee
@ -80,10 +80,8 @@ pub const Options = struct {
|
|||||||
// an error if the destination is a boolean type. By default, these comparisons are
|
// 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
|
// case-sensitive. See the `case_insensitive_scalar_coersion` option to change
|
||||||
// this.
|
// this.
|
||||||
boolean_scalars: struct { truthy: []const []const u8, falsy: []const []const u8 } = .{
|
truthy_boolean_scalars: []const []const u8 = &.{ "true", "True", "yes", "on" },
|
||||||
.truthy = &.{ "true", "True", "yes", "on" },
|
falsy_boolean_scalars: []const []const u8 = &.{ "false", "False", "no", "off" },
|
||||||
.falsy = &.{ "false", "False", "no", "off" },
|
|
||||||
},
|
|
||||||
|
|
||||||
// Only used by the parseTo family of functions.
|
// Only used by the parseTo family of functions.
|
||||||
// A list of strings. Scalars in the doucment that match any of the values listed
|
// A list of strings. Scalars in the doucment that match any of the values listed
|
||||||
|
@ -82,14 +82,14 @@ pub const Value = union(enum) {
|
|||||||
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 (options.case_insensitive_scalar_coersion) {
|
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;
|
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;
|
if (std.ascii.eqlIgnoreCase(str, check)) return false;
|
||||||
} else {
|
} else {
|
||||||
for (options.boolean_scalars.truthy) |check|
|
for (options.truthy_boolean_scalars) |check|
|
||||||
if (std.mem.eql(u8, str, check)) return true;
|
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;
|
if (std.mem.eql(u8, str, check)) return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user