use binrw::binrw;
use super::StringAscii;
#[binrw]
#[derive(Default, Clone, PartialEq, Eq)]
#[brw(big)]
pub struct NameList(StringAscii);
impl NameList {
pub fn new(names: &[impl AsRef<str>]) -> Self {
Self(StringAscii::new(
names
.iter()
.map(AsRef::as_ref)
.collect::<Vec<_>>()
.join(","),
))
}
pub fn preferred_in(&self, other: &Self) -> Option<&str> {
self.into_iter()
.find(|&name| other.into_iter().any(|n| name == n))
}
}
impl<'n> IntoIterator for &'n NameList {
type Item = &'n str;
type IntoIter = std::iter::Filter<std::str::Split<'n, char>, for<'a> fn(&'a &'n str) -> bool>;
fn into_iter(self) -> Self::IntoIter {
self.0.split(',').filter(|s| !s.is_empty())
}
}
impl std::fmt::Debug for NameList {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("NameList")
.field(&self.into_iter().collect::<Vec<_>>())
.finish()
}
}