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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use std::io::prelude::*;
use std::fs::{File};
use yaml_rust::{YamlLoader};
use client::{Client};
use ua::{UserAgent, UserAgentParser};
use device::{Device, DeviceParser};
use os::{OS, OSParser};
use result::*;
use yaml;
#[derive(Debug)]
pub struct Parser {
pub ua_regex: Vec<UserAgentParser>,
pub devices_regex: Vec<DeviceParser>,
pub os_regex: Vec<OSParser>,
}
impl Parser {
pub fn from_file(regexes_file: &str) -> Result<Parser> {
let mut file = try!(File::open(regexes_file));
let mut yaml = String::new();
let _ = file.read_to_string(&mut yaml);
Parser::from_str(&yaml)
}
pub fn from_str(s: &str) -> Result<Parser> {
let docs = try!(YamlLoader::load_from_str(&s));
let p = Parser {
devices_regex: yaml::from_map(&docs[0],"device_parsers")
.map(|y| yaml::map_over_arr(y, DeviceParser::from_yaml)).unwrap(),
ua_regex: yaml::from_map(&docs[0],"user_agent_parsers")
.map(|y| yaml::map_over_arr(y, UserAgentParser::from_yaml)).unwrap(),
os_regex: yaml::from_map(&docs[0],"os_parsers")
.map(|y| yaml::map_over_arr(y, OSParser::from_yaml)).unwrap(),
};
Ok(p)
}
pub fn new() -> Result<Parser> {
let s = include_str!("uap-core/regexes.yaml");
Parser::from_str(&s)
}
pub fn parse(&self, agent: String) -> Client {
let ua = self.ua_regex.iter().filter_map(|u| u.parse(agent.clone())).next();
let u = ua.unwrap_or(
UserAgent {
family: "Other".to_string(),
major: None,
minor: None,
patch: None,
});
let dev = self.devices_regex.iter().filter_map(|d| d.parse(agent.clone())).next();
let d = dev.unwrap_or(Device {
family: "Other".to_string(),
model: None,
brand: None,
});
let oss = self.os_regex.iter().filter_map(|d| d.parse(agent.clone())).next();
let o = oss.unwrap_or(OS {
family: "Other".to_string(),
major: None,
minor: None,
patch: None,
patch_minor: None,
});
Client {
user_agent: u,
os: o,
device: d,
}
}
}