#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Provider(ProviderKind);
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum ProviderKind {
#[cfg(feature = "aws-lc-rs")]
AwsLcRs,
#[cfg(feature = "boring")]
Boring,
#[cfg(feature = "ring")]
Ring,
#[cfg(feature = "rustcrypto")]
RustCrypto,
}
impl Provider {
#[cfg(feature = "aws-lc-rs")]
pub const AWS_LC_RS: Self = Self(ProviderKind::AwsLcRs);
#[cfg(feature = "boring")]
pub const BORING: Self = Self(ProviderKind::Boring);
#[cfg(feature = "ring")]
pub const RING: Self = Self(ProviderKind::Ring);
#[cfg(feature = "rustcrypto")]
pub const RUSTCRYPTO: Self = Self(ProviderKind::RustCrypto);
pub const COMPILED: &'static [Self] = &[
#[cfg(feature = "aws-lc-rs")]
Self::AWS_LC_RS,
#[cfg(feature = "boring")]
Self::BORING,
#[cfg(feature = "ring")]
Self::RING,
#[cfg(feature = "rustcrypto")]
Self::RUSTCRYPTO,
];
#[must_use]
pub const fn build_default() -> Option<Self> {
if Self::COMPILED.len() == 1 {
Some(Self::COMPILED[0])
} else {
None
}
}
#[must_use]
pub const fn name(self) -> &'static str {
match self.0 {
#[cfg(feature = "aws-lc-rs")]
ProviderKind::AwsLcRs => "aws-lc-rs",
#[cfg(feature = "boring")]
ProviderKind::Boring => "boring",
#[cfg(feature = "ring")]
ProviderKind::Ring => "ring",
#[cfg(feature = "rustcrypto")]
ProviderKind::RustCrypto => "rustcrypto",
}
}
pub(crate) const fn kind(self) -> ProviderKind {
self.0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn compiled_providers_match_features() {
let expected = &[
#[cfg(feature = "aws-lc-rs")]
Provider::AWS_LC_RS,
#[cfg(feature = "boring")]
Provider::BORING,
#[cfg(feature = "ring")]
Provider::RING,
#[cfg(feature = "rustcrypto")]
Provider::RUSTCRYPTO,
];
assert_eq!(Provider::COMPILED, expected);
assert_eq!(
Provider::COMPILED
.iter()
.map(|provider| provider.name())
.collect::<Vec<_>>(),
[
#[cfg(feature = "aws-lc-rs")]
"aws-lc-rs",
#[cfg(feature = "boring")]
"boring",
#[cfg(feature = "ring")]
"ring",
#[cfg(feature = "rustcrypto")]
"rustcrypto",
]
);
assert_eq!(
Provider::build_default(),
(Provider::COMPILED.len() == 1).then_some(Provider::COMPILED[0])
);
}
}