Skip to content

Commit

Permalink
chore: Macros simplify some debug implement (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
0x676e67 authored Dec 20, 2024
1 parent 65391fb commit 5a92fa5
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 145 deletions.
159 changes: 40 additions & 119 deletions src/client/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ use crate::cookie;
#[cfg(feature = "hickory-dns")]
use crate::dns::hickory::HickoryDnsResolver;
use crate::dns::{gai::GaiResolver, DnsResolverWithOverrides, DynResolver, Resolve};
use crate::error;
use crate::into_url::try_uri;
use crate::redirect::{self, remove_sensitive_headers};
#[cfg(feature = "boring-tls")]
use crate::tls::{self, BoringTlsConnector, Impersonate, ImpersonateSettings, TlsSettings};
use crate::{error, impl_debug};
use crate::{IntoUrl, Method, Proxy, StatusCode, Url};
#[cfg(feature = "hickory-dns")]
use hickory_resolver::config::LookupIpStrategy;
Expand All @@ -53,13 +53,14 @@ use log::{debug, trace};
/// because it already uses an [`Arc`] internally.
///
/// [`Rc`]: std::rc::Rc
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct Client {
inner: Arc<ClientRef>,
}

/// A `ClientBuilder` can be used to create a `Client` with custom configuration.
#[must_use]
#[derive(Debug)]
pub struct ClientBuilder {
config: Config,
}
Expand Down Expand Up @@ -1734,14 +1735,6 @@ impl Client {
}
}

impl fmt::Debug for Client {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut builder = f.debug_struct("Client");
self.inner.fmt_fields(&mut builder);
builder.finish()
}
}

impl tower_service::Service<Request> for Client {
type Response = Response;
type Error = crate::Error;
Expand Down Expand Up @@ -1770,82 +1763,27 @@ impl tower_service::Service<Request> for &'_ Client {
}
}

impl fmt::Debug for ClientBuilder {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut builder = f.debug_struct("ClientBuilder");
self.config.fmt_fields(&mut builder);
builder.finish()
}
}

impl Config {
fn fmt_fields(&self, f: &mut fmt::DebugStruct<'_, '_>) {
// Instead of deriving Debug, only print fields when their output
// would provide relevant or interesting data.

#[cfg(feature = "cookies")]
{
if let Some(_) = self.cookie_store {
f.field("cookie_store", &true);
}
}

f.field("accepts", &self.accepts);

if !self.proxies.is_empty() {
f.field("proxies", &self.proxies);
}

if !self.redirect_policy.is_default() {
f.field("redirect_policy", &self.redirect_policy);
}

if self.referer {
f.field("referer", &true);
}

f.field("default_headers", &self.headers);

if let Some(ref d) = self.connect_timeout {
f.field("connect_timeout", d);
}

if let Some(ref d) = self.timeout {
f.field("timeout", d);
}

if let Some(ref v) = self.local_address_ipv4 {
f.field("local_address_4", v);
}

if let Some(ref v) = self.local_address_ipv6 {
f.field("local_address_6", v);
}

if self.nodelay {
f.field("tcp_nodelay", &true);
}

#[cfg(feature = "boring-tls")]
{
if !self.tls.certs_verification {
f.field("danger_accept_invalid_certs", &true);
}

f.field("tls_info", &self.tls_info);
}

if self.https_only {
f.field("https_only", &true);
}

if !self.dns_overrides.is_empty() {
f.field("dns_overrides", &self.dns_overrides);
}

f.field("builder", &self.builder);
}
}
impl_debug!(
Config,
{
accepts,
headers,
headers_order,
proxies,
redirect_policy,
accepts,
referer,
timeout,
connect_timeout,
https_only,
nodelay,
local_address_ipv4,
local_address_ipv6,
dns_overrides,
base_url,
builder
}
);

#[derive(Clone)]
struct ClientRef {
Expand All @@ -1863,40 +1801,23 @@ struct ClientRef {
base_url: Option<Arc<Url>>,
}

impl ClientRef {
fn fmt_fields(&self, f: &mut fmt::DebugStruct<'_, '_>) {
// Instead of deriving Debug, only print fields when their output
// would provide relevant or interesting data.

#[cfg(feature = "cookies")]
{
if let Some(_) = self.cookie_store {
f.field("cookie_store", &true);
}
}

f.field("accepts", &self.accepts);

let proxies = self.hyper.get_proxies();
if !proxies.is_empty() {
f.field("proxies", &proxies);
}

if !self.redirect.is_default() {
f.field("redirect_policy", &self.redirect);
}

if self.referer {
f.field("referer", &true);
}

f.field("default_headers", &self.headers);

if let Some(ref d) = self.request_timeout {
f.field("timeout", d);
}
}
impl_debug!(
ClientRef,
{
accepts,
headers,
headers_order,
hyper,
redirect,
referer,
request_timeout,
https_only,
proxies_maybe_http_auth,
base_url
}
);

impl ClientRef {
#[inline]
fn proxy_auth(&self, dst: &Uri, headers: &mut HeaderMap) {
if !self.proxies_maybe_http_auth {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ mod connect;
#[cfg(feature = "cookies")]
pub mod cookie;
pub mod dns;
mod macros;
mod proxy;
pub mod redirect;
#[cfg(feature = "boring-tls")]
Expand Down
15 changes: 15 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// Macro to implement Debug for a type, skipping certain fields.
#[macro_export]
macro_rules! impl_debug {
($type:ty, { $($field_name:ident),* }) => {
impl std::fmt::Debug for $type {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut debug_struct = f.debug_struct(stringify!($type));
$(
debug_struct.field(stringify!($field_name), &self.$field_name);
)*
debug_struct.finish()
}
}
}
}
4 changes: 0 additions & 4 deletions src/redirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,6 @@ impl Policy {
})
.inner
}

pub(crate) fn is_default(&self) -> bool {
matches!(self.inner, PolicyKind::Limit(10))
}
}

impl Default for Policy {
Expand Down
24 changes: 2 additions & 22 deletions src/tls/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use std::borrow::Cow;

use crate::{
impl_debug,
tls::{cert_compression::CertCompressionAlgorithm, TlsVersion},
HttpVersionPref,
};
Expand Down Expand Up @@ -192,24 +193,7 @@ pub struct TlsSettings {
pub extension_permutation: Option<Cow<'static, [ExtensionType]>>,
}

macro_rules! impl_debug_for_tls {
($type:ty, { $($field_name:ident),* }, { $($skip_field_name:ident),* }) => {
impl std::fmt::Debug for $type {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut debug_struct = f.debug_struct(stringify!($type));
$(
debug_struct.field(stringify!($field_name), &self.$field_name);
)*
$(
let _ = &self.$skip_field_name;
)*
debug_struct.finish()
}
}
}
}

impl_debug_for_tls!(
impl_debug!(
TlsSettings,
{
certs_verification,
Expand All @@ -232,10 +216,6 @@ impl_debug_for_tls!(
record_size_limit,
key_shares_length_limit,
psk_skip_session_ticket
},
{
connector,
root_certs_store
}
);

Expand Down

0 comments on commit 5a92fa5

Please sign in to comment.