aboutsummaryrefslogtreecommitdiff
path: root/alacritty/src/migrate.rs
blob: 5f47b08313a7a861c29d005fe2beb6feb1b17a74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//! Configuration file migration.

use std::fs;
use std::path::Path;

use toml::map::Entry;
use toml::{Table, Value};

use crate::cli::MigrateOptions;
use crate::config;

/// Handle migration.
pub fn migrate(options: MigrateOptions) {
    // Find configuration file path.
    let config_path = options
        .config_file
        .clone()
        .or_else(|| config::installed_config("toml"))
        .or_else(|| config::installed_config("yml"));

    // Abort if system has no installed configuration.
    let config_path = match config_path {
        Some(config_path) => config_path,
        None => {
            eprintln!("No configuration file found");
            std::process::exit(1);
        },
    };

    // If we're doing a wet run, perform a dry run first for safety.
    if !options.dry_run {
        #[allow(clippy::redundant_clone)]
        let mut options = options.clone();
        options.silent = true;
        options.dry_run = true;
        if let Err(err) = migrate_config(&options, &config_path, config::IMPORT_RECURSION_LIMIT) {
            eprintln!("Configuration file migration failed:");
            eprintln!("    {config_path:?}: {err}");
            std::process::exit(1);
        }
    }

    // Migrate the root config.
    match migrate_config(&options, &config_path, config::IMPORT_RECURSION_LIMIT) {
        Ok(new_path) => {
            if !options.silent {
                println!("Successfully migrated {config_path:?} to {new_path:?}");
            }
        },
        Err(err) => {
            eprintln!("Configuration file migration failed:");
            eprintln!("    {config_path:?}: {err}");
            std::process::exit(1);
        },
    }
}

/// Migrate a specific configuration file.
fn migrate_config(
    options: &MigrateOptions,
    path: &Path,
    recursion_limit: usize,
) -> Result<String, String> {
    // Ensure configuration file has an extension.
    let path_str = path.to_string_lossy();
    let (prefix, suffix) = match path_str.rsplit_once('.') {
        Some((prefix, suffix)) => (prefix, suffix),
        None => return Err("missing file extension".to_string()),
    };

    // Abort if config is already toml.
    if suffix == "toml" {
        return Err("already in TOML format".to_string());
    }

    // Try to parse the configuration file.
    let mut config = match config::deserialize_config(path) {
        Ok(config) => config,
        Err(err) => return Err(format!("parsing error: {err}")),
    };

    // Migrate config imports.
    if !options.skip_imports {
        migrate_imports(options, &mut config, recursion_limit)?;
    }

    // Migrate deprecated field names to their new location.
    if !options.skip_renames {
        migrate_renames(&mut config)?;
    }

    // Convert to TOML format.
    let toml = toml::to_string(&config).map_err(|err| format!("conversion error: {err}"))?;
    let new_path = format!("{prefix}.toml");

    if options.dry_run && !options.silent {
        // Output new content to STDOUT.
        println!(
            "\nv-----Start TOML for {path:?}-----v\n\n{toml}\n^-----End TOML for {path:?}-----^\n"
        );
    } else if !options.dry_run {
        // Write the new toml configuration.
        fs::write(&new_path, toml).map_err(|err| format!("filesystem error: {err}"))?;
    }

    Ok(new_path)
}

/// Migrate the imports of a config.
fn migrate_imports(
    options: &MigrateOptions,
    config: &mut Value,
    recursion_limit: usize,
) -> Result<(), String> {
    let imports = match config::imports(config, recursion_limit) {
        Ok(imports) => imports,
        Err(err) => return Err(format!("import error: {err}")),
    };

    // Migrate the individual imports.
    let mut new_imports = Vec::new();
    for import in imports {
        let import = match import {
            Ok(import) => import,
            Err(err) => return Err(format!("import error: {err}")),
        };

        // Keep yaml import if path does not exist.
        if !import.exists() {
            if options.dry_run {
                eprintln!("Keeping yaml config for nonexistent import: {import:?}");
            }
            new_imports.push(Value::String(import.to_string_lossy().into()));
            continue;
        }

        let new_path = migrate_config(options, &import, recursion_limit - 1)?;

        // Print new import path.
        if options.dry_run {
            println!("Successfully migrated import {import:?} to {new_path:?}");
        }

        new_imports.push(Value::String(new_path));
    }

    // Update the imports field.
    if let Some(import) = config.get_mut("import") {
        *import = Value::Array(new_imports);
    }

    Ok(())
}

/// Migrate deprecated fields.
fn migrate_renames(config: &mut Value) -> Result<(), String> {
    let config_table = match config.as_table_mut() {
        Some(config_table) => config_table,
        None => return Ok(()),
    };

    // draw_bold_text_with_bright_colors -> colors.draw_bold_text_with_bright_colors
    move_value(config_table, &["draw_bold_text_with_bright_colors"], &[
        "colors",
        "draw_bold_text_with_bright_colors",
    ])?;

    // key_bindings -> keyboard.bindings
    move_value(config_table, &["key_bindings"], &["keyboard", "bindings"])?;

    // mouse_bindings -> mouse.bindings
    move_value(config_table, &["mouse_bindings"], &["mouse", "bindings"])?;

    Ok(())
}

/// Move a toml value from one map to another.
fn move_value(config_table: &mut Table, origin: &[&str], target: &[&str]) -> Result<(), String> {
    if let Some(value) = remove_node(config_table, origin)? {
        if !insert_node_if_empty(config_table, target, value)? {
            return Err(format!(
                "conflict: both `{}` and `{}` are set",
                origin.join("."),
                target.join(".")
            ));
        }
    }

    Ok(())
}

/// Remove a node from a tree of tables.
fn remove_node(table: &mut Table, path: &[&str]) -> Result<Option<Value>, String> {
    if path.len() == 1 {
        Ok(table.remove(path[0]))
    } else {
        let next_table_value = match table.get_mut(path[0]) {
            Some(next_table_value) => next_table_value,
            None => return Ok(None),
        };

        let next_table = match next_table_value.as_table_mut() {
            Some(next_table) => next_table,
            None => return Err(format!("invalid `{}` table", path[0])),
        };

        remove_node(next_table, &path[1..])
    }
}

/// Try to insert a node into a tree of tables.
///
/// Returns `false` if the node already exists.
fn insert_node_if_empty(table: &mut Table, path: &[&str], node: Value) -> Result<bool, String> {
    if path.len() == 1 {
        match table.entry(path[0]) {
            Entry::Vacant(vacant_entry) => {
                vacant_entry.insert(node);
                Ok(true)
            },
            Entry::Occupied(_) => Ok(false),
        }
    } else {
        let next_table_value = table.entry(path[0]).or_insert_with(|| Value::Table(Table::new()));

        let next_table = match next_table_value.as_table_mut() {
            Some(next_table) => next_table,
            None => return Err(format!("invalid `{}` table", path[0])),
        };

        insert_node_if_empty(next_table, &path[1..], node)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn move_values() {
        let input = r#"
root_value = 3

[table]
table_value = 5

[preexisting]
not_moved = 9
        "#;

        let mut value: Value = toml::from_str(input).unwrap();
        let table = value.as_table_mut().unwrap();

        move_value(table, &["root_value"], &["new_table", "root_value"]).unwrap();
        move_value(table, &["table", "table_value"], &["preexisting", "subtable", "new_name"])
            .unwrap();

        let output = toml::to_string(table).unwrap();

        assert_eq!(
            output,
            "[new_table]\nroot_value = 3\n\n[preexisting]\nnot_moved = \
             9\n\n[preexisting.subtable]\nnew_name = 5\n\n[table]\n"
        );
    }
}