{"id":15023237,"url":"https://github.com/magiclen/user-agent-parser","last_synced_at":"2025-10-06T02:10:52.877Z","repository":{"id":35050463,"uuid":"200765867","full_name":"magiclen/user-agent-parser","owner":"magiclen","description":"A parser to get the product, OS, device, cpu, and engine information from a user agent, inspired by https://github.com/faisalman/ua-parser-js and https://github.com/ua-parser/uap-core","archived":false,"fork":false,"pushed_at":"2024-07-23T16:56:00.000Z","size":58,"stargazers_count":18,"open_issues_count":4,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-01-31T22:11:24.402Z","etag":null,"topics":["rocket","rust","user-agent"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/magiclen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-08-06T03:00:12.000Z","updated_at":"2025-01-30T16:32:42.000Z","dependencies_parsed_at":"2024-10-11T06:40:32.222Z","dependency_job_id":"543c7b1f-5a30-419a-9a5f-75c2b45edf7d","html_url":"https://github.com/magiclen/user-agent-parser","commit_stats":{"total_commits":36,"total_committers":2,"mean_commits":18.0,"dds":0.02777777777777779,"last_synced_commit":"3c4b6a0469a92f71407a270fa44b01c277bdc7cf"},"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magiclen%2Fuser-agent-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magiclen%2Fuser-agent-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magiclen%2Fuser-agent-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magiclen%2Fuser-agent-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/magiclen","download_url":"https://codeload.github.com/magiclen/user-agent-parser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238386075,"owners_count":19463291,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["rocket","rust","user-agent"],"created_at":"2024-09-24T19:58:51.728Z","updated_at":"2025-10-06T02:10:47.846Z","avatar_url":"https://github.com/magiclen.png","language":"Rust","readme":"User Agent Parser\n====================\n\n[![CI](https://github.com/magiclen/user-agent-parser/actions/workflows/ci.yml/badge.svg)](https://github.com/magiclen/user-agent-parser/actions/workflows/ci.yml)\n\nA parser to get the product, OS, device, cpu, and engine information from a user agent, inspired by https://github.com/faisalman/ua-parser-js and https://github.com/ua-parser/uap-core\n\n## Usage\n\nYou can make a **regexes.yaml** file or copy one from https://github.com/ua-parser/uap-core\n\nThis is a simple example of **regexes.yaml**.\n\n```yaml\nuser_agent_parsers:\n  - regex: '(ESPN)[%20| ]+Radio/(\\d+)\\.(\\d+)\\.(\\d+) CFNetwork'\n  - regex: '(Namoroka|Shiretoko|Minefield)/(\\d+)\\.(\\d+)\\.(\\d+(?:pre|))'\n    family_replacement: 'Firefox ($1)'\n  - regex: '(Android) Eclair'\n    v1_replacement: '2'\n    v2_replacement: '1'\n\nos_parsers:\n  - regex: 'Win(?:dows)? ?(95|98|3.1|NT|ME|2000|XP|Vista|7|CE)'\n    os_replacement: 'Windows'\n    os_v1_replacement: '$1'\n\ndevice_parsers:\n  - regex: '\\bSmartWatch *\\( *([^;]+) *; *([^;]+) *;'\n    device_replacement: '$1 $2'\n    brand_replacement: '$1'\n    model_replacement: '$2'\n```\n\nThen, use the `from_path` (or `from_str` if your YAML data is in-memory) associated function to create a `UserAgentParser` instance.\n\n\n```rust\nuse user_agent_parser::UserAgentParser;\n\nlet ua_parser = UserAgentParser::from_path(\"/path/to/regexes.yaml\").unwrap();\n```\n\nUse the `parse_*` methods and input a user-agent string to get information.\n\n```rust\nuse user_agent_parser::UserAgentParser;\n\nlet ua_parser = UserAgentParser::from_path(\"/path/to/regexes.yaml\").unwrap();\n\nlet user_agent = \"Mozilla/5.0 (X11; Linux x86_64; rv:10.0) Gecko/20100101 Firefox/10.0 [FBAN/FBIOS;FBAV/8.0.0.28.18;FBBV/1665515;FBDV/iPhone4,1;FBMD/iPhone;FBSN/iPhone OS;FBSV/7.0.4;FBSS/2; FBCR/Telekom.de;FBID/phone;FBLC/de_DE;FBOP/5]\";\n\nlet product = ua_parser.parse_product(user_agent);\n\nprintln!(\"{:#?}\", product);\n\n//    Product {\n//        name: Some(\n//            \"Facebook\",\n//        ),\n//        major: Some(\n//            \"8\",\n//        ),\n//        minor: Some(\n//            \"0\",\n//        ),\n//        patch: Some(\n//            \"0\",\n//        ),\n//    }\n\nlet os = ua_parser.parse_os(user_agent);\n\nprintln!(\"{:#?}\", os);\n\n//    OS {\n//        name: Some(\n//            \"iOS\",\n//        ),\n//        major: None,\n//        minor: None,\n//        patch: None,\n//        patch_minor: None,\n//    }\n\nlet device = ua_parser.parse_device(user_agent);\n\nprintln!(\"{:#?}\", device);\n\n//    Device {\n//        name: Some(\n//            \"iPhone\",\n//        ),\n//        brand: Some(\n//            \"Apple\",\n//        ),\n//        model: Some(\n//            \"iPhone4,1\",\n//        ),\n//    }\n\nlet cpu = ua_parser.parse_cpu(user_agent);\n\nprintln!(\"{:#?}\", cpu);\n\n//    CPU {\n//        architecture: Some(\n//            \"amd64\",\n//        ),\n//    }\n\nlet engine = ua_parser.parse_engine(user_agent);\n\nprintln!(\"{:#?}\", engine);\n\n//    Engine {\n//        name: Some(\n//            \"Gecko\",\n//        ),\n//        major: Some(\n//            \"10\",\n//        ),\n//        minor: Some(\n//            \"0\",\n//        ),\n//        patch: None,\n//    }\n```\n\nThe lifetime of result instances of the `parse_*` methods depends on the user-agent string and the `UserAgentParser` instance. To make it independent, call the `into_owned` method.\n\n```rust\nuse user_agent_parser::UserAgentParser;\n\nlet ua_parser = UserAgentParser::from_path(\"/path/to/regexes.yaml\").unwrap();\n\nlet product = ua_parser.parse_product(\"Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.12) Gecko/20101027 Ubuntu/10.04 (lucid) Firefox/3.6.12\").into_owned();\n```\n\n## Rocket Support\n\nThis crate supports the Rocket framework. All you have to do is enabling the `rocket` feature for this crate.\n\n```toml\n[dependencies.user-agent-parser]\nversion = \"*\"\nfeatures = [\"rocket\"]\n```\n\nLet `Rocket` manage a `UserAgentParser` instance, and the `Product`, `OS`, `Device`, `CPU`, `Engine` models of this crate (plus the `UserAgent` model) can be used as *Request Guards*.\n\n```rust\n#[macro_use]\nextern crate rocket;\n\nuse user_agent_parser::{UserAgentParser, UserAgent, Product, OS, Device, CPU, Engine};\n\n#[get(\"/\")]\nfn index(user_agent: UserAgent, product: Product, os: OS, device: Device, cpu: CPU, engine: Engine) -\u003e String {\n    format!(\"{user_agent:#?}\\n{product:#?}\\n{os:#?}\\n{device:#?}\\n{cpu:#?}\\n{engine:#?}\",\n            user_agent = user_agent,\n            product = product,\n            os = os,\n            device = device,\n            cpu = cpu,\n            engine = engine,\n    )\n}\n\n#[launch]\nfn rocket() -\u003e _ {\n    rocket::build()\n        .manage(UserAgentParser::from_path(\"/path/to/regexes.yaml\").unwrap())\n        .mount(\"/\", routes![index])\n}\n```\n\n## Testing\n\n```bash\n# git clone --recurse-submodules git://github.com/magiclen/user-agent-parser.git\n\ngit clone git://github.com/magiclen/user-agent-parser.git\n\ncd user-agent-parser\n\ngit submodule init\ngit submodule update --recursive\n\ncargo test\n```\n\n## Crates.io\n\nhttps://crates.io/crates/user-agent-parser\n\n## Documentation\n\nhttps://docs.rs/user-agent-parser\n\n## License\n\n[MIT](LICENSE)","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagiclen%2Fuser-agent-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmagiclen%2Fuser-agent-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagiclen%2Fuser-agent-parser/lists"}