1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//! TLS backend selection and `rustls` crypto-provider installation.
//!
//! `deribit-websocket` supports three mutually-exclusive TLS backends,
//! selected at compile time via Cargo features (see this crate's
//! `Cargo.toml` or the README):
//!
//! - `rustls-aws-lc` (default) — `rustls` with the `aws-lc-rs` crypto
//! provider, OS-native root store.
//! - `rustls-ring` — `rustls` with the `ring` crypto provider, OS-native
//! root store.
//! - `native-tls` — OS-native TLS stack (SChannel / SecureTransport /
//! OpenSSL); no `rustls` dependency is pulled in.
//!
//! Exactly one of the three must be active. The [`compile_error!`]
//! gates below reject any other combination — zero or two or three — at
//! compile time so misconfigured builds fail fast rather than at
//! runtime during the first WebSocket handshake.
//!
//! # Crypto provider
//!
//! The two `rustls-*` backends rely on `rustls`'s process-global crypto
//! provider slot being populated before any TLS handshake starts.
//! Applications must call [`install_default_crypto_provider`] once at
//! startup; the helper picks the right provider for the active feature
//! and is a no-op under `native-tls`.
// ---------------------------------------------------------------------
// Compile-time mutex — exactly one TLS backend.
// ---------------------------------------------------------------------
compile_error!;
compile_error!;
// ---------------------------------------------------------------------
// Public API — crypto-provider installation.
// ---------------------------------------------------------------------
/// Errors raised when installing the `rustls` crypto provider.
///
/// Separate from [`crate::error::WebSocketError`] because this is a
/// one-shot startup concern distinct from runtime protocol errors;
/// folding it into `WebSocketError` would bloat the main enum for a
/// call that runs once per process.
/// Install the process-global `rustls` crypto provider matching the
/// active TLS feature.
///
/// - Under `rustls-aws-lc`, installs
/// `rustls::crypto::aws_lc_rs::default_provider()`.
/// - Under `rustls-ring`, installs
/// `rustls::crypto::ring::default_provider()`.
/// - Under `native-tls`, this is a no-op that returns `Ok(())` — the
/// OS TLS stack does not require any process-level initialization.
///
/// Call this exactly once at application startup, before any call to
/// [`crate::client::DeribitWebSocketClient::connect`]. Subsequent calls
/// return [`CryptoProviderError::AlreadyInstalled`] rather than panic, which
/// lets callers be robust against multiple entry points (tests, libs,
/// `main`) all trying to initialize the provider.
///
/// # Errors
///
/// Returns [`CryptoProviderError::AlreadyInstalled`] if a provider is
/// already installed in this process by this call, a previous call, or
/// any other library that uses `rustls`.
///
/// # Examples
///
/// ```no_run
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // Idempotent: subsequent calls return AlreadyInstalled but are
/// // otherwise safe.
/// let _ = deribit_websocket::install_default_crypto_provider();
/// # Ok(())
/// # }
/// ```
// `Result` already carries `#[must_use]`, so an extra attribute on the
// function itself would be redundant (see `clippy::double_must_use`).
// The explicit `return`s below are load-bearing: without them the
// function body would have multiple `#[cfg]`-gated tail expressions and
// fail to type-check when more than one TLS feature is active — the
// very case the `compile_error!` mutex above is meant to reject
// cleanly. Allowing `needless_return` keeps the diagnostic limited to
// the mutex message alone.
// ---------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------