Skip to content

Commit b72c792

Browse files
committed
chore: improve compile-time directive in http client
1 parent 7334120 commit b72c792

4 files changed

Lines changed: 13 additions & 46 deletions

File tree

libdd-http-client/src/backend/hyper_backend.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use http_body_util::BodyExt;
99
use libdd_common::connector::Connector;
1010
use libdd_common::http_common::{self, Body};
1111

12-
#[cfg(feature = "hyper-backend")]
1312
pub(crate) struct HyperBackend {
1413
client: http_common::GenericHttpClient<Connector>,
1514
transport: TransportConfig,
@@ -23,7 +22,6 @@ impl std::fmt::Debug for HyperBackend {
2322
}
2423
}
2524

26-
#[cfg(feature = "hyper-backend")]
2725
impl HyperBackend {
2826
/// Rewrite the request URL for UDS/Named Pipe transports.
2927
fn rewrite_url(&self, url: &str) -> Result<hyper::Uri, HttpClientError> {
@@ -136,7 +134,6 @@ fn collect_response_headers<T>(
136134
.collect()
137135
}
138136

139-
#[cfg(feature = "hyper-backend")]
140137
fn map_hyper_error(e: hyper_util::client::legacy::Error) -> HttpClientError {
141138
let err = http_common::into_error(e);
142139
match err.kind() {
@@ -146,7 +143,6 @@ fn map_hyper_error(e: hyper_util::client::legacy::Error) -> HttpClientError {
146143
}
147144
}
148145

149-
#[cfg(feature = "hyper-backend")]
150146
impl super::Backend for HyperBackend {
151147
fn new(
152148
_timeout: std::time::Duration,

libdd-http-client/src/backend/reqwest_backend.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@ use crate::{HttpClientError, HttpRequest, HttpResponse};
1111
///
1212
/// Holds a connection-pooling client that is reused across all requests.
1313
#[derive(Debug)]
14-
#[cfg(feature = "reqwest-backend")]
1514
pub(crate) struct ReqwestBackend {
1615
client: reqwest::Client,
1716
}
1817

19-
#[cfg(feature = "reqwest-backend")]
2018
impl super::Backend for ReqwestBackend {
2119
// Creates a `reqwest::Client` with connection pooling enabled.
2220
fn new(
@@ -117,7 +115,6 @@ impl super::Backend for ReqwestBackend {
117115
}
118116

119117
/// Convert our `MultipartPart` list into a `reqwest::multipart::Form`.
120-
#[cfg(feature = "reqwest-backend")]
121118
fn build_multipart_form(
122119
parts: Vec<crate::request::MultipartPart>,
123120
) -> Result<reqwest::multipart::Form, HttpClientError> {
@@ -138,7 +135,6 @@ fn build_multipart_form(
138135
}
139136

140137
/// Map a `reqwest::Error` to our `HttpClientError` variants.
141-
#[cfg(feature = "reqwest-backend")]
142138
fn map_reqwest_error(e: reqwest::Error) -> HttpClientError {
143139
if e.is_timeout() {
144140
HttpClientError::TimedOut

libdd-http-client/src/client.rs

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,24 @@
33

44
//! The public `HttpClient` struct.
55
6+
use crate::backend::Backend;
67
use crate::config::{HttpClientBuilder, HttpClientConfig, TransportConfig};
78
use crate::{HttpClientError, HttpRequest, HttpResponse};
89
use std::time::Duration;
910

10-
#[cfg(any(feature = "reqwest-backend", feature = "hyper-backend"))]
11-
use crate::backend::Backend;
11+
#[cfg(all(feature = "hyper-backend", not(feature = "reqwest-backend")))]
12+
type BackendImpl = crate::backend::hyper_backend::HyperBackend;
1213

1314
#[cfg(feature = "reqwest-backend")]
14-
use crate::backend::reqwest_backend::ReqwestBackend;
15-
16-
#[cfg(all(feature = "hyper-backend", not(feature = "reqwest-backend")))]
17-
use crate::backend::hyper_backend::HyperBackend;
15+
type BackendImpl = crate::backend::reqwest_backend::ReqwestBackend;
1816

1917
/// A high-level async HTTP client.
2018
///
2119
/// Constructed once and reused across many [`HttpClient::send`] calls. Holds
2220
/// a connection pool internally.
2321
#[derive(Debug)]
2422
pub struct HttpClient {
25-
#[cfg(feature = "reqwest-backend")]
26-
backend: ReqwestBackend,
27-
#[cfg(all(feature = "hyper-backend", not(feature = "reqwest-backend")))]
28-
backend: HyperBackend,
23+
backend: BackendImpl,
2924
config: HttpClientConfig,
3025
}
3126

@@ -51,23 +46,8 @@ impl HttpClient {
5146
config: HttpClientConfig,
5247
transport: TransportConfig,
5348
) -> Result<Self, HttpClientError> {
54-
#[cfg(feature = "reqwest-backend")]
55-
{
56-
let backend = ReqwestBackend::new(config.timeout(), transport)?;
57-
Ok(Self { backend, config })
58-
}
59-
#[cfg(all(feature = "hyper-backend", not(feature = "reqwest-backend")))]
60-
{
61-
let backend = HyperBackend::new(config.timeout(), transport)?;
62-
Ok(Self { backend, config })
63-
}
64-
#[cfg(not(any(feature = "reqwest-backend", feature = "hyper-backend")))]
65-
{
66-
let _ = (config, transport);
67-
Err(HttpClientError::InvalidConfig(
68-
"no backend feature enabled".to_owned(),
69-
))
70-
}
49+
let backend = BackendImpl::new(config.timeout(), transport)?;
50+
Ok(Self { backend, config })
7151
}
7252

7353
/// The client's configuration.
@@ -88,17 +68,7 @@ impl HttpClient {
8868
}
8969

9070
async fn send_once(&self, request: HttpRequest) -> Result<HttpResponse, HttpClientError> {
91-
#[cfg(any(feature = "reqwest-backend", feature = "hyper-backend"))]
92-
{
93-
self.backend.send(request, &self.config).await
94-
}
95-
#[cfg(not(any(feature = "reqwest-backend", feature = "hyper-backend")))]
96-
{
97-
let _ = request;
98-
Err(HttpClientError::InvalidConfig(
99-
"no backend feature enabled".to_owned(),
100-
))
101-
}
71+
self.backend.send(request, &self.config).await
10272
}
10373

10474
async fn send_with_retry(

libdd-http-client/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@
4242
//! # }
4343
//! ```
4444
45+
#[cfg(not(any(feature = "hyper-backend", feature = "reqwest-backend")))]
46+
compile_error!(
47+
"at least one backend feature must be enabled: `reqwest-backend` or `hyper-backend`"
48+
);
49+
4550
pub mod config;
4651

4752
pub(crate) mod backend;

0 commit comments

Comments
 (0)