mod browser;
mod config;
mod os_version;
use std::fmt::Display;
pub use crate::{
browser::Browser, os_version::DesktopDevice, os_version::Devices, os_version::MobileDevice,
};
enum Product {
Mozilla,
Opera,
}
impl<'a> Default for Product {
fn default() -> Self {
Product::Mozilla
}
}
impl<'a> Display for Product {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Product::Mozilla => write!(f, "Mozilla/5.0"),
Product::Opera => write!(f, "Opera/9.80"),
}
}
}
pub struct UserAgent {
product: Product,
os_ver: Devices,
browser: Browser,
}
impl Display for UserAgent {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{} {} {}",
self.product.to_string(),
self.os_ver.to_string(),
self.browser.to_string()
)
}
}
impl UserAgent {
pub fn random() -> Self {
UserAgent {
product: Product::Mozilla,
os_ver: rand::random(),
browser: rand::random(),
}
}
pub fn mobile() -> Self {
UserAgent {
product: Product::Mozilla,
os_ver: Devices::Mobile(rand::random()),
browser: rand::random(),
}
}
pub fn pc() -> Self {
UserAgent {
product: Product::Mozilla,
os_ver: Devices::Desktop(rand::random()),
browser: rand::random(),
}
}
pub fn custom(os_ver: Devices, browser: Browser) -> Self {
UserAgent {
product: Product::Mozilla,
os_ver,
browser,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
println!("{}", UserAgent::random().to_string());
}
#[test]
fn os_check() {
println!("Mobile:{}", UserAgent::mobile().to_string());
println!("Pc:{}", UserAgent::pc().to_string());
}
#[test]
fn os_custom() {
let mut rng = rand::thread_rng();
println!(
"custom Mobile Iphone Chrome:{}",
UserAgent::custom(Devices::Mobile(MobileDevice::Iphone), Browser::Chrome)
);
println!(
"custom Desktop Windows random browser :{}",
UserAgent::custom(Devices::Desktop(DesktopDevice::Windows), rand::random())
);
println!(
"custom random Mobile & random browser :{}",
UserAgent::custom(Devices::Mobile(rand::random()), Browser::random(&mut rng))
);
println!(
"custom random Mobile & random browser :{}",
UserAgent::custom(Devices::Mobile(rand::random()), Browser::random(&mut rng))
);
let mut rng = rand::thread_rng();
UserAgent::custom(Devices::random(&mut rng), Browser::random(&mut rng));
UserAgent::custom(rand::random(), Browser::random(&mut rng));
}
}