use std::sync::{Arc, Mutex, PoisonError};
use crate::contracts::CardSource;
#[derive(Default)]
pub struct CardSources {
sources: Mutex<Vec<(String, Arc<dyn CardSource>)>>,
}
impl CardSources {
pub(crate) fn new(sources: Vec<(String, Arc<dyn CardSource>)>) -> Self {
Self {
sources: Mutex::new(sources),
}
}
pub fn add(&self, name: impl Into<String>, source: impl CardSource + 'static) {
let name = name.into();
let mut sources = self.sources.lock().unwrap_or_else(PoisonError::into_inner);
sources.retain(|(existing, _)| *existing != name);
sources.push((name, Arc::new(source)));
}
pub fn all(&self) -> Vec<(String, Arc<dyn CardSource>)> {
self.sources.lock().unwrap_or_else(PoisonError::into_inner).clone()
}
}