utopia_user_agent/lib.rs
1//! User-agent detection: the operating system, client, device and bot
2//! behind a user-agent string.
3//!
4//! Any bytes are accepted, including bytes that are not UTF-8, and what is
5//! read from the string is borrowed from it.
6//!
7//! ```
8//! use utopia_user_agent::UserAgent;
9//!
10//! let agent = UserAgent::parse("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0");
11//! assert_eq!(agent.operating_system().version.as_deref(), Some(b"7".as_slice()));
12//! assert_eq!(agent.client().name.as_deref(), Some(b"Firefox".as_slice()));
13//! assert!(!agent.is_bot());
14//! ```
15//!
16//! Detection runs on every request, so it avoids regular expressions where
17//! native code does the same job: case-insensitive substring, word and
18//! `<token>/<version>` scans that never allocate, and one pass over the
19//! string for the whole bot table. The few patterns left (device models
20//! and brands, Internet Explorer, the generic bot rule) are compiled once
21//! per process, and run only when the string holds a literal they need.
22//!
23//! PHP: the port of `utopia-php/user-agent` (`packages/user-agent`), with
24//! identical results for every input. The remaining patterns are the PHP
25//! library's, run by [`php_std::pcre`] (a port of the PCRE2 PHP uses); each
26//! native scan names the pattern it replaces, and the crate's tests check
27//! every one, and every prefilter, against that pattern.
28//!
29//! | PHP | Rust |
30//! |---|---|
31//! | `UserAgent::parse($value)` | [`UserAgent::parse`] |
32//! | `UserAgent::raw()` | [`UserAgent::raw`] |
33//! | `UserAgent::operatingSystem()` / `client()` / `device()` / `bot()` | [`UserAgent::operating_system`], [`UserAgent::client`], [`UserAgent::device`], [`UserAgent::bot`] |
34//! | `UserAgent::isBot()` | [`UserAgent::is_bot`] |
35//! | `UserAgent::toArray()`, `*::toArray()` | the public fields of [`OperatingSystem`], [`Client`], [`Device`], [`Bot`] |
36//! | `OperatingSystem` (`code`, `name`, `version`, `isKnown()`) | [`OperatingSystem`], [`OperatingSystem::is_known`] |
37//! | `Client` (`type`, ..., `engineVersion`, `isKnown()`, `isBrowser()`) | [`Client`] (`kind`, ..., `engine_version`), [`Client::is_known`], [`Client::is_browser`] |
38//! | `Device` (`type`, `brand`, `model`, `isKnown()`) | [`Device`] (`kind`, ...), [`Device::is_known`] |
39//! | `new Bot($name, $category = 'crawler')` | [`Bot::new`], [`Bot::with_category`] |
40//! | `Detection\OperatingSystemDetector::detect()` | [`detection::operating_system`] |
41//! | `Detection\ClientDetector::detect()` | [`detection::client`] |
42//! | `Detection\DeviceDetector::detect()` | [`detection::device`] |
43//! | `Detection\BotDetector::detect()` | [`detection::bot`] |
44//!
45//! PHP: Appwrite's session attributes (`Appwrite\Detector\Detector`) are in
46//! `appwrite_core::detector`.
47
48#![warn(missing_docs)]
49
50mod agent;
51pub mod detection;
52mod pattern;
53mod scan;
54mod values;
55mod version;
56
57pub use agent::UserAgent;
58pub use values::{Bot, Client, Device, OperatingSystem, Text};
59
60/// The getting started guide (`guide.md`), compiled and run with the doctests.
61#[cfg(doctest)]
62#[doc = include_str!("../guide.md")]
63pub struct GettingStarted;