100 lines
3.3 KiB
Rust
100 lines
3.3 KiB
Rust
//! Global provider registry.
|
|
//!
|
|
//! [`ProviderRegistry`] is a lazily-initialised, thread-safe map from provider
|
|
//! name to a boxed [`CryptoProvider`]. Each provider crate's `init()` function
|
|
//! registers its implementation here; the bridge crate calls those `init()`
|
|
//! functions from `ccc_init()`.
|
|
//!
|
|
//! # Example
|
|
//!
|
|
//! ```rust,ignore
|
|
//! // In ccc-crypto-wolfssl:
|
|
//! pub fn init() {
|
|
//! ProviderRegistry::global().register("wolfssl", Box::new(WolfSslProvider::new()));
|
|
//! }
|
|
//!
|
|
//! // In the bridge:
|
|
//! pub fn ccc_init() {
|
|
//! ccc_crypto_wolfssl::init();
|
|
//! }
|
|
//!
|
|
//! // Anywhere:
|
|
//! let registry = ProviderRegistry::global();
|
|
//! let provider = registry.get("wolfssl").expect("wolfssl not registered");
|
|
//! let ct = provider.encrypt_aead(...)?;
|
|
//! ```
|
|
|
|
use std::collections::HashMap;
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
use once_cell::sync::OnceCell;
|
|
|
|
use crate::provider::CryptoProvider;
|
|
|
|
// ──────────────────────────────────────────────────────────────────────────────
|
|
|
|
/// Thread-safe registry mapping provider names to boxed [`CryptoProvider`]
|
|
/// trait objects.
|
|
pub struct ProviderRegistry {
|
|
inner: Mutex<HashMap<String, Arc<dyn CryptoProvider>>>,
|
|
}
|
|
|
|
impl ProviderRegistry {
|
|
/// Return the global singleton registry.
|
|
///
|
|
/// Initialised on first call. Safe to call from any thread.
|
|
pub fn global() -> &'static Self {
|
|
static INSTANCE: OnceCell<ProviderRegistry> = OnceCell::new();
|
|
INSTANCE.get_or_init(|| ProviderRegistry {
|
|
inner: Mutex::new(HashMap::new()),
|
|
})
|
|
}
|
|
|
|
/// Register a provider under `name`.
|
|
///
|
|
/// If a provider with the same name already exists it is replaced.
|
|
/// `name` must be lowercase, no spaces (e.g. `"wolfssl"`).
|
|
///
|
|
/// # Panics
|
|
///
|
|
/// Panics if the internal mutex is poisoned (indicates an earlier panic
|
|
/// in a thread holding the lock — treat as unrecoverable).
|
|
pub fn register(&self, name: &str, provider: Box<dyn CryptoProvider>) {
|
|
let mut map = self.inner.lock().expect("ProviderRegistry mutex poisoned");
|
|
log::info!("[ccc-crypto] registered provider '{}'", name);
|
|
map.insert(name.to_string(), Arc::from(provider));
|
|
}
|
|
|
|
/// Retrieve a provider by name.
|
|
///
|
|
/// Returns `None` if no provider with that name has been registered.
|
|
pub fn get(&self, name: &str) -> Option<Arc<dyn CryptoProvider>> {
|
|
let map = self.inner.lock().expect("ProviderRegistry mutex poisoned");
|
|
map.get(name).cloned()
|
|
}
|
|
|
|
/// Return the list of all registered provider names.
|
|
pub fn list(&self) -> Vec<String> {
|
|
let map = self.inner.lock().expect("ProviderRegistry mutex poisoned");
|
|
map.keys().cloned().collect()
|
|
}
|
|
|
|
/// Return `true` if the named provider has been registered.
|
|
pub fn contains(&self, name: &str) -> bool {
|
|
let map = self.inner.lock().expect("ProviderRegistry mutex poisoned");
|
|
map.contains_key(name)
|
|
}
|
|
|
|
/// Remove a provider from the registry.
|
|
///
|
|
/// Primarily for use in tests.
|
|
pub fn unregister(&self, name: &str) {
|
|
let mut map = self.inner.lock().expect("ProviderRegistry mutex poisoned");
|
|
map.remove(name);
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[path = "registry_tests.rs"]
|
|
mod tests;
|