From 48e797909354e9760c4efa78944fb25da1015280 Mon Sep 17 00:00:00 2001 From: Jeff Dickey <216188+jdx@users.noreply.github.com> Date: Sun, 13 Oct 2024 16:01:28 -0500 Subject: [PATCH] refactor: clean up `settings set` --- src/cli/settings/set.rs | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/src/cli/settings/set.rs b/src/cli/settings/set.rs index 5ac020216..431c3cbaf 100644 --- a/src/cli/settings/set.rs +++ b/src/cli/settings/set.rs @@ -23,7 +23,7 @@ impl SettingsSet { } } -pub fn set(key: &str, value: &str, add: bool) -> Result<()> { +pub fn set(mut key: &str, value: &str, add: bool) -> Result<()> { let value = if let Some(meta) = SETTINGS_META.get(key) { match meta.type_ { SettingsType::Bool => parse_bool(value)?, @@ -44,36 +44,28 @@ pub fn set(key: &str, value: &str, add: bool) -> Result<()> { if !config.contains_key("settings") { config["settings"] = toml_edit::Item::Table(toml_edit::Table::new()); } - let settings = config["settings"].as_table_mut().unwrap(); + let mut settings = config["settings"].as_table_mut().unwrap(); if key.contains(".") { - let (parent, child) = key.split_once('.').unwrap(); + let (parent_key, child_key) = key.split_once('.').unwrap(); - let parent_table = settings - .entry(parent) + key = child_key; + settings = settings + .entry(parent_key) .or_insert(toml_edit::Item::Table(toml_edit::Table::new())) .as_table_mut() .unwrap(); - let value = match parent_table.get(child).map(|c| c.as_array()) { - Some(Some(array)) if add => { - let mut array = array.clone(); - array.extend(value.as_array().unwrap().iter().cloned()); - array.into() - } - _ => value, - }; - parent_table.insert(child, value.into()); - } else { - let value = match settings.get(key).map(|c| c.as_array()) { - Some(Some(array)) if add => { - let mut array = array.clone(); - array.extend(value.as_array().unwrap().iter().cloned()); - array.into() - } - _ => value, - }; - settings.insert(key, value.into()); } + let value = match settings.get(key).map(|c| c.as_array()) { + Some(Some(array)) if add => { + let mut array = array.clone(); + array.extend(value.as_array().unwrap().iter().cloned()); + array.into() + } + _ => value, + }; + settings.insert(key, value.into()); + // validate let _: SettingsFile = toml::from_str(&config.to_string())?;