From 4dd206a5a3a32e23e05c0842ce4db7a108de4d5f Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Thu, 14 Aug 2025 17:50:42 +0200 Subject: Refactor shared eval function parameters into a CommandContext struct. --- src/cmd/lib/types.zig | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'src/cmd/lib') diff --git a/src/cmd/lib/types.zig b/src/cmd/lib/types.zig index 1f0e162..2d96295 100644 --- a/src/cmd/lib/types.zig +++ b/src/cmd/lib/types.zig @@ -61,3 +61,34 @@ pub const InputSource = struct { } } }; + +pub const CommandContext = struct { + allocator: Allocator, + output_capture: ?*OutputCapture, + input_source: ?*InputSource, + + // The real type is: + // + // const ExecuteCommandFn = *const fn (Command, Allocator, ?*OutputCapture, ?*InputSource) anyerror!CommandStatus; + // + // But Command is defined in ../cmd.zig, so we can't write it. + execute_command: ?*const anyopaque, // Will be cast to the appropriate function type when used + + pub fn init(allocator: Allocator, output_capture: ?*OutputCapture, input_source: ?*InputSource) CommandContext { + return CommandContext{ + .allocator = allocator, + .output_capture = output_capture, + .input_source = input_source, + .execute_command = null, + }; + } + + pub fn with_executor(allocator: Allocator, output_capture: ?*OutputCapture, input_source: ?*InputSource, execute_command: anytype) CommandContext { + return CommandContext{ + .allocator = allocator, + .output_capture = output_capture, + .input_source = input_source, + .execute_command = @ptrCast(&execute_command), + }; + } +}; -- cgit v1.2.1