use burn::module::Module;
use crate::{
errors::{
BunsenError,
BunsenResult,
LookupError,
WithOkOrPanic,
},
kits::speech::silero_vad::SileroVad,
};
#[derive(Module, Debug)]
pub struct SileroVadCollection {
pub branches: Vec<(usize, SileroVad)>,
}
impl SileroVadCollection {
pub fn try_branch(
&self,
sample_rate: usize,
) -> BunsenResult<&SileroVad> {
self.branches
.iter()
.find(|(rate, _)| *rate == sample_rate)
.map(|(_, vad)| vad)
.ok_or_else(|| {
BunsenError::lookup(
LookupError::missing("sample-rate branch", sample_rate.to_string())
.with_candidates(self.branches.iter().map(|(rate, _)| rate.to_string())),
)
})
}
pub fn expect_branch(
&self,
sample_rate: usize,
) -> &SileroVad {
self.try_branch(sample_rate).ok_or_panic()
}
}