Provide iter type hint to cli.args.parse

This commit is contained in:
Wilmer Paulino
2024-08-03 11:53:10 -07:00
parent 6d7f2a4d2a
commit 6dff92472a
18 changed files with 34 additions and 32 deletions

View File

@ -70,7 +70,7 @@ pub fn main() !void {
{
var iter = try std.process.argsWithAllocator(alloc);
defer iter.deinit();
try cli.args.parse(Args, alloc, &args, &iter);
try cli.args.parse(Args, @TypeOf(iter), alloc, &args, &iter);
}
const reader = std.io.getStdIn().reader();

View File

@ -62,7 +62,7 @@ pub fn main() !void {
{
var iter = try std.process.argsWithAllocator(alloc);
defer iter.deinit();
try cli.args.parse(Args, alloc, &args, &iter);
try cli.args.parse(Args, @TypeOf(iter), alloc, &args, &iter);
}
const reader = std.io.getStdIn().reader();

View File

@ -47,7 +47,7 @@ pub fn main() !void {
{
var iter = try std.process.argsWithAllocator(alloc);
defer iter.deinit();
try cli.args.parse(Args, alloc, &args, &iter);
try cli.args.parse(Args, @TypeOf(iter), alloc, &args, &iter);
}
// Handle the modes that do not depend on terminal state first.

View File

@ -29,7 +29,7 @@ pub fn main() !void {
errdefer args.deinit();
var iter = try std.process.argsWithAllocator(alloc);
defer iter.deinit();
try cli.args.parse(Args, alloc, &args, &iter);
try cli.args.parse(Args, @TypeOf(iter), alloc, &args, &iter);
break :args args;
};
defer args.deinit();

View File

@ -86,7 +86,7 @@ pub fn main() !void {
{
var iter = try std.process.argsWithAllocator(alloc);
defer iter.deinit();
try cli.args.parse(Args, alloc, &args, &iter);
try cli.args.parse(Args, @TypeOf(iter), alloc, &args, &iter);
}
const reader = std.io.getStdIn().reader();

View File

@ -38,9 +38,10 @@ pub const Error = error{
///
/// Note: If the arena is already non-null, then it will be used. In this
/// case, in the case of an error some memory might be leaked into the arena.
pub fn parse(comptime T: type, alloc: Allocator, dst: *T, iter: anytype) !void {
pub fn parse(comptime T: type, comptime Iter: type, alloc: Allocator, dst: *T, iter: *Iter) !void {
const info = @typeInfo(T);
assert(info == .Struct);
assert(@typeInfo(Iter) == .Struct);
// Make an arena for all our allocations if we support it. Otherwise,
// use an allocator that always fails. If the arena is already set on
@ -357,7 +358,7 @@ test "parse: simple" {
"--a=42 --b --b-f=false",
);
defer iter.deinit();
try parse(@TypeOf(data), testing.allocator, &data, &iter);
try parse(@TypeOf(data), @TypeOf(iter), testing.allocator, &data, &iter);
try testing.expect(data._arena != null);
try testing.expectEqualStrings("42", data.a);
try testing.expect(data.b);
@ -369,7 +370,7 @@ test "parse: simple" {
"--a=84",
);
defer iter2.deinit();
try parse(@TypeOf(data), testing.allocator, &data, &iter2);
try parse(@TypeOf(data), @TypeOf(iter), testing.allocator, &data, &iter2);
try testing.expect(data._arena != null);
try testing.expectEqualStrings("84", data.a);
try testing.expect(data.b);
@ -391,7 +392,7 @@ test "parse: quoted value" {
"--a=\"42\" --b=\"hello!\"",
);
defer iter.deinit();
try parse(@TypeOf(data), testing.allocator, &data, &iter);
try parse(@TypeOf(data), @TypeOf(iter), testing.allocator, &data, &iter);
try testing.expectEqual(@as(u8, 42), data.a);
try testing.expectEqualStrings("hello!", data.b);
}
@ -411,7 +412,7 @@ test "parse: empty value resets to default" {
"--a= --b=",
);
defer iter.deinit();
try parse(@TypeOf(data), testing.allocator, &data, &iter);
try parse(@TypeOf(data), @TypeOf(iter), testing.allocator, &data, &iter);
try testing.expectEqual(@as(u8, 42), data.a);
try testing.expect(!data.b);
}
@ -433,7 +434,7 @@ test "parse: error tracking" {
"--what --a=42",
);
defer iter.deinit();
try parse(@TypeOf(data), testing.allocator, &data, &iter);
try parse(@TypeOf(data), @TypeOf(iter), testing.allocator, &data, &iter);
try testing.expect(data._arena != null);
try testing.expectEqualStrings("42", data.a);
try testing.expect(!data._errors.empty());

View File

@ -35,7 +35,7 @@ pub fn run(alloc_gpa: Allocator) !u8 {
{
var iter = try std.process.argsWithAllocator(alloc);
defer iter.deinit();
try args.parse(Options, alloc, &opts, &iter);
try args.parse(Options, @TypeOf(iter), alloc, &opts, &iter);
}
const crash_dir = try crash.defaultDir(alloc);

View File

@ -25,7 +25,7 @@ pub fn run(alloc: Allocator) !u8 {
{
var iter = try std.process.argsWithAllocator(alloc);
defer iter.deinit();
try args.parse(Options, alloc, &opts, &iter);
try args.parse(Options, @TypeOf(iter), alloc, &opts, &iter);
}
const stdout = std.io.getStdOut().writer();

View File

@ -31,7 +31,7 @@ pub fn run(alloc: Allocator) !u8 {
{
var iter = try std.process.argsWithAllocator(alloc);
defer iter.deinit();
try args.parse(Options, alloc, &opts, &iter);
try args.parse(Options, @TypeOf(iter), alloc, &opts, &iter);
}
const stdout = std.io.getStdOut().writer();

View File

@ -24,7 +24,7 @@ pub fn run(alloc: std.mem.Allocator) !u8 {
{
var iter = try std.process.argsWithAllocator(alloc);
defer iter.deinit();
try args.parse(Options, alloc, &opts, &iter);
try args.parse(Options, @TypeOf(iter), alloc, &opts, &iter);
}
const stdout = std.io.getStdOut().writer();

View File

@ -55,13 +55,13 @@ pub const Config = struct {
pub fn run(alloc: Allocator) !u8 {
var iter = try std.process.argsWithAllocator(alloc);
defer iter.deinit();
return try runArgs(alloc, &iter);
return try runArgs(@TypeOf(iter), alloc, &iter);
}
fn runArgs(alloc_gpa: Allocator, argsIter: anytype) !u8 {
fn runArgs(comptime Iter: type, alloc_gpa: Allocator, argsIter: *Iter) !u8 {
var config: Config = .{};
defer config.deinit();
try args.parse(Config, alloc_gpa, &config, argsIter);
try args.parse(Config, Iter, alloc_gpa, &config, argsIter);
// Use an arena for all our memory allocs
var arena = ArenaAllocator.init(alloc_gpa);

View File

@ -54,7 +54,7 @@ pub fn run(alloc: Allocator) !u8 {
{
var iter = try std.process.argsWithAllocator(alloc);
defer iter.deinit();
try args.parse(Options, alloc, &opts, &iter);
try args.parse(Options, @TypeOf(iter), alloc, &opts, &iter);
}
var config = if (opts.default) try Config.default(alloc) else try Config.load(alloc);

View File

@ -56,7 +56,7 @@ pub fn run(gpa_alloc: std.mem.Allocator) !u8 {
{
var iter = try std.process.argsWithAllocator(gpa_alloc);
defer iter.deinit();
try args.parse(Options, gpa_alloc, &opts, &iter);
try args.parse(Options, @TypeOf(iter), gpa_alloc, &opts, &iter);
}
var arena = std.heap.ArenaAllocator.init(gpa_alloc);

View File

@ -62,7 +62,7 @@ pub fn run(alloc: Allocator) !u8 {
{
var iter = try std.process.argsWithAllocator(alloc);
defer iter.deinit();
try args.parse(Options, alloc, &opts, &iter);
try args.parse(Options, @TypeOf(iter), alloc, &opts, &iter);
}
var config = if (opts.default) try Config.default(alloc) else try Config.load(alloc);

View File

@ -34,7 +34,7 @@ pub fn run(alloc: std.mem.Allocator) !u8 {
{
var iter = try std.process.argsWithAllocator(alloc);
defer iter.deinit();
try args.parse(Options, alloc, &opts, &iter);
try args.parse(Options, @TypeOf(iter), alloc, &opts, &iter);
}
const stdout = std.io.getStdOut().writer();

View File

@ -54,7 +54,7 @@ export fn ghostty_config_load_string(
fn config_load_string_(self: *Config, str: []const u8) !void {
var fbs = std.io.fixedBufferStream(str);
var iter = cli.args.lineIterator(fbs.reader());
try cli.args.parse(Config, global.alloc, self, &iter);
try cli.args.parse(Config, @TypeOf(iter), global.alloc, self, &iter);
}
/// Load the configuration from the default file locations. This

View File

@ -2058,10 +2058,11 @@ pub fn default(alloc_gpa: Allocator) Allocator.Error!Config {
/// command-line arguments, i.e. `--key=value`.
pub fn loadIter(
self: *Config,
comptime Iter: type,
alloc: Allocator,
iter: anytype,
iter: *Iter,
) !void {
try cli.args.parse(Config, alloc, self, iter);
try cli.args.parse(Config, Iter, alloc, self, iter);
}
/// Load configuration from the target config file at `path`.
@ -2077,7 +2078,7 @@ pub fn loadFile(self: *Config, alloc: Allocator, path: []const u8) !void {
var buf_reader = std.io.bufferedReader(file.reader());
var iter = cli.args.lineIterator(buf_reader.reader());
try self.loadIter(alloc, &iter);
try self.loadIter(@TypeOf(iter), alloc, &iter);
try self.expandPaths(std.fs.path.dirname(path).?);
}
@ -2178,7 +2179,7 @@ pub fn loadCliArgs(self: *Config, alloc_gpa: Allocator) !void {
// Parse the config from the CLI args
var iter = try std.process.argsWithAllocator(alloc_gpa);
defer iter.deinit();
try self.loadIter(alloc_gpa, &iter);
try self.loadIter(@TypeOf(iter), alloc_gpa, &iter);
// If we are not loading the default files, then we need to
// replay the steps up to this point so that we can rebuild
@ -2193,7 +2194,7 @@ pub fn loadCliArgs(self: *Config, alloc_gpa: Allocator) !void {
self._replay_steps.items[replay_len_start..replay_len_end],
&new_config,
);
try new_config.loadIter(alloc_gpa, &it);
try new_config.loadIter(@TypeOf(it), alloc_gpa, &it);
self.deinit();
self.* = new_config;
} else {
@ -2271,7 +2272,7 @@ pub fn loadRecursiveFiles(self: *Config, alloc_gpa: Allocator) !void {
log.info("loading config-file path={s}", .{path});
var buf_reader = std.io.bufferedReader(file.reader());
var iter = cli.args.lineIterator(buf_reader.reader());
try self.loadIter(alloc_gpa, &iter);
try self.loadIter(@TypeOf(iter), alloc_gpa, &iter);
try self.expandPaths(std.fs.path.dirname(path).?);
}
}
@ -2332,12 +2333,12 @@ fn loadTheme(self: *Config, theme: []const u8) !void {
// Load our theme
var buf_reader = std.io.bufferedReader(file.reader());
var iter = cli.args.lineIterator(buf_reader.reader());
try new_config.loadIter(alloc_gpa, &iter);
try new_config.loadIter(@TypeOf(iter), alloc_gpa, &iter);
// Replay our previous inputs so that we can override values
// from the theme.
var slice_it = Replay.iterator(self._replay_steps.items[0..replay_len], &new_config);
try new_config.loadIter(alloc_gpa, &slice_it);
try new_config.loadIter(@TypeOf(slice_it), alloc_gpa, &slice_it);
// Success, swap our new config in and free the old.
self.deinit();

View File

@ -44,7 +44,7 @@ export fn config_load_string(
fn config_load_string_(self: *Config, str: []const u8) !void {
var fbs = std.io.fixedBufferStream(str);
var iter = cli.args.lineIterator(fbs.reader());
try cli.args.parse(Config, alloc, self, &iter);
try cli.args.parse(Config, @TypeOf(iter), alloc, self, &iter);
}
export fn config_finalize(self: *Config) void {