From f016c5028c87badec3257afa7dd8cbcf003ae1f1 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 18 Nov 2024 14:48:40 -0800 Subject: [PATCH] config: Replay.Step supports a conditional arg --- src/config/Config.zig | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/config/Config.zig b/src/config/Config.zig index 6e569e795..75bf0225b 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -23,6 +23,8 @@ const internal_os = @import("../os/main.zig"); const cli = @import("../cli.zig"); const Command = @import("../Command.zig"); +const conditional = @import("conditional.zig"); +const Conditional = conditional.Conditional; const formatterpkg = @import("formatter.zig"); const themepkg = @import("theme.zig"); const url = @import("url.zig"); @@ -1743,6 +1745,10 @@ _arena: ?ArenaAllocator = null, /// the configuration. _diagnostics: cli.DiagnosticList = .{}, +/// The conditional truths for the configuration. This is used to +/// determine if a conditional configuration matches or not. +_conditional_state: conditional.State = .{}, + /// The steps we can use to reload the configuration after it has been loaded /// without reopening the files. This is used in very specific cases such /// as loadTheme which has more details on why. @@ -3127,6 +3133,14 @@ const Replay = struct { /// A base path to expand relative paths against. expand: []const u8, + + /// A conditional argument. This arg is parsed only if all + /// conditions match (an "AND"). An "OR" can be achieved by + /// having multiple conditional arg entries. + conditional_arg: struct { + conditions: []const Conditional, + arg: []const u8, + }, }; const Iterator = struct { @@ -3141,7 +3155,6 @@ const Replay = struct { if (self.idx >= self.slice.len) return null; defer self.idx += 1; switch (self.slice[self.idx]) { - .arg => |arg| return arg, .expand => |base| self.config.expandPaths(base) catch |err| { // This shouldn't happen because to reach this step // means that it succeeded before. Its possible since @@ -3150,6 +3163,19 @@ const Replay = struct { // In that really unfortunate case, we log a warning. log.warn("error expanding paths err={}", .{err}); }, + + .arg => |arg| return arg, + + .conditional_arg => |v| conditional: { + // All conditions must match. + for (v.conditions) |cond| { + if (!self.config._conditional_state.match(cond)) { + break :conditional; + } + } + + return v.arg; + }, } } }