Repository: alacritty/alacritty
File: alacritty/src/config/mod.rs (Rust)

Code before:
    for import in imports {
        let mut path = match import {
            Value::String(path) => PathBuf::from(path),
            _ => {
                import_paths.push(Err("Invalid import element type: expected path string".into()));
                continue;
            },
        };

        // Resolve paths relative to user's home directory.
        if let (Ok(stripped), Some(home_dir)) = (path.strip_prefix("~/"), home::home_dir()) {
            path = home_dir.join(stripped);
        }

        import_paths.push(Ok(path));
    }

    Ok(import_paths)
}

/// Prune the nulls from the YAML.
fn prune_yaml_nulls(value: &mut serde_yaml::Value, warn_pruned: bool) {
    fn walk(value: &mut serde_yaml::Value, warn_pruned: bool) -> bool {
        if let Some(mapping) = value.as_mapping_mut() {
            mapping.retain(|key, value| {
                let retain = !walk(value, warn_pruned);
                if let Some(key_name) = key.as_str().filter(|_| !retain && warn_pruned) {
                    eprintln!("Removing null key \"{key_name}\" from the end config");
                }
                retain
            });
            mapping.is_empty()
        } else if let Some(sequence) = value.as_sequence_mut() {
            sequence.retain_mut(|value| !walk(value, warn_pruned));
            sequence.is_empty()
        } else {
            value.is_null()
        }
    }

    if walk(value, warn_pruned) {
        // When the value itself is null return the mapping.
        *value = serde_yaml::Value::Mapping(Default::default());
    }
}

/// Get the location of the first found default config file paths
/// according to the following order:
///
/// 1. $XDG_CONFIG_HOME/alacritty/alacritty.toml
/// 2. $XDG_CONFIG_HOME/alacritty.toml

Review comment (bug):
This is recursive, right? There doesn't seem a limit to this, so I could probably craft a config that would run into a stack overflow here?

Code after:
    for import in imports {
        let mut path = match import {
            Value::String(path) => PathBuf::from(path),
            _ => {
                import_paths.push(Err("Invalid import element type: expected path string".into()));
                continue;
            },
        };

        // Resolve paths relative to user's home directory.
        if let (Ok(stripped), Some(home_dir)) = (path.strip_prefix("~/"), home::home_dir()) {
            path = home_dir.join(stripped);
        }

        import_paths.push(Ok(path));
    }

    Ok(import_paths)
}

/// Prune the nulls from the YAML to ensure TOML compatibility.
fn prune_yaml_nulls(value: &mut serde_yaml::Value, warn_pruned: bool) {
    fn walk(value: &mut serde_yaml::Value, warn_pruned: bool) -> bool {
        match value {
            serde_yaml::Value::Sequence(sequence) => {
                sequence.retain_mut(|value| !walk(value, warn_pruned));
                sequence.is_empty()
            },
            serde_yaml::Value::Mapping(mapping) => {
                mapping.retain(|key, value| {
                    let retain = !walk(value, warn_pruned);
                    if let Some(key_name) = key.as_str().filter(|_| !retain && warn_pruned) {
                        eprintln!("Removing null key \"{key_name}\" from the end config");
                    }
                    retain
                });
                mapping.is_empty()
            },
            serde_yaml::Value::Null => true,
            _ => false,
        }
    }

    if walk(value, warn_pruned) {
        // When the value itself is null return the mapping.
        *value = serde_yaml::Value::Mapping(Default::default());
    }
}

/// Get the location of the first found default config file paths
/// according to the following order: