use super::ast::{Axis, AXIS_NAMES};
use super::error::SelectorError;
pub fn nearest(
name: &str,
candidates: impl IntoIterator<Item = &'static str>,
) -> Option<&'static str> {
let mut best: Option<(usize, &'static str)> = None;
for cand in candidates {
let d = edit_distance(name, cand);
if d > 2 {
continue;
}
match best {
Some((best_distance, best_candidate))
if best_distance < d || (best_distance == d && best_candidate <= cand) => {}
_ => best = Some((d, cand)),
}
}
best.map(|(_, candidate)| candidate)
}
fn edit_distance(a: &str, b: &str) -> usize {
let a: Vec<char> = a.chars().collect();
let b: Vec<char> = b.chars().collect();
if a.is_empty() {
return b.len();
}
if b.is_empty() {
return a.len();
}
let mut previous: Vec<usize> = (0..=b.len()).collect();
let mut current = vec![0usize; b.len() + 1];
for (index, left) in a.iter().enumerate() {
current[0] = index + 1;
for (other_index, right) in b.iter().enumerate() {
let cost = usize::from(left != right);
current[other_index + 1] = (previous[other_index + 1] + 1)
.min(current[other_index] + 1)
.min(previous[other_index] + cost);
}
std::mem::swap(&mut previous, &mut current);
}
previous[b.len()]
}
pub fn unknown_axis(name: &str, offset: usize) -> SelectorError {
let error = SelectorError::resolve(offset, format!("알 수 없는 축 `{name}`"))
.expecting(AXIS_NAMES.iter().map(|(name, _)| *name));
match nearest(name, AXIS_NAMES.iter().map(|(name, _)| *name)) {
Some(candidate) => error.hinting(format!("`{candidate}` 를 뜻했나")),
None => error,
}
}
pub fn unknown_attr(axis: Axis, name: &str, offset: usize) -> SelectorError {
let names: Vec<&'static str> = axis
.attributes()
.iter()
.map(|attribute| attribute.name)
.collect();
let error = SelectorError::resolve(
offset,
format!("축 `{}` 에 없는 속성 `{name}`", axis.name()),
)
.expecting(names.clone());
if let Some(candidate) = nearest(name, names) {
return error.hinting(format!("`{candidate}` 를 뜻했나"));
}
if axis == Axis::Any {
return error.hinting(
"축을 생략하면 `*` 이라 공통 속성만 쓸 수 있다 — 술어는 축에 붙여 `para[len>0]` 처럼 적는다 (공백은 자손 결합자다)",
);
}
error
}