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

Code before:
        return Err("Exceeded maximum configuration import depth".into());
    }

    let mut import_paths = Vec::new();

    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());
    }
}


Review comment (suggestion):
Maybe we should add a comment *why* one would want to do this?

So something like "to ensure toml compatibility" or something.

Code after:
        return Err("Exceeded maximum configuration import depth".into());
    }

    let mut import_paths = Vec::new();

    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());