Crates.io | fast_uaparser |
lib.rs | fast_uaparser |
version | 2.0.1 |
source | src |
created_at | 2019-08-10 16:52:24.852519 |
updated_at | 2020-02-16 17:14:25.639803 |
description | Regex-based parser for User-Agent request headers |
homepage | |
repository | https://gitlab.com/philbooth/uaparser-rs |
max_upload_size | |
id | 155621 |
size | 47,757 |
Parse User-Agent request header strings.
Parsers are derived from regular expressions published in the ua-parser/ua-core repository. The regular expressions are fetched in a custom build step, then used to generate static Rust code that is compiled into the lib.
There is a one-off initialisation cost
to load the parser objects at runtime,
which is paid when you call
the init
function.
If init
is not called explicitly,
initialisation occurs lazily instead
and parsing will block
until it finishes.
Add it to your dependencies
in Cargo.toml
:
[dependencies]
fast_uaparser = "1"
For more detailed information see the API docs, but the general gist is as follows:
use fast_uaparser::{Device, OperatingSystem, UserAgent};
// Pay initialisation costs up-front
fast_uaparser::init().unwrap();
// Parse user-agent information
let ua: UserAgent =
"Mozilla/5.0 (X11; Linux i686; rv:70.0) Gecko/20100101 Firefox/70.0"
.parse()
.unwrap();
assert_eq!(ua.family, "Firefox");
assert_eq!(ua.version.major.unwrap(), "70");
assert_eq!(ua.version.minor.unwrap(), "0");
assert!(ua.version.patch.is_none());
assert!(ua.version.patch_minor.is_none());
// Parse OS information
let os: OperatingSystem =
"Mozilla/5.0 (iPad; CPU OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1 Mobile/15E148 Safari/604.1"
.parse()
.unwrap();
assert_eq!(os.family, "iOS");
assert_eq!(os.version.major.unwrap(), "12");
assert_eq!(os.version.minor.unwrap(), "2");
assert!(os.version.patch.is_none());
assert!(os.version.patch_minor.is_none());
// Parse device information
let device: Device =
"Mozilla/5.0 (Windows Mobile 10; Android 8.0.0; Microsoft; Lumia 950XL) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.89 Mobile Safari/537.36 Edge/40.15254.369"
.parse()
.unwrap();
assert_eq!(device.family, "Lumia 950XL");
assert_eq!(device.brand.unwrap(), "Nokia");
assert_eq!(device.model.unwrap(), "Lumia 950XL");
f you don't already have Rust installed,
get that first using rustup
:
curl https://sh.rustup.rs -sSf | sh
Then you can build the project:
cargo b
And run the tests:
cargo t
Here.