Skip to main content

utopia_user_agent/
values.rs

1//! What detection finds: the operating system, the client, the device and
2//! the bot behind a user-agent string.
3//!
4//! Text is [`Text`]: a user-agent header may carry any byte, so names and
5//! versions read from it (a device model, a native app's bundle identifier)
6//! are the input's bytes, borrowed from it. Values from the detectors'
7//! tables are static.
8//!
9//! PHP: strings are bytes there too; Rust borrows where PHP copies.
10
11use std::borrow::Cow;
12
13/// A text value: borrowed from the user-agent or from a static table, owned
14/// when detection rewrites it (`17_4` becomes `17.4`).
15pub type Text<'a> = Cow<'a, [u8]>;
16
17/// `Some` static text.
18pub(crate) fn text(value: &'static str) -> Option<Text<'static>> {
19    Some(Cow::Borrowed(value.as_bytes()))
20}
21
22/// The operating system a user-agent string names; every field is `None`
23/// when it names none.
24///
25/// PHP: `Utopia\UserAgent\OperatingSystem`.
26#[derive(Debug, Clone, Default, PartialEq, Eq)]
27pub struct OperatingSystem<'a> {
28    /// Short code (`WIN`, `IOS`, `UBT`).
29    pub code: Option<Text<'a>>,
30    /// Display name (`Windows`, `iOS`, `Ubuntu`).
31    pub name: Option<Text<'a>>,
32    /// Version, when the string carries one (`7`, `17.4`).
33    pub version: Option<Text<'a>>,
34}
35
36impl OperatingSystem<'_> {
37    /// Whether the operating system has a name.
38    ///
39    /// PHP: `isKnown()`.
40    pub fn is_known(&self) -> bool {
41        self.name.is_some()
42    }
43}
44
45/// The client a user-agent string names: a browser, an HTTP library or a
46/// native app.
47///
48/// PHP: `Utopia\UserAgent\Client`.
49#[derive(Debug, Clone, Default, PartialEq, Eq)]
50pub struct Client<'a> {
51    /// Kind of client: `browser`, `library` or `mobile app`.
52    ///
53    /// PHP: the `type` field.
54    pub kind: Option<Text<'a>>,
55    /// Short code of a browser (`CH`, `FF`); libraries and apps have none.
56    pub code: Option<Text<'a>>,
57    /// Display name (`Chrome`, `curl`, or a native app's bundle identifier).
58    pub name: Option<Text<'a>>,
59    /// Version of the client.
60    pub version: Option<Text<'a>>,
61    /// Rendering engine (`Blink`, `WebKit`, `Gecko`, `Trident`, `Presto`).
62    pub engine: Option<Text<'a>>,
63    /// Version of the rendering engine.
64    pub engine_version: Option<Text<'a>>,
65}
66
67impl Client<'_> {
68    /// Whether the client has a name.
69    ///
70    /// PHP: `isKnown()`.
71    pub fn is_known(&self) -> bool {
72        self.name.is_some()
73    }
74
75    /// Whether the client is a browser (its kind is `browser`).
76    ///
77    /// PHP: `isBrowser()`.
78    pub fn is_browser(&self) -> bool {
79        self.kind.as_deref() == Some(b"browser".as_slice())
80    }
81}
82
83/// The device a user-agent string names.
84///
85/// PHP: `Utopia\UserAgent\Device`.
86#[derive(Debug, Clone, Default, PartialEq, Eq)]
87pub struct Device<'a> {
88    /// Kind of device: `desktop`, `smartphone`, `tablet`, `tv`, `console`,
89    /// `wearable` or `portable media player`.
90    ///
91    /// PHP: the `type` field.
92    pub kind: Option<Text<'a>>,
93    /// Manufacturer (`Apple`, `Samsung`).
94    pub brand: Option<Text<'a>>,
95    /// Model (`iPhone`, `iPad`, `Apple TV`).
96    pub model: Option<Text<'a>>,
97}
98
99impl Device<'_> {
100    /// Whether the device has a kind, a brand or a model.
101    ///
102    /// PHP: `isKnown()`.
103    pub fn is_known(&self) -> bool {
104        self.kind.is_some() || self.brand.is_some() || self.model.is_some()
105    }
106}
107
108/// A bot a user-agent string names: a crawler, preview fetcher, monitor or
109/// automation tool.
110///
111/// PHP: `Utopia\UserAgent\Bot`.
112#[derive(Debug, Clone, PartialEq, Eq)]
113pub struct Bot<'a> {
114    /// Name of the bot (`Googlebot`, `GPTBot`).
115    pub name: Text<'a>,
116    /// `search crawler`, `ai crawler`, `social preview`, ... ([`Bot::CRAWLER`]
117    /// for bots only the generic rule recognises).
118    pub category: Text<'a>,
119}
120
121impl<'a> Bot<'a> {
122    /// The default category, given to bots only the generic rule recognises.
123    pub const CRAWLER: &'static str = "crawler";
124
125    /// A bot of category [`Bot::CRAWLER`].
126    ///
127    /// PHP: `new Bot($name)`.
128    pub fn new(name: impl Into<Text<'a>>) -> Self {
129        Self { name: name.into(), category: Cow::Borrowed(Self::CRAWLER.as_bytes()) }
130    }
131
132    /// A bot of the given category.
133    ///
134    /// PHP: `new Bot($name, $category)`.
135    pub fn with_category(name: impl Into<Text<'a>>, category: impl Into<Text<'a>>) -> Self {
136        Self { name: name.into(), category: category.into() }
137    }
138}