1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use crate::{Error, UserAgentParser};
pub struct UserAgentParserBuilder {
pub(super) device: bool,
pub(super) os: bool,
pub(super) user_agent: bool,
pub(super) unicode: bool,
}
impl UserAgentParserBuilder {
pub(super) fn new() -> Self {
UserAgentParserBuilder {
device: true,
os: true,
user_agent: true,
unicode: true,
}
}
/// Enable or disable unicode support. This is enabled by default.
/// Unicode regexes are much more complex and take up more memory.
/// Most uaparser implementation do not support unicode, so disabling
/// this is generally safe to do.
pub fn with_unicode_support(mut self, enabled: bool) -> Self {
self.unicode = enabled;
self
}
/// Enable or disable device parsing. This is enabled by default.
/// Because all regexes are compiled up front, disabling this will
/// save a decent amount of memory.
pub fn with_device(mut self, enabled: bool) -> Self {
self.device = enabled;
self
}
/// Enable or disable os parsing. This is enabled by default.
/// Because all regexes are compiled up front, disabling this will
/// save a decent amount of memory.
pub fn with_os(mut self, enabled: bool) -> Self {
self.os = enabled;
self
}
/// Enable or disable user agent parsing. This is enabled by default.
/// Because all regexes are compiled up front, disabling this will
/// save a decent amount of memory.
pub fn with_user_agent(mut self, enabled: bool) -> Self {
self.user_agent = enabled;
self
}
pub fn build_from_yaml(self, path: &str) -> Result<UserAgentParser, Error> {
UserAgentParser::_build_from_yaml(path, self)
}
/// Attempts to construct a `UserAgentParser` from a slice of raw bytes. The
/// intention with providing this function is to allow using the
/// `include_bytes!` macro to compile the `regexes.yaml` file into the
/// the library by a consuming application.
///
/// ```rust
/// # use uaparser::*;
/// let regexes = include_bytes!("../../src/core/regexes.yaml");
/// let parser = UserAgentParser::builder().build_from_bytes(regexes);
/// ```
pub fn build_from_bytes(self, bytes: &[u8]) -> Result<UserAgentParser, Error> {
UserAgentParser::_build_from_bytes(bytes, self)
}
}