use super::Cryptographer;
use once_cell::sync::OnceCell;
static CRYPTOGRAPHER: OnceCell<&'static dyn Cryptographer> = OnceCell::new();
#[derive(Debug, thiserror::Error)]
#[error("Cryptographer already initialized")]
pub struct SetCryptographerError(());
#[cfg(not(feature = "backend-openssl"))]
pub fn set_boxed_cryptographer(c: Box<dyn Cryptographer>) -> Result<(), SetCryptographerError> {
set_cryptographer(Box::leak(c))
}
pub fn set_cryptographer(c: &'static dyn Cryptographer) -> Result<(), SetCryptographerError> {
CRYPTOGRAPHER.set(c).map_err(|_| SetCryptographerError(()))
}
pub(crate) fn get_cryptographer() -> &'static dyn Cryptographer {
autoinit_crypto();
*CRYPTOGRAPHER
.get()
.expect("`rust-ece` cryptographer not initialized!")
}
#[cfg(feature = "backend-openssl")]
#[inline]
fn autoinit_crypto() {
let _ = set_cryptographer(&super::openssl::OpensslCryptographer);
}
#[cfg(not(feature = "backend-openssl"))]
#[inline]
fn autoinit_crypto() {}