Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: boolean value support for packed structs #1930

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/cli/args.zig
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,17 @@ fn parsePackedStruct(comptime T: type, v: []const u8) !T {

var result: T = .{};

// Allow standalone boolean values like "true" and "false" to
// turn on or off all of the struct's fields.
bools: {
const b = parseBool(v) catch break :bools;
inline for (info.fields) |field| {
assert(field.type == bool);
@field(result, field.name) = b;
}
return result;
}

// We split each value by ","
var iter = std.mem.splitSequence(u8, v, ",");
loop: while (iter.next()) |part_raw| {
Expand Down Expand Up @@ -586,6 +597,34 @@ test "parseIntoField: packed struct negation" {
try testing.expect(!data.v.b);
}

test "parseIntoField: packed struct true/false" {
const testing = std.testing;
var arena = ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();

const Field = packed struct {
a: bool = false,
b: bool = true,
};
var data: struct {
v: Field,
} = undefined;

try parseIntoField(@TypeOf(data), alloc, &data, "v", "true");
try testing.expect(data.v.a);
try testing.expect(data.v.b);

try parseIntoField(@TypeOf(data), alloc, &data, "v", "false");
try testing.expect(!data.v.a);
try testing.expect(!data.v.b);

try testing.expectError(
error.InvalidValue,
parseIntoField(@TypeOf(data), alloc, &data, "v", "true,a"),
);
}

test "parseIntoField: packed struct whitespace" {
const testing = std.testing;
var arena = ArenaAllocator.init(testing.allocator);
Expand Down
Loading