Skip to content

Commit

Permalink
refactor(emulation): reorganized export module (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
0x676e67 authored Mar 7, 2025
1 parent 1bbb6a4 commit 5288ba4
Show file tree
Hide file tree
Showing 3 changed files with 314 additions and 321 deletions.
10 changes: 0 additions & 10 deletions src/emulation/device/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,3 @@ macro_rules! join {
concat!($first $(, $sep, $rest)*)
};
}

macro_rules! emulation_match {
($ver:expr, $opt:expr, $($variant:pat => $path:expr),+) => {
match $ver {
$(
$variant => $path($opt),
)+
}
}
}
318 changes: 8 additions & 310 deletions src/emulation/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,20 @@

#[macro_use]
mod macros;
mod chrome;
mod firefox;
mod okhttp;
mod safari;

use rquest::{EmulationProvider, EmulationProviderFactory};
use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;

#[cfg(feature = "emulation-rand")]
use strum_macros::VariantArray;

use chrome::*;
use firefox::*;
use okhttp::*;
use safari::*;
pub mod chrome;
pub mod firefox;
pub mod okhttp;
pub mod safari;

mod emulation_imports {
pub use rquest::Http2Config;
pub use crate::emulation::{EmulationOS, EmulationOption};
#[cfg(all(feature = "gzip", feature = "deflate", feature = "brotli"))]
pub use rquest::header::ACCEPT_ENCODING;
pub use rquest::header::{
ACCEPT, ACCEPT_LANGUAGE, HeaderMap, HeaderName, HeaderValue, UPGRADE_INSECURE_REQUESTS,
USER_AGENT,
};

#[cfg(all(feature = "gzip", feature = "deflate", feature = "brotli"))]
pub use rquest::header::ACCEPT_ENCODING;
pub use rquest::{EmulationProvider, Http2Config};
}

mod tls_imports {
Expand All @@ -46,293 +34,3 @@ mod http2_imports {
pub use rquest::{Priority, StreamDependency, StreamId};
pub use std::sync::LazyLock;
}

macro_rules! define_emulation_enum {
($(#[$meta:meta])* $name:ident, $default_variant:ident, $($variant:ident => $rename:expr),*) => {
$(#[$meta])*
#[cfg(not(feature = "emulation-rand"))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub enum $name {
$(
#[serde(rename = $rename)]
$variant,
)*
}

#[cfg(not(feature = "emulation-rand"))]
impl Default for $name {
fn default() -> Self {
$name::$default_variant
}
}

$(#[$meta])*
#[cfg(feature = "emulation-rand")]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize, VariantArray)]
pub enum $name {
$(
#[serde(rename = $rename)]
$variant,
)*
}

#[cfg(feature = "emulation-rand")]
impl Default for $name {
fn default() -> Self {
$name::$default_variant
}
}
};
}

define_emulation_enum!(
/// Represents different browser versions for impersonation.
///
/// The `Emulation` enum provides variants for different browser versions that can be used
/// to emulation HTTP requests. Each variant corresponds to a specific browser version.
///
/// # Naming Convention
///
/// The naming convention for the variants follows the pattern `browser_version`, where
/// `browser` is the name of the browser (e.g., `chrome`, `firefox`, `safari`) and `version`
/// is the version number. For example, `Chrome100` represents Chrome version 100.
///
/// The serialized names of the variants use underscores to separate the browser name and
/// version number, following the pattern `browser_version`. For example, `Chrome100` is
/// serialized as `"chrome_100"`.
///
/// # Examples
///
/// ```rust
/// use rquest_util::Emulation;
///
/// let emulation = Emulation::Chrome100;
/// let serialized = serde_json::to_string(&emulation).unwrap();
/// assert_eq!(serialized, "\"chrome_100\"");
///
/// let deserialized: Emulation = serde_json::from_str(&serialized).unwrap();
/// assert_eq!(deserialized, Emulation::Chrome100);
/// ```
Emulation, Chrome133,
Chrome100 => "chrome_100",
Chrome101 => "chrome_101",
Chrome104 => "chrome_104",
Chrome105 => "chrome_105",
Chrome106 => "chrome_106",
Chrome107 => "chrome_107",
Chrome108 => "chrome_108",
Chrome109 => "chrome_109",
Chrome114 => "chrome_114",
Chrome116 => "chrome_116",
Chrome117 => "chrome_117",
Chrome118 => "chrome_118",
Chrome119 => "chrome_119",
Chrome120 => "chrome_120",
Chrome123 => "chrome_123",
Chrome124 => "chrome_124",
Chrome126 => "chrome_126",
Chrome127 => "chrome_127",
Chrome128 => "chrome_128",
Chrome129 => "chrome_129",
Chrome130 => "chrome_130",
Chrome131 => "chrome_131",
Chrome132 => "chrome_132",
Chrome133 => "chrome_133",
SafariIos17_2 => "safari_ios_17.2",
SafariIos17_4_1 => "safari_ios_17.4.1",
SafariIos16_5 => "safari_ios_16.5",
Safari15_3 => "safari_15.3",
Safari15_5 => "safari_15.5",
Safari15_6_1 => "safari_15.6.1",
Safari16 => "safari_16",
Safari16_5 => "safari_16.5",
Safari17_0 => "safari_17.0",
Safari17_2_1 => "safari_17.2.1",
Safari17_4_1 => "safari_17.4.1",
Safari17_5 => "safari_17.5",
Safari18 => "safari_18",
SafariIPad18 => "safari_ipad_18",
Safari18_2 => "safari_18.2",
SafariIos18_1_1 => "safari_ios_18.1.1",
Safari18_3 => "safari_18.3",
OkHttp3_9 => "okhttp_3.9",
OkHttp3_11 => "okhttp_3.11",
OkHttp3_13 => "okhttp_3.13",
OkHttp3_14 => "okhttp_3.14",
OkHttp4_9 => "okhttp_4.9",
OkHttp4_10 => "okhttp_4.10",
OkHttp5 => "okhttp_5",
Edge101 => "edge_101",
Edge122 => "edge_122",
Edge127 => "edge_127",
Edge131 => "edge_131",
Firefox109 => "firefox_109",
Firefox117 => "firefox_117",
Firefox128 => "firefox_128",
Firefox133 => "firefox_133",
Firefox135 => "firefox_135",
FirefoxPrivate135 => "firefox_private_135",
FirefoxAndroid135 => "firefox_android_135"
);

/// ======== Emulation impls ========
impl EmulationProviderFactory for Emulation {
fn emulation(self) -> EmulationProvider {
EmulationOption::builder()
.emulation(self)
.build()
.emulation()
}
}

/// Represents different operating systems for impersonation.
///
/// The `EmulationOS` enum provides variants for different operating systems that can be used
/// to emulation HTTP requests. Each variant corresponds to a specific operating system.
///
/// # Naming Convention
///
/// The naming convention for the variants follows the pattern `os_name`, where
/// `os_name` is the name of the operating system (e.g., `windows`, `macos`, `linux`, `android`, `ios`).
///
/// The serialized names of the variants use lowercase letters to represent the operating system names.
/// For example, `Windows` is serialized as `"windows"`.
///
/// # Examples
///
/// ```rust
/// use rquest::EmulationOS;
///
/// let emulation_os = EmulationOS::Windows;
/// let serialized = serde_json::to_string(&emulation_os).unwrap();
/// assert_eq!(serialized, "\"windows\"");
///
/// let deserialized: EmulationOS = serde_json::from_str(&serialized).unwrap();
/// assert_eq!(deserialized, EmulationOS::Windows);
/// ```
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Deserialize, Serialize)]
pub enum EmulationOS {
#[serde(rename = "windows")]
Windows,
#[serde(rename = "macos")]
#[default]
MacOS,
#[serde(rename = "linux")]
Linux,
#[serde(rename = "android")]
Android,
#[serde(rename = "ios")]
IOS,
}

/// ======== EmulationOS impls ========
impl EmulationOS {
#[inline]
fn platform(&self) -> &'static str {
match self {
EmulationOS::MacOS => "\"macOS\"",
EmulationOS::Linux => "\"Linux\"",
EmulationOS::Windows => "\"Windows\"",
EmulationOS::Android => "\"Android\"",
EmulationOS::IOS => "\"iOS\"",
}
}

#[inline]
fn is_mobile(&self) -> bool {
matches!(self, EmulationOS::Android | EmulationOS::IOS)
}
}

#[derive(Default, TypedBuilder)]
pub struct EmulationOption {
/// The browser version to emulation.
#[builder(default)]
emulation: Emulation,

/// The operating system.
#[builder(default)]
emulation_os: EmulationOS,

/// Whether to skip HTTP/2.
#[builder(default = false)]
skip_http2: bool,

/// Whether to skip headers.
#[builder(default = false)]
skip_headers: bool,
}

/// ======== EmulationOption impls ========
impl EmulationProviderFactory for EmulationOption {
fn emulation(self) -> EmulationProvider {
emulation_match!(
self.emulation,
self,

Emulation::Chrome100 => v100::emulation,
Emulation::Chrome101 => v101::emulation,
Emulation::Chrome104 => v104::emulation,
Emulation::Chrome105 => v105::emulation,
Emulation::Chrome106 => v106::emulation,
Emulation::Chrome107 => v107::emulation,
Emulation::Chrome108 => v108::emulation,
Emulation::Chrome109 => v109::emulation,
Emulation::Chrome114 => v114::emulation,
Emulation::Chrome116 => v116::emulation,
Emulation::Chrome117 => v117::emulation,
Emulation::Chrome118 => v118::emulation,
Emulation::Chrome119 => v119::emulation,
Emulation::Chrome120 => v120::emulation,
Emulation::Chrome123 => v123::emulation,
Emulation::Chrome124 => v124::emulation,
Emulation::Chrome126 => v126::emulation,
Emulation::Chrome127 => v127::emulation,
Emulation::Chrome128 => v128::emulation,
Emulation::Chrome129 => v129::emulation,
Emulation::Chrome130 => v130::emulation,
Emulation::Chrome131 => v131::emulation,
Emulation::Chrome132 => v132::emulation,
Emulation::Chrome133 => v133::emulation,

Emulation::SafariIos17_2 => safari_ios_17_2::emulation,
Emulation::SafariIos17_4_1 => safari_ios_17_4_1::emulation,
Emulation::SafariIos16_5 => safari_ios_16_5::emulation,
Emulation::Safari15_3 => safari15_3::emulation,
Emulation::Safari15_5 => safari15_5::emulation,
Emulation::Safari15_6_1 => safari15_6_1::emulation,
Emulation::Safari16 => safari16::emulation,
Emulation::Safari16_5 => safari16_5::emulation,
Emulation::Safari17_0 => safari17_0::emulation,
Emulation::Safari17_2_1 => safari17_2_1::emulation,
Emulation::Safari17_4_1 => safari17_4_1::emulation,
Emulation::Safari17_5 => safari17_5::emulation,
Emulation::Safari18 => safari18::emulation,
Emulation::SafariIPad18 => safari_ipad_18::emulation,
Emulation::Safari18_2 => safari18_2::emulation,
Emulation::SafariIos18_1_1 => safari_ios_18_1_1::emulation,
Emulation::Safari18_3 => safari18_3::emulation,

Emulation::OkHttp3_9 => okhttp3_9::emulation,
Emulation::OkHttp3_11 => okhttp3_11::emulation,
Emulation::OkHttp3_13 => okhttp3_13::emulation,
Emulation::OkHttp3_14 => okhttp3_14::emulation,
Emulation::OkHttp4_9 => okhttp4_9::emulation,
Emulation::OkHttp4_10 => okhttp4_10::emulation,
Emulation::OkHttp5 => okhttp5::emulation,

Emulation::Edge101 => edge101::emulation,
Emulation::Edge122 => edge122::emulation,
Emulation::Edge127 => edge127::emulation,
Emulation::Edge131 => edge131::emulation,

Emulation::Firefox109 => ff109::emulation,
Emulation::Firefox117 => ff117::emulation,
Emulation::Firefox128 => ff128::emulation,
Emulation::Firefox133 => ff133::emulation,
Emulation::Firefox135 => ff135::emulation,
Emulation::FirefoxPrivate135 => ff_private_135::emulation,
Emulation::FirefoxAndroid135 => ff_android_135::emulation
)
}
}
Loading

0 comments on commit 5288ba4

Please sign in to comment.