1#[derive(Clone, Copy, Debug, Eq, PartialEq)]
9pub struct Provider(ProviderKind);
10
11#[derive(Clone, Copy, Debug, Eq, PartialEq)]
12pub(crate) enum ProviderKind {
13 #[cfg(feature = "aws-lc-rs")]
14 AwsLcRs,
15 #[cfg(feature = "boring")]
16 Boring,
17 #[cfg(feature = "ring")]
18 Ring,
19 #[cfg(feature = "rustcrypto")]
20 RustCrypto,
21}
22
23impl Provider {
24 #[cfg(feature = "aws-lc-rs")]
26 pub const AWS_LC_RS: Self = Self(ProviderKind::AwsLcRs);
27
28 #[cfg(feature = "boring")]
30 pub const BORING: Self = Self(ProviderKind::Boring);
31
32 #[cfg(feature = "ring")]
34 pub const RING: Self = Self(ProviderKind::Ring);
35
36 #[cfg(feature = "rustcrypto")]
38 pub const RUSTCRYPTO: Self = Self(ProviderKind::RustCrypto);
39
40 pub const COMPILED: &'static [Self] = &[
42 #[cfg(feature = "aws-lc-rs")]
43 Self::AWS_LC_RS,
44 #[cfg(feature = "boring")]
45 Self::BORING,
46 #[cfg(feature = "ring")]
47 Self::RING,
48 #[cfg(feature = "rustcrypto")]
49 Self::RUSTCRYPTO,
50 ];
51
52 #[must_use]
56 pub const fn build_default() -> Option<Self> {
57 if Self::COMPILED.len() == 1 {
58 Some(Self::COMPILED[0])
59 } else {
60 None
61 }
62 }
63
64 #[must_use]
66 pub const fn name(self) -> &'static str {
67 match self.0 {
68 #[cfg(feature = "aws-lc-rs")]
69 ProviderKind::AwsLcRs => "aws-lc-rs",
70 #[cfg(feature = "boring")]
71 ProviderKind::Boring => "boring",
72 #[cfg(feature = "ring")]
73 ProviderKind::Ring => "ring",
74 #[cfg(feature = "rustcrypto")]
75 ProviderKind::RustCrypto => "rustcrypto",
76 }
77 }
78
79 pub(crate) const fn kind(self) -> ProviderKind {
80 self.0
81 }
82}
83
84#[cfg(test)]
85mod tests {
86 use super::*;
87
88 #[test]
89 fn compiled_providers_match_features() {
90 let expected = &[
92 #[cfg(feature = "aws-lc-rs")]
93 Provider::AWS_LC_RS,
94 #[cfg(feature = "boring")]
95 Provider::BORING,
96 #[cfg(feature = "ring")]
97 Provider::RING,
98 #[cfg(feature = "rustcrypto")]
99 Provider::RUSTCRYPTO,
100 ];
101
102 assert_eq!(Provider::COMPILED, expected);
104
105 assert_eq!(
107 Provider::COMPILED
108 .iter()
109 .map(|provider| provider.name())
110 .collect::<Vec<_>>(),
111 [
112 #[cfg(feature = "aws-lc-rs")]
113 "aws-lc-rs",
114 #[cfg(feature = "boring")]
115 "boring",
116 #[cfg(feature = "ring")]
117 "ring",
118 #[cfg(feature = "rustcrypto")]
119 "rustcrypto",
120 ]
121 );
122
123 assert_eq!(
125 Provider::build_default(),
126 (Provider::COMPILED.len() == 1).then_some(Provider::COMPILED[0])
127 );
128 }
129}