use std::collections::HashSet;
pub fn parse_performers(performers_str: &str) -> Vec<(&str, Vec<&str>)> {
performers_str
.split(" - ")
.filter_map(|group| {
let group = group.trim();
let mut parts: Vec<&str> = group.split(',').map(str::trim).collect();
if parts.is_empty() {
return None;
}
let person_name = parts.remove(0).trim();
Some((person_name, parts))
})
.collect()
}
pub fn extract_artist_names_from_performers(
performers_str: &str,
existing: &HashSet<String>,
) -> Vec<String> {
let mut names = Vec::new();
for (person_name, roles) in parse_performers(performers_str) {
let has_role = roles.iter().any(|role| {
*role == "Performer" || role.contains("MainArtist") || role.contains("FeaturedArtist")
});
if has_role && !existing.contains(person_name) && !names.contains(&person_name.to_string())
{
names.push(person_name.to_string());
}
}
names
}
pub fn extract_composers_from_performers(performers_str: &str) -> Vec<String> {
let mut composers = Vec::new();
for (person_name, roles) in parse_performers(performers_str) {
let is_composer = roles
.iter()
.any(|r| r.contains("Composer") || r.contains("Lyricist"));
if is_composer && !composers.contains(&person_name.to_string()) {
composers.push(person_name.to_string());
}
}
composers
}
pub fn extract_producers_from_performers(performers_str: &str) -> Vec<String> {
let mut producers = Vec::new();
for (person_name, roles) in parse_performers(performers_str) {
if roles.contains(&"Producer") {
producers.push(person_name.to_string());
}
}
producers
}
pub fn normalize_composer_name(name: &str) -> String {
name.to_lowercase()
.trim()
.replace(['.', ','], "")
.replace('-', " ")
.replace(" ", " ")
.trim()
.to_string()
}
pub fn is_duplicate_composer(name: &str, existing: &HashSet<String>) -> bool {
let normalized = normalize_composer_name(name);
if existing.contains(&normalized) {
return true;
}
for e in existing {
if e.contains(&normalized) || normalized.contains(e) {
return true;
}
}
false
}