Skip to content

Commit 11db0e2

Browse files
committed
style(macros): fmt code
1 parent 1818e68 commit 11db0e2

File tree

5 files changed

+164
-183
lines changed

5 files changed

+164
-183
lines changed

src/emulate/profile.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,22 @@ use super::{
2828
Emulation, Platform,
2929
compress::{BrotliCompressor, ZlibCompressor, ZstdCompressor},
3030
};
31+
32+
pub(super) fn build_standard_emulation(
33+
group: &'static str,
34+
tls_options: TlsOptions,
35+
http2_options: Option<Http2Options>,
36+
default_headers: Option<HeaderMap>,
37+
) -> wreq::Emulation {
38+
let mut builder = wreq::Emulation::builder().tls_options(tls_options);
39+
40+
if let Some(http2_options) = http2_options {
41+
builder = builder.http2_options(http2_options);
42+
}
43+
44+
if let Some(headers) = default_headers {
45+
builder = builder.headers(headers);
46+
}
47+
48+
builder.build(Group::named(group))
49+
}

src/emulate/profile/firefox.rs

Lines changed: 21 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -17,80 +17,36 @@ macro_rules! mod_generator {
1717
$header_initializer:ident,
1818
[($default_os:ident, $default_ua:tt) $(, ($other_os:ident, $other_ua:tt))*]
1919
) => {
20-
pub(crate) mod $mod_name {
21-
use super::*;
22-
23-
pub fn emulation(emulation: Emulation) -> wreq::Emulation {
24-
let default_headers = if emulation.headers {
25-
#[allow(unreachable_patterns)]
26-
let default_headers = match emulation.platform {
27-
$(
28-
Platform::$other_os => {
29-
$header_initializer($other_ua)
30-
}
31-
),*
32-
_ => {
33-
$header_initializer($default_ua)
34-
}
35-
};
36-
37-
Some(default_headers)
38-
} else {
39-
None
40-
};
41-
42-
build_emulation(emulation, default_headers)
20+
standard_mod_generator!(
21+
$mod_name,
22+
$tls_options,
23+
$http2_options,
24+
|emulation: &Emulation| {
25+
firefox_platform_headers!(
26+
emulation,
27+
$header_initializer,
28+
[($default_os, $default_ua) $(, ($other_os, $other_ua))*]
29+
)
4330
}
44-
45-
pub fn build_emulation(
46-
emulation: Emulation,
47-
default_headers: Option<HeaderMap>
48-
) -> wreq::Emulation {
49-
let mut builder = wreq::Emulation::builder().tls_options($tls_options);
50-
51-
if emulation.http2 {
52-
builder = builder.http2_options($http2_options);
53-
}
54-
55-
if let Some(headers) = default_headers {
56-
builder = builder.headers(headers);
57-
}
58-
59-
builder.build(Group::named(stringify!($mod_name)))
60-
}
61-
}
31+
);
6232
};
6333
(
6434
$mod_name:ident,
6535
$build_emulation:expr,
6636
$header_initializer:ident,
6737
[($default_os:ident, $default_ua:tt) $(, ($other_os:ident, $other_ua:tt))*]
6838
) => {
69-
pub(crate) mod $mod_name {
70-
use super::*;
71-
72-
pub fn emulation(emulation: Emulation) -> wreq::Emulation {
73-
let default_headers = if emulation.headers {
74-
#[allow(unreachable_patterns)]
75-
let default_headers = match emulation.platform {
76-
$(
77-
Platform::$other_os => {
78-
$header_initializer($other_ua)
79-
}
80-
),*
81-
_ => {
82-
$header_initializer($default_ua)
83-
}
84-
};
85-
86-
Some(default_headers)
87-
} else {
88-
None
89-
};
90-
91-
$build_emulation(emulation, default_headers)
39+
standard_mod_generator!(
40+
$mod_name,
41+
$build_emulation,
42+
|emulation: &Emulation| {
43+
firefox_platform_headers!(
44+
emulation,
45+
$header_initializer,
46+
[($default_os, $default_ua) $(, ($other_os, $other_ua))*]
47+
)
9248
}
93-
}
49+
);
9450
};
9551
}
9652

src/emulate/profile/macros.rs

Lines changed: 96 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -97,93 +97,128 @@ macro_rules! join {
9797
};
9898
}
9999

100-
macro_rules! mod_generator {
101-
(
102-
$mod_name:ident,
103-
$tls_options:expr,
104-
$http2_options:expr,
105-
$header_initializer:ident,
106-
[($default_os:ident, $default_sec_ch_ua:tt, $default_ua:tt) $(, ($other_os:ident, $other_sec_ch_ua:tt, $other_ua:tt))*]
107-
) => {
100+
macro_rules! standard_mod_generator {
101+
($mod_name:ident, $tls_options:expr, $http2_options:expr, $headers:expr) => {
108102
pub(crate) mod $mod_name {
109103
use super::*;
110104

111105
pub fn emulation(emulation: Emulation) -> wreq::Emulation {
112106
let default_headers = if emulation.headers {
113-
#[allow(unreachable_patterns)]
114-
let default_headers = match emulation.platform {
115-
$(
116-
Platform::$other_os => $header_initializer(
117-
$other_sec_ch_ua,
118-
$other_ua,
119-
emulation.platform,
120-
),
121-
)*
122-
_ => $header_initializer(
123-
$default_sec_ch_ua,
124-
$default_ua,
125-
Platform::$default_os,
126-
),
127-
};
128-
Some(default_headers)
107+
($headers)(&emulation)
129108
} else {
130109
None
131110
};
132111

133-
build_emulation(emulation, default_headers)
112+
build_emulation(emulation.http2, default_headers)
134113
}
135114

136115
pub fn build_emulation(
137-
emulation: Emulation,
138-
default_headers: Option<HeaderMap>
116+
http2: bool,
117+
default_headers: Option<HeaderMap>,
139118
) -> wreq::Emulation {
140-
let mut builder = wreq::Emulation::builder().tls_options($tls_options);
141-
142-
if emulation.http2 {
143-
builder = builder.http2_options($http2_options);
144-
}
145-
146-
if let Some(headers) = default_headers {
147-
builder = builder.headers(headers);
148-
}
149-
150-
builder.build(Group::named(stringify!($mod_name)))
119+
build_standard_emulation(
120+
stringify!($mod_name),
121+
$tls_options,
122+
http2.then(|| $http2_options),
123+
default_headers,
124+
)
151125
}
152126
}
153127
};
154-
(
155-
$mod_name:ident,
156-
$build_emulation:expr,
157-
$header_initializer:ident,
158-
[($default_os:ident, $default_sec_ch_ua:tt, $default_ua:tt) $(, ($other_os:ident, $other_sec_ch_ua:tt, $other_ua:tt))*]
159-
) => {
128+
($mod_name:ident, $build_emulation:expr, $headers:expr) => {
160129
pub(crate) mod $mod_name {
161130
use super::*;
162131

163132
pub fn emulation(emulation: Emulation) -> wreq::Emulation {
164133
let default_headers = if emulation.headers {
165-
#[allow(unreachable_patterns)]
166-
let default_headers = match emulation.platform {
167-
$(
168-
Platform::$other_os => $header_initializer(
169-
$other_sec_ch_ua,
170-
$other_ua,
171-
emulation.platform,
172-
),
173-
)*
174-
_ => $header_initializer(
175-
$default_sec_ch_ua,
176-
$default_ua,
177-
Platform::$default_os,
178-
),
179-
};
180-
Some(default_headers)
134+
($headers)(&emulation)
181135
} else {
182136
None
183137
};
184138

185-
$build_emulation(emulation, default_headers)
139+
$build_emulation(emulation.http2, default_headers)
186140
}
187141
}
188142
};
189143
}
144+
145+
macro_rules! platform_headers {
146+
(
147+
$emulation:expr,
148+
$header_initializer:ident,
149+
[($default_os:ident, $default_sec_ch_ua:tt, $default_ua:tt) $(, ($other_os:ident, $other_sec_ch_ua:tt, $other_ua:tt))*]
150+
) => {{
151+
#[allow(unreachable_patterns)]
152+
let default_headers = match $emulation.platform {
153+
$(
154+
Platform::$other_os => $header_initializer(
155+
$other_sec_ch_ua,
156+
$other_ua,
157+
$emulation.platform,
158+
),
159+
)*
160+
_ => $header_initializer(
161+
$default_sec_ch_ua,
162+
$default_ua,
163+
Platform::$default_os,
164+
),
165+
};
166+
167+
Some(default_headers)
168+
}};
169+
}
170+
171+
macro_rules! firefox_platform_headers {
172+
($emulation:expr, $header_initializer:ident, [($default_os:ident, $default_ua:tt) $(, ($other_os:ident, $other_ua:tt))*]) => {{
173+
#[allow(unreachable_patterns)]
174+
let default_headers = match $emulation.platform {
175+
$(
176+
Platform::$other_os => $header_initializer($other_ua),
177+
)*
178+
_ => $header_initializer($default_ua),
179+
};
180+
181+
Some(default_headers)
182+
}};
183+
}
184+
185+
macro_rules! mod_generator {
186+
(
187+
$mod_name:ident,
188+
$tls_options:expr,
189+
$http2_options:expr,
190+
$header_initializer:ident,
191+
[($default_os:ident, $default_sec_ch_ua:tt, $default_ua:tt) $(, ($other_os:ident, $other_sec_ch_ua:tt, $other_ua:tt))*]
192+
) => {
193+
standard_mod_generator!(
194+
$mod_name,
195+
$tls_options,
196+
$http2_options,
197+
|emulation: &Emulation| {
198+
platform_headers!(
199+
emulation,
200+
$header_initializer,
201+
[($default_os, $default_sec_ch_ua, $default_ua) $(, ($other_os, $other_sec_ch_ua, $other_ua))*]
202+
)
203+
}
204+
);
205+
};
206+
(
207+
$mod_name:ident,
208+
$build_emulation:expr,
209+
$header_initializer:ident,
210+
[($default_os:ident, $default_sec_ch_ua:tt, $default_ua:tt) $(, ($other_os:ident, $other_sec_ch_ua:tt, $other_ua:tt))*]
211+
) => {
212+
standard_mod_generator!(
213+
$mod_name,
214+
$build_emulation,
215+
|emulation: &Emulation| {
216+
platform_headers!(
217+
emulation,
218+
$header_initializer,
219+
[($default_os, $default_sec_ch_ua, $default_ua) $(, ($other_os, $other_sec_ch_ua, $other_ua))*]
220+
)
221+
}
222+
);
223+
};
224+
}

src/emulate/profile/okhttp.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,7 @@ fn build_emulation(
7878
cipher_list: &'static str,
7979
user_agent: &'static str,
8080
) -> wreq::Emulation {
81-
let mut builder = wreq::Emulation::builder().tls_options(
82-
OkHttpTlsConfig::builder()
83-
.cipher_list(cipher_list)
84-
.build()
85-
.into(),
86-
);
87-
88-
if emulation.http2 {
81+
let http2_options = if emulation.http2 {
8982
let settings_order = SettingsOrder::builder()
9083
.extend([
9184
SettingId::HeaderTableSize,
@@ -119,10 +112,12 @@ fn build_emulation(
119112
.settings_order(settings_order)
120113
.build();
121114

122-
builder = builder.http2_options(http2_opts);
123-
}
115+
Some(http2_opts)
116+
} else {
117+
None
118+
};
124119

125-
if emulation.headers {
120+
let default_headers = if emulation.headers {
126121
let mut headers = HeaderMap::new();
127122
headers.insert(ACCEPT, HeaderValue::from_static("*/*"));
128123
headers.insert(ACCEPT_LANGUAGE, HeaderValue::from_static("en-US,en;q=0.9"));
@@ -132,10 +127,20 @@ fn build_emulation(
132127
ACCEPT_ENCODING,
133128
HeaderValue::from_static("gzip, deflate, br"),
134129
);
135-
builder = builder.headers(headers);
136-
}
130+
Some(headers)
131+
} else {
132+
None
133+
};
137134

138-
builder.build(Group::named(group))
135+
build_standard_emulation(
136+
group,
137+
OkHttpTlsConfig::builder()
138+
.cipher_list(cipher_list)
139+
.build()
140+
.into(),
141+
http2_options,
142+
default_headers,
143+
)
139144
}
140145

141146
mod_generator!(

0 commit comments

Comments
 (0)