1use std::borrow::Cow;
2use std::io;
3
4use io::Write;
5
6use woothee::parser::Parser;
7use woothee::parser::WootheeResult;
8
9#[derive(serde::Serialize)]
10pub struct UserAgent<'a> {
11 pub name: &'a str,
12 pub category: &'a str,
13 pub os: &'a str,
14 pub os_version: Cow<'a, str>,
15 pub browser_type: &'a str,
16 pub version: &'a str,
17 pub vendor: &'a str,
18}
19
20impl<'a> From<WootheeResult<'a>> for UserAgent<'a> {
21 fn from(e: WootheeResult<'a>) -> Self {
22 Self {
23 name: e.name,
24 category: e.category,
25 os: e.os,
26 os_version: e.os_version,
27 browser_type: e.browser_type,
28 version: e.version,
29 vendor: e.vendor,
30 }
31 }
32}
33
34pub fn ua2parsed2json2writer<W>(p: &Parser, ua: &str, wtr: W) -> Result<(), io::Error>
35where
36 W: Write,
37{
38 let oparsed: Option<_> = p.parse(ua);
39 let parsed: WootheeResult = oparsed
40 .ok_or_else(|| format!("invalid user agent: {ua}"))
41 .map_err(io::Error::other)?;
42 let converted: UserAgent = parsed.into();
43 serde_json::to_writer(wtr, &converted)?;
44 Ok(())
45}