sqlload 0.91.1

SQL data loading tooling.
//! The existing columns a source column could load into when no rule picks one of them.

use crate::column_types::models::Warehouse;
use crate::load_naming::_helpers::spellings::{kept_as_written, reaches};
use crate::load_naming::main::existing_spelling::existing_spelling;

/// The columns of `existing` a column with the source name `source` would load into under [`existing_spelling`]'s rule, when several match it ignoring case and neither the source name nor `named` is one of them: loading into any would be a guess, so the plan names them all instead.
pub fn ambiguous_spellings<'a>(
    (existing, warehouse): (&'a [String], Option<Warehouse>),
    source: &str,
    named: &str,
) -> Vec<&'a str> {
    let matching: Vec<&'a str> = existing
        .iter()
        .filter(|column| column.eq_ignore_ascii_case(source))
        .filter(|column| kept_as_written(column, warehouse))
        .map(String::as_str)
        .collect();
    let decided = matching.len() <= 1
        || matching.contains(&source)
        || existing_spelling((existing, warehouse), source, named).is_some()
        || matching
            .iter()
            .any(|column| reaches(column, named, warehouse));
    match decided {
        true => Vec::new(),
        false => matching,
    }
}