Skip to content

Commit e63d552

Browse files
djcrami3l
authored andcommitted
download: clean up TLS feature guards
1 parent fb572dd commit e63d552

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

download/src/lib.rs

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -160,17 +160,39 @@ impl Backend {
160160
#[cfg(feature = "curl-backend")]
161161
Self::Curl => curl::download(url, resume_from, callback),
162162
#[cfg(any(feature = "reqwest-rustls-tls", feature = "reqwest-native-tls"))]
163-
Self::Reqwest(tls) => reqwest_be::download(url, resume_from, callback, tls).await,
163+
Self::Reqwest(tls) => tls.download(url, resume_from, callback).await,
164164
}
165165
}
166166
}
167167

168+
#[cfg(any(feature = "reqwest-rustls-tls", feature = "reqwest-native-tls"))]
168169
#[derive(Debug, Copy, Clone)]
169170
pub enum TlsBackend {
171+
#[cfg(feature = "reqwest-rustls-tls")]
170172
Rustls,
173+
#[cfg(feature = "reqwest-native-tls")]
171174
NativeTls,
172175
}
173176

177+
#[cfg(any(feature = "reqwest-rustls-tls", feature = "reqwest-native-tls"))]
178+
impl TlsBackend {
179+
async fn download(
180+
self,
181+
url: &Url,
182+
resume_from: u64,
183+
callback: DownloadCallback<'_>,
184+
) -> Result<()> {
185+
let client = match self {
186+
#[cfg(feature = "reqwest-rustls-tls")]
187+
Self::Rustls => &reqwest_be::CLIENT_RUSTLS_TLS,
188+
#[cfg(feature = "reqwest-native-tls")]
189+
Self::NativeTls => &reqwest_be::CLIENT_NATIVE_TLS,
190+
};
191+
192+
reqwest_be::download(url, resume_from, callback, client).await
193+
}
194+
}
195+
174196
#[derive(Debug, Copy, Clone)]
175197
pub enum Event<'a> {
176198
ResumingPartialDownload,
@@ -298,12 +320,6 @@ pub mod curl {
298320

299321
#[cfg(any(feature = "reqwest-rustls-tls", feature = "reqwest-native-tls"))]
300322
pub mod reqwest_be {
301-
#[cfg(all(
302-
not(feature = "reqwest-rustls-tls"),
303-
not(feature = "reqwest-native-tls")
304-
))]
305-
compile_error!("Must select a reqwest TLS backend");
306-
307323
use std::io;
308324
#[cfg(feature = "reqwest-rustls-tls")]
309325
use std::sync::Arc;
@@ -320,20 +336,20 @@ pub mod reqwest_be {
320336
use tokio_stream::StreamExt;
321337
use url::Url;
322338

323-
use super::{DownloadError, Event, TlsBackend};
339+
use super::{DownloadError, Event};
324340

325341
pub async fn download(
326342
url: &Url,
327343
resume_from: u64,
328344
callback: &dyn Fn(Event<'_>) -> Result<()>,
329-
tls: TlsBackend,
345+
client: &Client,
330346
) -> Result<()> {
331347
// Short-circuit reqwest for the "file:" URL scheme
332348
if download_from_file_url(url, resume_from, callback)? {
333349
return Ok(());
334350
}
335351

336-
let res = request(url, resume_from, tls)
352+
let res = request(url, resume_from, client)
337353
.await
338354
.context("failed to make network request")?;
339355

@@ -367,7 +383,7 @@ pub mod reqwest_be {
367383
}
368384

369385
#[cfg(feature = "reqwest-rustls-tls")]
370-
static CLIENT_RUSTLS_TLS: LazyLock<Client> = LazyLock::new(|| {
386+
pub(super) static CLIENT_RUSTLS_TLS: LazyLock<Client> = LazyLock::new(|| {
371387
let catcher = || {
372388
client_generic()
373389
.use_preconfigured_tls(
@@ -393,7 +409,7 @@ pub mod reqwest_be {
393409
});
394410

395411
#[cfg(feature = "reqwest-native-tls")]
396-
static CLIENT_DEFAULT_TLS: LazyLock<Client> = LazyLock::new(|| {
412+
pub(super) static CLIENT_NATIVE_TLS: LazyLock<Client> = LazyLock::new(|| {
397413
let catcher = || {
398414
client_generic()
399415
.user_agent(super::REQWEST_DEFAULT_TLS_USER_AGENT)
@@ -416,22 +432,8 @@ pub mod reqwest_be {
416432
async fn request(
417433
url: &Url,
418434
resume_from: u64,
419-
backend: TlsBackend,
435+
client: &Client,
420436
) -> Result<Response, DownloadError> {
421-
let client: &Client = match backend {
422-
#[cfg(feature = "reqwest-rustls-tls")]
423-
TlsBackend::Rustls => &CLIENT_RUSTLS_TLS,
424-
#[cfg(not(feature = "reqwest-rustls-tls"))]
425-
TlsBackend::Rustls => {
426-
return Err(DownloadError::BackendUnavailable("reqwest rustls"));
427-
}
428-
#[cfg(feature = "reqwest-native-tls")]
429-
TlsBackend::NativeTls => &CLIENT_DEFAULT_TLS,
430-
#[cfg(not(feature = "reqwest-native-tls"))]
431-
TlsBackend::NativeTls => {
432-
return Err(DownloadError::BackendUnavailable("reqwest default TLS"));
433-
}
434-
};
435437
let mut req = client.get(url.as_str());
436438

437439
if resume_from != 0 {

0 commit comments

Comments
 (0)