GithubHelp home page GithubHelp logo

jannschu / uap-rust Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hhatto/uap-rust

1.0 2.0 0.0 1.12 MB

User agent parser for the Rust programming language based on ua-parser

License: MIT License

Rust 91.71% Python 8.29%

uap-rust's Introduction

uap-rust

Build Status

This is a web browser user agent parser for Rust based on ua-parser.

This fork ended up being a complete rewrite. All uap-core tests are passing.

The crate offers parsers with optional thread safety. The regular expressions for detecting the browser, device or os are only run when requested. Also, parsers can be chosen to own or borrow the user agent string. We try to avoid string allocation as much as possible.

Usage example

use uap_rust::unsync::BorrowingParser as Parser;
let agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3";
let parser = Parser::new(agent);

let browser = parser.browser();
assert_eq!(browser.family, "Mobile Safari");
let browser_version = browser.version().unwrap();
assert_eq!(browser_version.major, 5);
assert_eq!(browser_version.minor, 1);

let os = parser.os();
assert_eq!(os.family, "iOS");
let os_version = os.version().unwrap();
assert_eq!(os_version.major, 5);
assert_eq!(os_version.minor, 1);

let device = parser.device();
assert_eq!(device.family, "iPhone");
assert_eq!(device.brand.as_ref().unwrap(), "Apple");

To use a Arc<str> as a user agent do

// use std::sync::Arc;
use uap_rust::sync::OwningParser as Parser;
let agent: Arc<str> = Arc::from("Mozilla/5.0 ...");
let parser = Parser::new(agent.clone());

In the example above agent can also be a String. To use Rc, additionally replace unsync by sync.

The OwningParser variant is a convenience wrapper around BorrowingParser to allow storing the user agent along the parser, which is not trivial, since rust does not understand self-referential structs.

uap-rust's People

Contributors

jannschu avatar

Stargazers

dzmitry-lahoda avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.