Skip to content

Commit

Permalink
perf(emulation): Improve and reduce macro generated code templates (#9)
Browse files Browse the repository at this point in the history
* perf(emulation): Improve and reduce macro generated code templates

* update ci.yml
  • Loading branch information
0x676e67 authored Mar 7, 2025
1 parent e9262b8 commit 8182bf6
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 74 deletions.
14 changes: 13 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,23 @@ jobs:
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
override: true
- name: Style check
run: cargo fmt --all -- --check
- name: Clippy check
run: cargo clippy --all-targets --all-features
- name: Install macros expand
run: cargo install cargo-expand
- name: Expand emulation
run: cargo expand emulation --all-features
- name: Expand emulation chrome
run: cargo expand emulation::device::chrome --all-features
- name: Expand emulation firefox
run: cargo expand emulation::device::firefox --all-features
- name: Expand emulation safari
run: cargo expand emulation::device::safari --all-features
- name: Expand emulation okhttp
run: cargo expand emulation::device::okhttp --all-features

docs:
name: Docs
Expand Down
53 changes: 26 additions & 27 deletions src/emulation/device/chrome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,32 @@ macro_rules! mod_generator {

#[inline(always)]
pub fn emulation(option: EmulationOption) -> EmulationProvider {
#[allow(unreachable_patterns)]
match option.emulation_os {
$(
EmulationOS::$other_os => EmulationProvider::builder()
.tls_config($tls_config)
.http2_config(conditional_http2!(option.skip_http2, $http2_config))
.default_headers(conditional_headers!(option.skip_headers, || {
$header_initializer(
$other_sec_ch_ua,
$other_ua,
EmulationOS::$other_os,
)
}))
.build(),
)*
_ => EmulationProvider::builder()
.tls_config($tls_config)
.http2_config(conditional_http2!(option.skip_http2, $http2_config))
.default_headers(conditional_headers!(option.skip_headers, || {
$header_initializer(
$default_sec_ch_ua,
$default_ua,
EmulationOS::$default_os,
)
}))
.build(),
}
let default_headers = if !option.skip_headers {
#[allow(unreachable_patterns)]
let default_headers = match option.emulation_os {
$(
EmulationOS::$other_os => $header_initializer(
$other_sec_ch_ua,
$other_ua,
option.emulation_os,
),
)*
_ => $header_initializer(
$default_sec_ch_ua,
$default_ua,
EmulationOS::$default_os,
),
};
Some(default_headers)
} else {
None
};

EmulationProvider::builder()
.tls_config($tls_config)
.http2_config(conditional_http2!(option.skip_http2, $http2_config))
.default_headers(default_headers)
.build()
}
}
};
Expand Down
40 changes: 22 additions & 18 deletions src/emulation/device/firefox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,29 @@ macro_rules! mod_generator {

#[inline(always)]
pub fn emulation(option: EmulationOption) -> EmulationProvider {
#[allow(unreachable_patterns)]
match option.emulation_os {
$(
EmulationOS::$other_os => {
EmulationProvider::builder()
.tls_config($tls_config)
.http2_config(conditional_http2!(option.skip_http2, $http2_config))
.default_headers(conditional_headers!(option.skip_headers, $header_initializer, $other_ua))
.build()
let default_headers = if !option.skip_headers {
#[allow(unreachable_patterns)]
let default_headers = match option.emulation_os {
$(
EmulationOS::$other_os => {
$header_initializer($other_ua)
}
),*
_ => {
$header_initializer($default_ua)
}
),*
_ => {
EmulationProvider::builder()
.tls_config($tls_config)
.http2_config(conditional_http2!(option.skip_http2, $http2_config))
.default_headers(conditional_headers!(option.skip_headers, $header_initializer, $default_ua))
.build()
}
}
};

Some(default_headers)
} else {
None
};

EmulationProvider::builder()
.tls_config($tls_config)
.http2_config(conditional_http2!(option.skip_http2, $http2_config))
.default_headers(default_headers)
.build()
}
}
};
Expand Down
18 changes: 0 additions & 18 deletions src/emulation/device/macros.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
#[macro_export]
macro_rules! conditional_headers {
($skip_headers:expr, $initializer:expr) => {
if $skip_headers {
None
} else {
Some($initializer())
}
};
($skip_headers:expr, $initializer:expr, $ua:expr) => {
if $skip_headers {
None
} else {
Some($initializer($ua))
}
};
}

#[macro_export]
macro_rules! conditional_http2 {
($skip_http2:expr, $http2:expr) => {
Expand Down
12 changes: 7 additions & 5 deletions src/emulation/device/okhttp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ macro_rules! mod_generator {

#[inline(always)]
pub fn emulation(option: EmulationOption) -> EmulationProvider {
let default_headers = if !option.skip_headers {
Some(super::header_initializer($ua))
} else {
None
};

EmulationProvider::builder()
.tls_config(tls_config!($cipher_list))
.http2_config(conditional_http2!(option.skip_http2, http2_config!()))
.default_headers(conditional_headers!(
option.skip_headers,
super::header_initializer,
$ua
))
.default_headers(default_headers)
.build()
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/emulation/device/safari.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ macro_rules! mod_generator {

#[inline(always)]
pub fn emulation(option: EmulationOption) -> EmulationProvider {
let default_headers = if !option.skip_headers {
Some($header_initializer($ua))
} else {
None
};

EmulationProvider::builder()
.tls_config($tls_config)
.http2_config(conditional_http2!(option.skip_http2, $http2_config))
.default_headers(conditional_headers!(
option.skip_headers,
$header_initializer,
$ua
))
.default_headers(default_headers)
.build()
}
}
Expand Down

0 comments on commit 8182bf6

Please sign in to comment.