From ea52c99fee0daf71dde6a02ff223359e25883379 Mon Sep 17 00:00:00 2001 From: torque Date: Fri, 1 Dec 2023 22:33:14 -0800 Subject: [PATCH] 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. --- src/parser.zig | 6 ++---- src/parser/value.zig | 8 ++++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/parser.zig b/src/parser.zig index bbf0a62..01c9420 100644 --- a/src/parser.zig +++ b/src/parser.zig @@ -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 diff --git a/src/parser/value.zig b/src/parser/value.zig index 191b8ce..f1795b4 100644 --- a/src/parser/value.zig +++ b/src/parser/value.zig @@ -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; }