Skip to content

Commit d43e6a9

Browse files
Support dispatch on FormFactor in spoof_by (#8)
* docs(agents): fix link reference in docstring Link reference defined in list item is not shown in rendered docstring. <https://rust-lang.github.io/rust-clippy/rust-1.92.0/index.html#doc_nested_refdefs> Caught by Clippy. * refactor(agents): remove superfluous reference `ua_instance` is borrowed but then immediately dereferenced by the compiler. <https://rust-lang.github.io/rust-clippy/rust-1.92.0/index.html#needless_borrow> Caught by Clippy. * feat(agents): support dispatch on FormFactor in spoof_by Add functions to collate static user agents into groups by form factor, and then dispatch on them in spoof_by. Addresses #7: [1] [1]: #7 * perf(ua): zero copy form factor --------- Co-authored-by: j-mendez <jeff@spider.cloud>
1 parent 57cb301 commit d43e6a9

File tree

4 files changed

+64
-4
lines changed

4 files changed

+64
-4
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ua_generator/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ua_generator"
3-
version = "0.5.42"
3+
version = "0.5.43"
44
edition = "2021"
55
authors = ["j-mendez <jeff@spider.cloud>"]
66
description = "Random User Agent Spoofer in Rust."

ua_generator/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//!
88
//!
99
//! - **Generate** get a random UA to use.
10-
//! - [`spoof_ua`]: ua/fn.spoof_ua.html
10+
//! - [`spoof_ua`][]: ua/fn.spoof_ua.html
1111
//!
1212
//! # Basic usage
1313
//!

ua_generator/src/ua.rs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,37 @@ pub fn spoof_by(
367367
// Any Safari fallback → global Safari list
368368
(_, _, Some(Browser::Safari)) => pick_rand(rng, STATIC_SAFARI_AGENTS),
369369

370+
// --- FormFactor match only ---
371+
(_, Some(FormFactor::Desktop), _) => pick_rand_multi(
372+
rng,
373+
&[
374+
STATIC_CHROME_WINDOWS_AGENTS,
375+
STATIC_CHROME_MAC_AGENTS,
376+
STATIC_CHROME_LINUX_AGENTS,
377+
STATIC_FIREFOX_WINDOWS_AGENTS,
378+
STATIC_FIREFOX_MAC_AGENTS,
379+
STATIC_FIREFOX_LINUX_AGENTS,
380+
STATIC_SAFARI_MAC_AGENTS,
381+
],
382+
),
383+
(_, Some(FormFactor::Mobile), _) => pick_rand_multi(
384+
rng,
385+
&[
386+
STATIC_CHROME_MOBILE_AGENTS,
387+
STATIC_FIREFOX_MOBILE_AGENTS,
388+
STATIC_SAFARI_MOBILE_AGENTS,
389+
],
390+
),
391+
(_, Some(FormFactor::Tablet), _) => pick_rand_multi(
392+
rng,
393+
&[
394+
STATIC_CHROME_TABLET_AGENTS,
395+
STATIC_FIREFOX_TABLET_AGENTS,
396+
STATIC_SAFARI_TABLET_AGENTS,
397+
],
398+
),
399+
400+
// --- Fall Back ---
370401
// IE: until IE lists are generated, fall back to mixed static
371402
_ => pick_rand(rng, STATIC_AGENTS),
372403
}
@@ -520,6 +551,30 @@ fn pick_rand<'a>(rng: Option<&mut Rng>, candidates: &'a [&'a str]) -> &'a str {
520551
}
521552
}
522553

554+
/// Pick randomly across multiple candidate slices without allocating.
555+
#[inline]
556+
fn pick_rand_multi<'a>(rng: Option<&mut Rng>, groups: &[&'a [&'a str]]) -> &'a str {
557+
let total: usize = groups.iter().map(|g| g.len()).sum();
558+
if total == 0 {
559+
return "";
560+
}
561+
562+
let mut idx = match rng {
563+
Some(r) => r.usize(..total),
564+
None => fastrand::usize(..total),
565+
};
566+
567+
for g in groups {
568+
if idx < g.len() {
569+
return g[idx];
570+
}
571+
idx -= g.len();
572+
}
573+
574+
// unreachable if `total` computed correctly
575+
""
576+
}
577+
523578
#[cfg(test)]
524579
mod tests {
525580
use super::*;
@@ -564,7 +619,7 @@ mod tests {
564619
assert!(
565620
Rc::ptr_eq(
566621
agent3_rc,
567-
&ua_instance
622+
ua_instance
568623
.list_map
569624
.iter()
570625
.find_map(|(rc, _)| if **rc == "Agent3" { Some(rc) } else { None })
@@ -633,5 +688,10 @@ mod tests {
633688
Some(Browser::Ie),
634689
None,
635690
);
691+
692+
// FormFactor-only
693+
let _ = spoof_by(None, Some(FormFactor::Desktop), None, None);
694+
let _ = spoof_by(None, Some(FormFactor::Mobile), None, None);
695+
let _ = spoof_by(None, Some(FormFactor::Tablet), None, None);
636696
}
637697
}

0 commit comments

Comments
 (0)