use crate::chart::Chart;
#[must_use]
pub fn normalize_cp1252_str(s: &str) -> String {
let filtered: String = s.chars().filter(|&ch| is_cp1252(ch)).collect();
filtered.split_whitespace().collect::<Vec<_>>().join(" ")
}
pub fn normalize_chart(chart: &mut Chart) {
chart.name = normalize_cp1252_str(&chart.name);
if let Some(v) = &chart.secondary_name {
chart.secondary_name = Some(normalize_cp1252_str(v));
}
if let Some(v) = &chart.city {
chart.city = Some(normalize_cp1252_str(v));
}
if let Some(v) = &chart.region {
chart.region = Some(normalize_cp1252_str(v));
}
if let Some(v) = &chart.source_rating {
chart.source_rating = Some(normalize_cp1252_str(v));
}
if let Some(v) = &chart.notes {
chart.notes = Some(normalize_cp1252_str(v));
}
for sub in &mut chart.sub_charts {
sub.name = normalize_cp1252_str(&sub.name);
if let Some(v) = &sub.city {
sub.city = Some(normalize_cp1252_str(v));
}
if let Some(v) = &sub.region {
sub.region = Some(normalize_cp1252_str(v));
}
if let Some(v) = &sub.notes {
sub.notes = Some(normalize_cp1252_str(v));
}
}
}
fn is_cp1252(ch: char) -> bool {
let mut buf = [0u8; 4];
let s = ch.encode_utf8(&mut buf);
let (encoded, _, had_errors) = encoding_rs::WINDOWS_1252.encode(s);
!had_errors && encoded.len() == 1
}