#![allow(deprecated)]
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct SpeakerId(pub u32);
#[derive(Debug, Clone, PartialEq)]
pub struct SpeakerIdRemap {
mapping: Vec<(SpeakerId, SpeakerId)>,
}
impl SpeakerIdRemap {
pub fn from_mapping(mapping: Vec<(SpeakerId, SpeakerId)>) -> Option<Self> {
let mut seen = HashSet::with_capacity(mapping.len());
for (old, _) in &mapping {
if !seen.insert(old) {
return None;
}
}
Some(Self { mapping })
}
pub fn remap(&self, id: SpeakerId) -> SpeakerId {
self.mapping
.iter()
.find(|(old, _)| *old == id)
.map(|(_, new)| *new)
.unwrap_or(id)
}
pub fn is_empty(&self) -> bool {
self.mapping.is_empty()
}
pub fn len(&self) -> usize {
self.mapping.len()
}
}
impl fmt::Display for SpeakerId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "SPEAKER_{:02}", self.0)
}
}