From 234e3986f9ec8656dcdffb25caaf17c1a6b93b2c Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 18 Nov 2024 15:00:06 -0800 Subject: [PATCH] config: function to change conditional state --- src/config/Config.zig | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/config/Config.zig b/src/config/Config.zig index 75bf0225b..31199429e 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -2560,6 +2560,36 @@ pub fn loadRecursiveFiles(self: *Config, alloc_gpa: Allocator) !void { } } +/// Change the state of conditionals and reload the configuration +/// based on the new state. This returns a new configuration based +/// on the new state. The caller must free the old configuration if they +/// wish. +/// +/// This doesn't re-read any files, it just re-applies the same +/// configuration with the new conditional state. Importantly, this means +/// that if you change the conditional state and the user in the interim +/// deleted a file that was referenced in the configuration, then the +/// configuration can still be reloaded. +/// TODO: totally untested +pub fn changeConditionalState( + self: *Config, + new: conditional.State, +) !Config { + // Create our new configuration + const alloc_gpa = self._arena.?.child_allocator; + var new_config = try default(alloc_gpa); + errdefer new_config.deinit(); + + // Set our conditional state so the replay below can use it + new_config._conditional_state = new; + + // Replay all of our steps to rebuild the configuration + var it = Replay.iterator(self._replay_steps.items, &new_config); + try new_config.loadIter(alloc_gpa, &it); + + return new_config; +} + /// Expand the relative paths in config-files to be absolute paths /// relative to the base directory. fn expandPaths(self: *Config, base: []const u8) !void {