Skip to content

Commit

Permalink
fix: panic when setting config value
Browse files Browse the repository at this point in the history
  • Loading branch information
roele committed Dec 26, 2024
1 parent 1fae2b0 commit 9bb6c5c
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/cli/config/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,27 @@ impl ConfigSet {
let mut config: toml_edit::DocumentMut = std::fs::read_to_string(&file)?.parse()?;
let mut container = config.as_item_mut();
let parts = self.key.split('.').collect::<Vec<&str>>();
for key in parts.iter().take(parts.len() - 1) {
container = container.as_table_mut().unwrap().entry(key).or_insert({
let mut t = toml_edit::Table::new();
t.set_implicit(true);
toml_edit::Item::Table(t)
});
}
let last_key = parts.last().unwrap();
for (idx, key) in parts.iter().take(parts.len() - 1).enumerate() {
container = container
.as_table_like_mut()
.unwrap()
.entry(key)
.or_insert({
let mut t = toml_edit::Table::new();
t.set_implicit(true);
toml_edit::Item::Table(t)
});
// if the key is a tool with a simple value, we want to convert it to a inline table preserving the version
let is_simple_tool_version =
self.key.starts_with("tools.") && idx == 1 && !container.is_table_like();
if is_simple_tool_version {
let mut inline_table = toml_edit::InlineTable::new();
inline_table.insert("version", container.as_value().unwrap().clone());
*container =
toml_edit::Item::Value(toml_edit::Value::InlineTable(inline_table));
}
}

let type_to_use = match self.type_ {
TomlValueTypes::Infer => {
Expand Down Expand Up @@ -99,7 +112,13 @@ impl ConfigSet {
TomlValueTypes::Infer => bail!("Type not found"),
};

container.as_table_mut().unwrap().insert(last_key, value);
container
.as_table_like_mut()
.unwrap_or({
let t = toml_edit::Table::new();
toml_edit::Item::Table(t).as_table_like_mut().unwrap()
})
.insert(last_key, value);

let raw = config.to_string();
MiseToml::from_str(&raw, &file)?;
Expand Down

0 comments on commit 9bb6c5c

Please sign in to comment.