4 releases (stable)

2.0.1 Feb 16, 2020
2.0.0 Feb 2, 2020
1.0.0 Aug 11, 2019
0.1.2 Aug 10, 2019

#1867 in Parser implementations

Download history 29/week @ 2024-07-29 13/week @ 2024-08-12 40/week @ 2024-09-23 27/week @ 2024-10-07 37/week @ 2024-10-14

64 downloads per month

Apache-2.0

34KB
577 lines

fast_uaparser

Build status Crate status Downloads License

Parse User-Agent request header strings.

How does it work?

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.

How do I install it?

Add it to your dependencies in Cargo.toml:

[dependencies]
fast_uaparser = "1"

How do I use it?

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");

How do I set up the dev environment?

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

Where are the API docs?

Here.

Where is the change log?

Here

What license is it released under?

Apache-2.0.

Dependencies

~3–10MB
~114K SLoC