use std::collections::HashMap;
use serde::Serialize;
use crate::column::{ColumnDef, ColumnKind};
use crate::value::normalize_text;
#[derive(Debug, Clone)]
pub struct HeaderOptions {
pub row_index: u32,
pub row_span: u32,
pub fuzzy: bool,
pub normalize: bool,
pub ignore_unknown: bool,
pub allow_index_fallback: bool,
pub trim: bool,
}
impl Default for HeaderOptions {
fn default() -> Self {
Self {
row_index: 0,
row_span: 1,
fuzzy: true,
normalize: true,
ignore_unknown: true,
allow_index_fallback: false,
trim: true,
}
}
}
impl HeaderOptions {
pub fn strict() -> Self {
Self {
fuzzy: false,
allow_index_fallback: false,
..Default::default()
}
}
pub fn row_index(mut self, row_index: u32) -> Self {
self.row_index = row_index;
self
}
pub fn row_span(mut self, span: u32) -> Self {
self.row_span = span.max(1);
self
}
pub fn strict_match(mut self) -> Self {
self.fuzzy = false;
self
}
pub fn index_fallback(mut self, enable: bool) -> Self {
self.allow_index_fallback = enable;
self
}
pub fn normalize_enabled(mut self, enable: bool) -> Self {
self.normalize = enable;
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MatchKind {
Exact,
Alias,
Fuzzy,
IndexFallback,
Missing,
}
impl MatchKind {
pub fn as_str(self) -> &'static str {
match self {
MatchKind::Exact => "exact",
MatchKind::Alias => "alias",
MatchKind::Fuzzy => "fuzzy",
MatchKind::IndexFallback => "index",
MatchKind::Missing => "missing",
}
}
}
#[derive(Debug, Clone)]
pub struct ResolvedColumn {
pub order: usize,
pub header: String,
pub required: bool,
pub kind: ColumnKind,
pub file_index: Option<u16>,
pub file_header: Option<String>,
pub matched_by: MatchKind,
}
impl ResolvedColumn {
pub fn is_matched(&self) -> bool {
self.file_index.is_some()
}
}
#[derive(Debug, Clone)]
pub struct HeaderMap {
pub columns: Vec<ResolvedColumn>,
pub file_headers: Vec<String>,
pub unmapped: Vec<(u16, String)>,
pub header_row: u32,
pub header_span: u32,
}
impl HeaderMap {
pub fn index_of(&self, header: &str) -> Option<u16> {
self.columns
.iter()
.find(|c| c.header == header && c.file_index.is_some())
.and_then(|c| c.file_index)
}
pub fn resolved(&self, header: &str) -> Option<&ResolvedColumn> {
self.columns.iter().find(|c| c.header == header)
}
pub fn file_header(&self, col: u16) -> Option<&str> {
self.file_headers.get(col as usize).map(|s| s.as_str())
}
pub fn missing_required(&self) -> Vec<String> {
self.columns
.iter()
.filter(|c| c.required && !c.is_matched())
.map(|c| c.header.clone())
.collect()
}
pub fn unknown_columns(&self) -> Vec<String> {
self.unmapped.iter().map(|(_, h)| h.clone()).collect()
}
pub fn analysis(&self) -> HeaderAnalysis {
HeaderAnalysis {
header_row: self.header_row + 1,
header_span: self.header_span,
total_columns: self.file_headers.len(),
matched: self
.columns
.iter()
.filter(|c| c.is_matched())
.map(|c| MatchedColumn {
header: c.header.clone(),
file_header: c.file_header.clone().unwrap_or_default(),
excel_column: excel_column_name(c.file_index.unwrap_or(0)),
matched_by: c.matched_by.as_str().to_string(),
})
.collect(),
missing: self.missing_required(),
unknown: self.unknown_columns(),
}
}
pub fn dynamic(file_headers: Vec<String>) -> Self {
let columns = file_headers
.iter()
.enumerate()
.map(|(i, h)| ResolvedColumn {
order: i,
header: h.clone(),
required: false,
kind: ColumnKind::Any,
file_index: Some(i as u16),
file_header: Some(h.clone()),
matched_by: MatchKind::Exact,
})
.collect();
Self {
columns,
file_headers,
unmapped: Vec::new(),
header_row: 0,
header_span: 1,
}
}
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct HeaderAnalysis {
pub header_row: u32,
pub header_span: u32,
pub total_columns: usize,
pub matched: Vec<MatchedColumn>,
pub missing: Vec<String>,
pub unknown: Vec<String>,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct MatchedColumn {
pub header: String,
pub file_header: String,
pub excel_column: String,
pub matched_by: String,
}
pub fn normalize_header(raw: &str) -> String {
let t = normalize_text(raw);
let mut out = String::with_capacity(t.len());
for c in t.chars() {
match c {
' ' | '\t' | '\r' | '\n' | '\u{3000}' | '\u{00a0}' => {}
'*' | '★' | '☆' | '●' | '▲' => {}
'(' => out.push('('),
')' => out.push(')'),
':' => out.push(':'),
',' | '、' => out.push(','),
'/' => out.push('/'),
'-' | '—' | '–' => out.push('-'),
c => out.push(c.to_ascii_lowercase()),
}
}
while out.ends_with(':') || out.ends_with('(') {
out.pop();
}
out
}
pub fn excel_column_name(index: u16) -> String {
let mut n = index as u32;
let mut out = String::new();
loop {
let r = (n % 26) as u8;
out.insert(0, (b'A' + r) as char);
if n < 26 {
break;
}
n = n / 26 - 1;
}
out
}
pub fn resolve_headers(
file_headers: &[String],
defs: &[ColumnDef],
opts: &HeaderOptions,
) -> HeaderMap {
let norm: Vec<String> = file_headers
.iter()
.map(|h| normalize_key(h, opts))
.collect();
let mut claimed = vec![false; file_headers.len()];
let mut columns = Vec::with_capacity(defs.len());
for (order, def) in defs.iter().enumerate() {
if !def.is_importable() {
continue;
}
let target = normalize_key(&def.header, opts);
let alias_keys: Vec<String> = def.aliases.iter().map(|a| normalize_key(a, opts)).collect();
let mut matched: Option<(usize, MatchKind)> = None;
if let Some(i) = find_exact(&norm, &claimed, std::slice::from_ref(&target)) {
matched = Some((i, MatchKind::Exact));
}
if matched.is_none() {
if let Some(i) = find_exact(&norm, &claimed, &alias_keys) {
matched = Some((i, MatchKind::Alias));
}
}
if matched.is_none() && opts.fuzzy {
let mut keys = vec![target.clone()];
keys.extend(alias_keys.iter().cloned());
if let Some(i) = find_fuzzy(&norm, &claimed, &keys) {
matched = Some((i, MatchKind::Fuzzy));
}
}
if matched.is_none() && opts.allow_index_fallback && order < norm.len() && !claimed[order] {
matched = Some((order, MatchKind::IndexFallback));
}
match matched {
Some((i, kind)) => {
claimed[i] = true;
columns.push(ResolvedColumn {
order,
header: def.header.clone(),
required: def.required,
kind: def.kind,
file_index: Some(i as u16),
file_header: Some(file_headers[i].clone()),
matched_by: kind,
});
}
None => columns.push(ResolvedColumn {
order,
header: def.header.clone(),
required: def.required,
kind: def.kind,
file_index: None,
file_header: None,
matched_by: MatchKind::Missing,
}),
}
}
let unmapped: Vec<(u16, String)> = file_headers
.iter()
.enumerate()
.filter(|(i, _)| !claimed[*i])
.map(|(i, h)| (i as u16, h.clone()))
.collect();
HeaderMap {
columns,
file_headers: file_headers.to_vec(),
unmapped,
header_row: opts.row_index,
header_span: opts.row_span.max(1),
}
}
fn normalize_key(raw: &str, opts: &HeaderOptions) -> String {
if opts.normalize {
normalize_header(raw)
} else {
raw.trim().to_ascii_lowercase()
}
}
fn find_exact(norm: &[String], claimed: &[bool], keys: &[String]) -> Option<usize> {
keys.iter().find_map(|k| {
if k.is_empty() {
return None;
}
norm.iter()
.enumerate()
.find(|(i, h)| !claimed[*i] && h.as_str() == k.as_str())
.map(|(i, _)| i)
})
}
fn find_fuzzy(norm: &[String], claimed: &[bool], keys: &[String]) -> Option<usize> {
let mut best: Option<(usize, usize)> = None;
for key in keys {
if key.chars().count() < 2 {
continue;
}
for (i, h) in norm.iter().enumerate() {
if claimed[i] || h.is_empty() {
continue;
}
if h.contains(key.as_str()) || key.contains(h.as_str()) {
let diff = h.chars().count().abs_diff(key.chars().count());
if best.map(|(_, d)| diff < d).unwrap_or(true) {
best = Some((i, diff));
}
}
}
}
best.map(|(i, _)| i)
}
pub fn simple_columns<I, S>(headers: I) -> Vec<ColumnDef>
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
headers.into_iter().map(ColumnDef::new).collect()
}
pub fn index_map(map: &HeaderMap) -> HashMap<String, u16> {
map.columns
.iter()
.filter_map(|c| c.file_index.map(|i| (c.header.clone(), i)))
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
fn headers(v: &[&str]) -> Vec<String> {
v.iter().map(|s| s.to_string()).collect()
}
#[test]
fn test_exact_and_alias_and_fuzzy() {
let defs = vec![
ColumnDef::new("工厂名称").required(),
ColumnDef::new("工厂品号").alias("品号").required(),
ColumnDef::new("报价(USD)").kind(ColumnKind::Decimal),
ColumnDef::new("图片1").image(),
];
let file = headers(&[" 工厂名称 ", "品 号", "报价(USD)(含税)", "备注", "图片1"]);
let map = resolve_headers(&file, &defs, &HeaderOptions::default());
assert_eq!(map.index_of("工厂名称"), Some(0));
assert_eq!(map.index_of("工厂品号"), Some(1));
assert_eq!(map.index_of("报价(USD)"), Some(2));
assert_eq!(map.index_of("图片1"), Some(4));
assert_eq!(map.missing_required(), Vec::<String>::new());
assert_eq!(map.unknown_columns(), vec!["备注".to_string()]);
}
#[test]
fn test_missing_required() {
let defs = vec![
ColumnDef::new("工厂名称").required(),
ColumnDef::new("品名").required(),
];
let file = headers(&["工厂名称"]);
let map = resolve_headers(&file, &defs, &HeaderOptions::default());
assert_eq!(map.missing_required(), vec!["品名".to_string()]);
}
#[test]
fn test_strict_mode_rejects_fuzzy() {
let defs = vec![ColumnDef::new("报价(USD)")];
let file = headers(&["报价(USD)(含税)"]);
let loose = resolve_headers(&file, &defs, &HeaderOptions::default());
assert_eq!(loose.index_of("报价(USD)"), Some(0));
let strict = resolve_headers(&file, &defs, &HeaderOptions::strict());
assert_eq!(strict.index_of("报价(USD)"), None);
}
#[test]
fn test_excel_column_name() {
assert_eq!(excel_column_name(0), "A");
assert_eq!(excel_column_name(25), "Z");
assert_eq!(excel_column_name(26), "AA");
assert_eq!(excel_column_name(701), "ZZ");
assert_eq!(excel_column_name(702), "AAA");
}
}