use crate::types::Epoch;
use commonware_cryptography::certificate::{self, Scheme};
use commonware_utils::sync::Mutex;
use std::{collections::HashMap, sync::Arc};
#[derive(Clone)]
pub struct Provider<S: Scheme> {
schemes: Arc<Mutex<HashMap<Epoch, Arc<S>>>>,
}
impl<S: Scheme> Default for Provider<S> {
fn default() -> Self {
Self::new()
}
}
impl<S: Scheme> Provider<S> {
pub fn new() -> Self {
Self {
schemes: Arc::new(Mutex::new(HashMap::new())),
}
}
}
impl<S: Scheme> Provider<S> {
pub fn register(&self, epoch: Epoch, scheme: S) -> bool {
let mut schemes = self.schemes.lock();
schemes.insert(epoch, Arc::new(scheme)).is_none()
}
}
impl<S: Scheme> certificate::Provider for Provider<S> {
type Scope = Epoch;
type Scheme = S;
fn scoped(&self, epoch: Epoch) -> Option<Arc<S>> {
let schemes = self.schemes.lock();
schemes.get(&epoch).cloned()
}
}