settings.rs

 1use anyhow::Result;
 2use serde_json::Value;
 3
 4use crate::migrations::migrate_settings;
 5
 6const SETTINGS_KEY: &str = "settings";
 7const TITLE_BAR_KEY: &str = "title_bar";
 8const OLD_KEY: &str = "show_branch_icon";
 9const NEW_KEY: &str = "show_branch_status_icon";
10
11pub fn promote_show_branch_icon_true_to_show_branch_status_icon(value: &mut Value) -> Result<()> {
12    migrate_settings(value, &mut migrate_one)
13}
14
15fn migrate_one(object: &mut serde_json::Map<String, Value>) -> Result<()> {
16    migrate_title_bar_value(object);
17
18    if let Some(settings) = object
19        .get_mut(SETTINGS_KEY)
20        .and_then(|value| value.as_object_mut())
21    {
22        migrate_title_bar_value(settings);
23    }
24
25    Ok(())
26}
27
28fn migrate_title_bar_value(object: &mut serde_json::Map<String, Value>) {
29    let Some(title_bar) = object
30        .get_mut(TITLE_BAR_KEY)
31        .and_then(|value| value.as_object_mut())
32    else {
33        return;
34    };
35
36    let Some(old_value) = title_bar.remove(OLD_KEY) else {
37        return;
38    };
39
40    if title_bar.contains_key(NEW_KEY) {
41        return;
42    }
43
44    if old_value == Value::Bool(true) {
45        title_bar.insert(NEW_KEY.to_string(), Value::Bool(true));
46    }
47}