diff options
author | Christian Duerr <contact@christianduerr.com> | 2024-09-23 02:15:52 +0200 |
---|---|---|
committer | Christian Duerr <contact@christianduerr.com> | 2024-10-02 21:38:07 +0200 |
commit | 3db09595f31bd9f2f211d43d96f0acb887a68991 (patch) | |
tree | 43ecfa0cff7705516085a869c664d1972591d0a2 /alacritty/src/migrate/yaml.rs | |
parent | 51089cfeed1adfd741d8dcbe6c9cb9376f88a576 (diff) | |
download | r-alacritty-3db09595f31bd9f2f211d43d96f0acb887a68991.tar.gz r-alacritty-3db09595f31bd9f2f211d43d96f0acb887a68991.tar.bz2 r-alacritty-3db09595f31bd9f2f211d43d96f0acb887a68991.zip |
Add migration support for TOML config changes
This patch allows running `alacritty migrate` to automatically apply
configuration changes made to the TOML format, like moving `ipc_socket`
to `general.ipc_socket`.
This should reduce the friction of moving around individual options
significantly, while also persisting the format of the existing TOML
file thanks to `toml_edit`.
The YAML migration has been simplified significantly to only switch the
format of the file from YAML to TOML. The new TOML features are used for
everything else.
Diffstat (limited to 'alacritty/src/migrate/yaml.rs')
-rw-r--r-- | alacritty/src/migrate/yaml.rs | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/alacritty/src/migrate/yaml.rs b/alacritty/src/migrate/yaml.rs new file mode 100644 index 00000000..9607e95e --- /dev/null +++ b/alacritty/src/migrate/yaml.rs @@ -0,0 +1,87 @@ +//! Migration of legacy YAML files to TOML. + +use std::path::Path; + +use toml::Value; + +use crate::cli::MigrateOptions; +use crate::config; +use crate::migrate::{migrate_config, migrate_toml, write_results}; + +/// Migrate a legacy YAML config to TOML. +pub fn migrate( + options: &MigrateOptions, + path: &Path, + recursion_limit: usize, + prefix: &str, +) -> Result<String, String> { + // Try to parse the configuration file. + let mut config = match config::deserialize_config(path, !options.dry_run) { + Ok(config) => config, + Err(err) => return Err(format!("YAML parsing error: {err}")), + }; + + // Migrate config imports. + if !options.skip_imports { + migrate_imports(options, &mut config, path, recursion_limit)?; + } + + // Convert to TOML format. + let mut toml = toml::to_string(&config).map_err(|err| format!("conversion error: {err}"))?; + let new_path = format!("{prefix}.toml"); + + // Apply TOML migration, without recursing through imports. + toml = migrate_toml(toml)?.to_string(); + + // Write migrated TOML config. + write_results(options, &new_path, &toml)?; + + Ok(new_path) +} + +/// Migrate the imports of a config. +fn migrate_imports( + options: &MigrateOptions, + config: &mut Value, + base_path: &Path, + recursion_limit: usize, +) -> Result<(), String> { + let imports = match config::imports(config, base_path, 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 migration = migrate_config(options, &import, recursion_limit - 1)?; + + // Print success message. + if options.dry_run { + println!("{}", migration.success_message(true)); + } + + new_imports.push(Value::String(migration.new_path())); + } + + // Update the imports field. + if let Some(import) = config.get_mut("import") { + *import = Value::Array(new_imports); + } + + Ok(()) +} |