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
use yaml_rust::{Yaml};
use yaml;
use regex::Regex;


///`UserAgent` contains the user agent information.
#[derive(Debug, PartialEq, Eq)]
pub struct UserAgent {
    pub family: String,
    pub major: Option<String>,
    pub minor: Option<String>,
    pub patch: Option<String>,
}

#[derive(Debug)]
pub struct UserAgentParser {
    pub regex: Regex,
    pub family: Option<String>,
    pub major: Option<String>,
    pub minor: Option<String>,
    pub patch: Option<String>,
}

impl UserAgentParser {
    pub fn from_yaml(y: &Yaml) -> Option<UserAgentParser> {
            yaml::string_from_map(y, "regex")
            .and_then(|r| Regex::new(&r[..]).ok())
            .map(|r| UserAgentParser {
                regex: r,
                family: yaml::string_from_map(y, "family_replacement"),
                major: yaml::string_from_map(y, "v1_replacement"),
                minor: yaml::string_from_map(y, "v2_replacement"),
                patch: yaml::string_from_map(y, "v3_replacement"),
            })
    }

    pub fn parse(&self, agent: String) -> Option<UserAgent> {
        self.regex.captures(&agent[..]).map(|c| {
            let family = self.family.clone()
                .and_then(|f| c.at(1).map(|a| f.replace("$1", a)))
                .unwrap_or(c.at(1).unwrap().to_string());
            let major = self.major.clone()
                .and_then(|f| c.at(2).map(|a| f.replace("$2", a)))
                .or(c.at(2).map(String::from));
            let minor = self.minor.clone()
                .and_then(|f| c.at(3).map(|a| f.replace("$3", a)))
                .or(c.at(3).map(String::from));
            let patch = self.patch.clone()
                .and_then(|f| c.at(4).map(|a| f.replace("$4", a)))
                .or(c.at(4).map(String::from));

            UserAgent {
                family: family,
                major: major,
                minor: minor,
                patch: patch,
            }
        })
    }
}