use std::{collections::HashMap, sync::Mutex};
use crate::runner::aligner::{
aligner::Aligner,
key::{AlignerKey, AlignmentFallback},
set::AlignmentSet,
};
pub struct AlignmentSetBuilder {
aligners: HashMap<AlignerKey, Mutex<Aligner>>,
fallback: AlignmentFallback,
}
impl AlignmentSetBuilder {
pub fn new() -> Self {
Self {
aligners: HashMap::new(),
fallback: AlignmentFallback::SkipChunk,
}
}
pub const fn with_fallback(mut self, value: AlignmentFallback) -> Self {
self.fallback = value;
self
}
pub const fn set_fallback(&mut self, value: AlignmentFallback) {
self.fallback = value;
}
pub fn register(mut self, key: AlignerKey, aligner: Aligner) -> Self {
if let AlignerKey::Lang(ref key_lang) = key {
assert_eq!(
aligner.language(),
key_lang,
"AlignerKey::Lang({key_lang:?}) registration cannot accept an aligner built \
for {actual:?}; either register under AlignerKey::Lang({actual:?}), \
AlignerKey::Any (explicit multilingual fallback), or rebuild the \
aligner for the desired language",
actual = aligner.language(),
);
}
self.aligners.insert(key, Mutex::new(aligner));
self
}
pub fn len(&self) -> usize {
self.aligners.len()
}
pub fn is_empty(&self) -> bool {
self.aligners.is_empty()
}
pub fn build(self) -> AlignmentSet {
AlignmentSet::from_parts(self.aligners, self.fallback)
}
}
impl Default for AlignmentSetBuilder {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::Lang;
#[test]
fn empty_builder_default_fallback() {
let b = AlignmentSetBuilder::new();
assert!(b.is_empty());
assert_eq!(b.len(), 0);
}
#[test]
fn with_fallback_round_trip() {
let b = AlignmentSetBuilder::new().with_fallback(AlignmentFallback::Error);
let set = b.build();
assert_eq!(set.fallback(), AlignmentFallback::Error);
}
#[test]
fn build_empty_produces_empty_set() {
let set = AlignmentSetBuilder::new().build();
assert!(set.is_empty());
match set.lookup(&Lang::En) {
crate::runner::aligner::set::AlignmentLookup::Miss { fallback } => {
assert_eq!(fallback, AlignmentFallback::SkipChunk);
}
_ => panic!("expected Miss"),
}
}
}