Skip to content

Commit 69f9298

Browse files
committed
style: simplify cfg flags
1 parent 8b2cbcf commit 69f9298

4 files changed

Lines changed: 60 additions & 100 deletions

File tree

bitreq/src/client.rs

Lines changed: 16 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,47 +9,32 @@
99
use std::collections::{hash_map, HashMap, VecDeque};
1010
use std::sync::{Arc, Mutex};
1111

12-
#[cfg(any(
13-
all(feature = "native-tls", feature = "tokio-native-tls"),
14-
all(feature = "rustls", feature = "tokio-rustls")
15-
))]
12+
#[cfg(any(feature = "tokio-rustls", feature = "tokio-native-tls"))]
1613
use crate::connection::certificates::{Certificates, CertificatesBuilder};
1714
use crate::connection::AsyncConnection;
1815
use crate::request::{OwnedConnectionParams as ConnectionKey, ParsedRequest};
1916
use crate::{Error, Request, Response};
2017

2118
#[derive(Clone)]
2219
pub(crate) struct ClientConfig {
23-
#[cfg(any(
24-
all(feature = "native-tls", feature = "tokio-native-tls"),
25-
all(feature = "rustls", feature = "tokio-rustls")
26-
))]
20+
#[cfg(any(feature = "tokio-rustls", feature = "tokio-native-tls"))]
2721
pub(crate) tls: Option<TlsConfig>,
2822
}
2923

30-
#[cfg(any(
31-
all(feature = "native-tls", feature = "tokio-native-tls"),
32-
all(feature = "rustls", feature = "tokio-rustls")
33-
))]
24+
#[cfg(any(feature = "tokio-rustls", feature = "tokio-native-tls"))]
3425
#[derive(Clone)]
3526
pub(crate) struct TlsConfig {
3627
pub(crate) certificates: Certificates,
3728
}
3829

39-
#[cfg(any(
40-
all(feature = "native-tls", feature = "tokio-native-tls"),
41-
all(feature = "rustls", feature = "tokio-rustls")
42-
))]
30+
#[cfg(any(feature = "tokio-rustls", feature = "tokio-native-tls"))]
4331
impl TlsConfig {
4432
fn new(certificates: Certificates) -> Self { Self { certificates } }
4533
}
4634

4735
pub struct ClientBuilder {
4836
capacity: usize,
49-
#[cfg(any(
50-
all(feature = "native-tls", feature = "tokio-native-tls"),
51-
all(feature = "rustls", feature = "tokio-rustls")
52-
))]
37+
#[cfg(any(feature = "tokio-rustls", feature = "tokio-native-tls"))]
5338
certificates: Option<CertificatesBuilder>,
5439
}
5540

@@ -71,29 +56,22 @@ pub struct ClientBuilder {
7156
/// ```
7257
impl ClientBuilder {
7358
/// Creates a new `ClientBuilder` with a default pool capacity of 10.
74-
#[cfg(any(
75-
all(feature = "native-tls", feature = "tokio-native-tls"),
76-
all(feature = "rustls", feature = "tokio-rustls")
77-
))]
78-
pub fn new() -> Self { Self { capacity: 10, certificates: None } }
59+
pub fn new() -> Self {
60+
Self {
61+
capacity: 10,
7962

80-
/// Creates a new `ClientBuilder` with a default pool capacity of 10.
81-
#[cfg(not(any(
82-
all(feature = "native-tls", feature = "tokio-native-tls"),
83-
all(feature = "rustls", feature = "tokio-rustls")
84-
)))]
85-
pub fn new() -> Self { Self { capacity: 10 } }
63+
#[cfg(any(feature = "tokio-rustls", feature = "tokio-native-tls"))]
64+
certificates: None,
65+
}
66+
}
8667

8768
/// Sets the maximum number of connections to keep in the pool.
8869
pub fn with_capacity(mut self, capacity: usize) -> Self {
8970
self.capacity = capacity;
9071
self
9172
}
9273

93-
#[cfg(any(
94-
all(feature = "native-tls", feature = "tokio-native-tls"),
95-
all(feature = "rustls", feature = "tokio-rustls")
96-
))]
74+
#[cfg(any(feature = "tokio-rustls", feature = "tokio-native-tls"))]
9775
/// Builds the `Client` with the configured settings.
9876
pub fn build(self) -> Result<Client, Error> {
9977
let build_config = if let Some(builder) = self.certificates {
@@ -116,10 +94,7 @@ impl ClientBuilder {
11694
}
11795

11896
/// Builds the `Client` with the configured settings.
119-
#[cfg(not(any(
120-
all(feature = "native-tls", feature = "tokio-native-tls"),
121-
all(feature = "rustls", feature = "tokio-rustls")
122-
)))]
97+
#[cfg(not(any(feature = "tokio-rustls", feature = "tokio-native-tls")))]
12398
pub fn build(self) -> Result<Client, Error> {
12499
Ok(Client {
125100
r#async: Arc::new(Mutex::new(ClientImpl {
@@ -149,10 +124,7 @@ impl ClientBuilder {
149124
/// # Ok(())
150125
/// # }
151126
/// ```
152-
#[cfg(any(
153-
all(feature = "native-tls", feature = "tokio-native-tls"),
154-
all(feature = "rustls", feature = "tokio-rustls")
155-
))]
127+
#[cfg(any(feature = "tokio-rustls", feature = "tokio-native-tls"))]
156128
pub fn with_root_certificate<T: Into<Vec<u8>>>(mut self, cert_der: T) -> Result<Self, Error> {
157129
let cert_der = cert_der.into();
158130
if let Some(ref mut certificates) = self.certificates {
@@ -167,10 +139,7 @@ impl ClientBuilder {
167139

168140
/// Disables default root certificates for TLS connections.
169141
/// Returns [`Error::InvalidTlsConfig`] if TLS has not been configured.
170-
#[cfg(any(
171-
all(feature = "native-tls", feature = "tokio-native-tls"),
172-
all(feature = "rustls", feature = "tokio-rustls")
173-
))]
142+
#[cfg(any(feature = "tokio-rustls", feature = "tokio-native-tls"))]
174143
pub fn disable_default_certificates(mut self) -> Result<Self, Error> {
175144
match self.certificates {
176145
Some(ref mut certificates) => certificates.disable_default()?,

bitreq/src/connection.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ use crate::{Error, Method, ResponseLazy};
3131

3232
type UnsecuredStream = TcpStream;
3333

34-
#[cfg(any(
35-
all(feature = "native-tls", feature = "tokio-native-tls"),
36-
all(feature = "rustls", feature = "tokio-rustls")
37-
))]
34+
#[cfg(any(feature = "tokio-rustls", feature = "tokio-native-tls"))]
3835
pub(crate) mod certificates;
3936
#[cfg(any(feature = "rustls", feature = "native-tls"))]
4037
mod rustls_stream;
Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
1-
#[cfg(any(feature = "rustls", feature = "native-tls"))]
21
use std::sync::Arc;
32

4-
#[cfg(all(feature = "native-tls", not(feature = "rustls")))]
3+
#[cfg(not(feature = "rustls"))]
54
use native_tls::{Certificate, TlsConnector, TlsConnectorBuilder};
65
#[cfg(feature = "rustls")]
76
use rustls::RootCertStore;
8-
#[cfg(all(feature = "native-tls", not(feature = "rustls"), feature = "tokio-native-tls"))]
7+
#[cfg(not(feature = "rustls"))]
98
use tokio_native_tls::TlsConnector as AsyncTlsConnector;
109
#[cfg(feature = "rustls-webpki")]
1110
use webpki_roots::TLS_SERVER_ROOTS;
1211

1312
use crate::Error;
1413

15-
#[cfg(all(feature = "rustls", feature = "tokio-rustls"))]
14+
#[cfg(feature = "rustls")]
1615
pub(crate) struct CertificatesBuilder {
1716
pub(crate) inner: RootCertStore,
1817
pub(crate) disable_default: bool,
1918
}
2019

21-
#[cfg(all(feature = "native-tls", not(feature = "rustls"), feature = "tokio-native-tls"))]
20+
#[cfg(not(feature = "rustls"))]
2221
pub(crate) struct CertificatesBuilder {
2322
pub(crate) inner: TlsConnectorBuilder,
2423
}
2524

25+
#[cfg(feature = "rustls")]
2626
impl CertificatesBuilder {
27-
#[cfg(all(feature = "rustls", feature = "tokio-rustls"))]
2827
pub(crate) fn new(cert_der: Option<Vec<u8>>) -> Result<Self, Error> {
2928
let mut certificates = Self { inner: RootCertStore::empty(), disable_default: false };
3029

@@ -35,42 +34,12 @@ impl CertificatesBuilder {
3534
Ok(certificates)
3635
}
3736

38-
#[cfg(all(feature = "native-tls", not(feature = "rustls"), feature = "tokio-native-tls"))]
39-
pub(crate) fn new(cert_der: Option<Vec<u8>>) -> Result<Self, Error> {
40-
let builder = TlsConnector::builder();
41-
let mut certificates = Self { inner: builder };
42-
43-
if let Some(cert_der) = cert_der {
44-
certificates.append_certificate(cert_der)?;
45-
}
46-
47-
Ok(certificates)
48-
}
49-
50-
#[cfg(all(feature = "rustls", feature = "tokio-rustls"))]
5137
pub(crate) fn append_certificate(&mut self, cert_der: Vec<u8>) -> Result<&mut Self, Error> {
5238
self.inner.add(&rustls::Certificate(cert_der)).map_err(Error::RustlsAppendCert)?;
5339

5440
Ok(self)
5541
}
5642

57-
#[cfg(all(feature = "native-tls", not(feature = "rustls"), feature = "tokio-native-tls"))]
58-
pub(crate) fn append_certificate(&mut self, cert_der: Vec<u8>) -> Result<&mut Self, Error> {
59-
let certificate = Certificate::from_der(&cert_der)?;
60-
self.inner.add_root_certificate(certificate);
61-
62-
Ok(self)
63-
}
64-
65-
#[cfg(all(feature = "native-tls", not(feature = "rustls"), feature = "tokio-native-tls"))]
66-
pub(crate) fn build(self) -> Result<Certificates, Error> {
67-
let connector = self.inner.build()?;
68-
let async_connector = AsyncTlsConnector::from(connector);
69-
70-
Ok(Certificates(Arc::new(async_connector)))
71-
}
72-
73-
#[cfg(all(feature = "rustls", feature = "tokio-rustls"))]
7443
pub(crate) fn build(mut self) -> Result<Certificates, Error> {
7544
if !self.disable_default {
7645
self.with_root_certificates();
@@ -79,7 +48,6 @@ impl CertificatesBuilder {
7948
Ok(Certificates(Arc::new(self.inner)))
8049
}
8150

82-
#[cfg(all(feature = "rustls", feature = "tokio-rustls"))]
8351
fn with_root_certificates(&mut self) -> &mut Self {
8452
// Try to load native certs
8553
#[cfg(feature = "https-rustls-probe")]
@@ -106,23 +74,49 @@ impl CertificatesBuilder {
10674
self
10775
}
10876

109-
#[cfg(all(feature = "rustls", feature = "tokio-rustls"))]
11077
pub(crate) fn disable_default(&mut self) -> Result<&mut Self, Error> {
11178
self.disable_default = true;
11279
Ok(self)
11380
}
81+
}
82+
83+
#[cfg(not(feature = "rustls"))]
84+
impl CertificatesBuilder {
85+
pub(crate) fn new(cert_der: Option<Vec<u8>>) -> Result<Self, Error> {
86+
let builder = TlsConnector::builder();
87+
let mut certificates = Self { inner: builder };
88+
89+
if let Some(cert_der) = cert_der {
90+
certificates.append_certificate(cert_der)?;
91+
}
92+
93+
Ok(certificates)
94+
}
95+
96+
pub(crate) fn append_certificate(&mut self, cert_der: Vec<u8>) -> Result<&mut Self, Error> {
97+
let certificate = Certificate::from_der(&cert_der)?;
98+
self.inner.add_root_certificate(certificate);
99+
100+
Ok(self)
101+
}
102+
103+
pub(crate) fn build(self) -> Result<Certificates, Error> {
104+
let connector = self.inner.build()?;
105+
let async_connector = AsyncTlsConnector::from(connector);
106+
107+
Ok(Certificates(Arc::new(async_connector)))
108+
}
114109

115-
#[cfg(all(feature = "native-tls", not(feature = "rustls"), feature = "tokio-native-tls"))]
116110
pub(crate) fn disable_default(&mut self) -> Result<&mut Self, Error> {
117111
self.inner.disable_built_in_roots(true);
118112
Ok(self)
119113
}
120114
}
121115

122116
#[derive(Clone)]
123-
#[cfg(all(feature = "rustls", feature = "tokio-rustls"))]
117+
#[cfg(feature = "rustls")]
124118
pub(crate) struct Certificates(pub(crate) Arc<RootCertStore>);
125119

126120
#[derive(Clone)]
127-
#[cfg(all(feature = "native-tls", not(feature = "rustls"), feature = "tokio-native-tls"))]
121+
#[cfg(not(feature = "rustls"))]
128122
pub(crate) struct Certificates(pub(crate) Arc<AsyncTlsConnector>);

bitreq/src/connection/rustls_stream.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::sync::OnceLock;
1414
use native_tls::{HandshakeError, TlsConnector, TlsStream};
1515
#[cfg(feature = "rustls")]
1616
use rustls::{self, ClientConfig, ClientConnection, RootCertStore, ServerName, StreamOwned};
17-
#[cfg(all(feature = "native-tls", not(feature = "rustls"), feature = "tokio-native-tls"))]
17+
#[cfg(all(feature = "tokio-native-tls", not(feature = "rustls")))]
1818
use tokio_native_tls::TlsConnector as AsyncTlsConnector;
1919
#[cfg(feature = "tokio-rustls")]
2020
use tokio_rustls::{client::TlsStream, TlsConnector};
@@ -72,7 +72,7 @@ fn build_client_config() -> Arc<ClientConfig> {
7272
Arc::new(config)
7373
}
7474

75-
#[cfg(all(feature = "rustls", feature = "tokio-rustls"))]
75+
#[cfg(feature = "tokio-rustls")]
7676
fn build_rustls_client_config(certificates: Arc<RootCertStore>) -> Arc<ClientConfig> {
7777
let config = ClientConfig::builder()
7878
.with_safe_defaults()
@@ -101,10 +101,10 @@ pub(super) fn wrap_stream(tcp: TcpStream, host: &str) -> Result<HttpStream, Erro
101101

102102
// Async rustls TLS implementation
103103

104-
#[cfg(all(feature = "rustls", feature = "tokio-rustls"))]
104+
#[cfg(feature = "tokio-rustls")]
105105
pub type AsyncSecuredStream = TlsStream<tokio::net::TcpStream>;
106106

107-
#[cfg(all(feature = "rustls", feature = "tokio-rustls"))]
107+
#[cfg(feature = "tokio-rustls")]
108108
pub(super) async fn wrap_async_stream(
109109
tcp: AsyncTcpStream,
110110
host: &str,
@@ -126,7 +126,7 @@ pub(super) async fn wrap_async_stream(
126126
Ok(AsyncHttpStream::Secured(Box::new(tls)))
127127
}
128128

129-
#[cfg(all(feature = "rustls", feature = "tokio-rustls"))]
129+
#[cfg(feature = "tokio-rustls")]
130130
pub(super) async fn wrap_async_stream_with_configs(
131131
tcp: AsyncTcpStream,
132132
host: &str,
@@ -192,10 +192,10 @@ pub(super) fn wrap_stream(tcp: TcpStream, host: &str) -> Result<HttpStream, Erro
192192
Ok(HttpStream::Secured(Box::new(tls), None))
193193
}
194194

195-
#[cfg(all(feature = "native-tls", not(feature = "rustls"), feature = "tokio-native-tls"))]
195+
#[cfg(all(feature = "tokio-native-tls", not(feature = "rustls")))]
196196
pub type AsyncSecuredStream = tokio_native_tls::TlsStream<tokio::net::TcpStream>;
197197

198-
#[cfg(all(feature = "native-tls", not(feature = "rustls"), feature = "tokio-native-tls"))]
198+
#[cfg(all(feature = "tokio-native-tls", not(feature = "rustls")))]
199199
pub(super) async fn wrap_async_stream(
200200
tcp: AsyncTcpStream,
201201
host: &str,
@@ -220,7 +220,7 @@ pub(super) async fn wrap_async_stream(
220220
Ok(AsyncHttpStream::Secured(Box::new(tls)))
221221
}
222222

223-
#[cfg(all(feature = "native-tls", not(feature = "rustls"), feature = "tokio-native-tls"))]
223+
#[cfg(all(feature = "tokio-native-tls", not(feature = "rustls")))]
224224
pub(super) async fn wrap_async_stream_with_configs(
225225
tcp: AsyncTcpStream,
226226
host: &str,

0 commit comments

Comments
 (0)