Skip to main content

user_agent_parser/models/
engine.rs

1use std::borrow::Cow;
2
3use super::version::join_version;
4
5/// The layout engine of the product a user agent belongs to.
6#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
7pub struct Engine<'a> {
8    /// The engine name, or `None` when no pattern matches.
9    pub name:  Option<Cow<'a, str>>,
10    /// The major version, if the matching pattern captures one.
11    pub major: Option<Cow<'a, str>>,
12    /// The minor version, if the matching pattern captures one.
13    pub minor: Option<Cow<'a, str>>,
14    /// The patch version, if the matching pattern captures one.
15    pub patch: Option<Cow<'a, str>>,
16}
17
18impl<'a> Engine<'a> {
19    /// Joins the version parts into a full version string, stopping at the first part which is missing.
20    ///
21    /// ```
22    /// use std::borrow::Cow;
23    ///
24    /// use user_agent_parser::Engine;
25    ///
26    /// let engine = Engine {
27    ///     name:  Some(Cow::from("Blink")),
28    ///     major: Some(Cow::from("57")),
29    ///     minor: Some(Cow::from("0")),
30    ///     patch: Some(Cow::from("2987")),
31    /// };
32    ///
33    /// assert_eq!(Some(Cow::from("57.0.2987")), engine.version());
34    /// ```
35    #[inline]
36    pub fn version(&self) -> Option<Cow<'_, str>> {
37        join_version(&[self.major.as_deref(), self.minor.as_deref(), self.patch.as_deref()])
38    }
39
40    /// Extracts the owned data.
41    #[inline]
42    pub fn into_owned(self) -> Engine<'static> {
43        let name = self.name.map(|c| Cow::from(c.into_owned()));
44        let major = self.major.map(|c| Cow::from(c.into_owned()));
45        let minor = self.minor.map(|c| Cow::from(c.into_owned()));
46        let patch = self.patch.map(|c| Cow::from(c.into_owned()));
47
48        Engine {
49            name,
50            major,
51            minor,
52            patch,
53        }
54    }
55}