Skip to content

Commit

Permalink
config: extract helper method that inserts multiple layers
Browse files Browse the repository at this point in the history
Default configuration will be split to per-source layers.
  • Loading branch information
yuja committed Dec 7, 2024
1 parent f53602d commit 9a93045
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions lib/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,7 @@ impl StackedConfig {
path: impl AsRef<Path>,
) -> Result<(), ConfigError> {
let layers = ConfigLayer::load_from_dir(source, path.as_ref())?;
let index = self.insert_point(source);
self.layers.splice(index..index, layers);
self.extend_layers(layers);
Ok(())
}

Expand All @@ -379,6 +378,14 @@ impl StackedConfig {
self.layers.insert(index, layer);
}

/// Inserts multiple layers at the positions specified by `layer.source`.
pub fn extend_layers(&mut self, layers: impl IntoIterator<Item = ConfigLayer>) {
for (source, chunk) in &layers.into_iter().chunk_by(|layer| layer.source) {
let index = self.insert_point(source);
self.layers.splice(index..index, chunk);
}
}

/// Removes layers of the specified `source`.
pub fn remove_layers(&mut self, source: ConfigSource) {
self.layers.drain(self.layer_range(source));
Expand Down Expand Up @@ -582,8 +589,26 @@ mod tests {
vec![ConfigSource::EnvBase, ConfigSource::Repo]
);

// Insert multiple
config.extend_layers([
ConfigLayer::with_data(ConfigSource::Repo, empty_data()),
ConfigLayer::with_data(ConfigSource::Repo, empty_data()),
ConfigLayer::with_data(ConfigSource::User, empty_data()),
]);
assert_eq!(
layer_sources(&config),
vec![
ConfigSource::EnvBase,
ConfigSource::User,
ConfigSource::Repo,
ConfigSource::Repo,
ConfigSource::Repo,
]
);

// Remove remainders
config.remove_layers(ConfigSource::EnvBase);
config.remove_layers(ConfigSource::User);
config.remove_layers(ConfigSource::Repo);
assert_eq!(layer_sources(&config), vec![]);
}
Expand Down

0 comments on commit 9a93045

Please sign in to comment.