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

///`Device` contains the device information from the user agent.
#[derive(Debug, PartialEq, Eq)]
pub struct Device {
    pub family: String,
    pub brand: Option<String>,
    pub model: Option<String>,
}

#[derive(Debug)]
pub struct DeviceParser {
    pub regex: Regex,
    pub family: Option<String>,
    pub brand: Option<String>,
    pub model: Option<String>,
}

impl DeviceParser {
    pub fn from_yaml(y: &Yaml) -> Option<DeviceParser> {
            yaml::string_from_map(y, "regex")
            .and_then(|r| Regex::new(&r[..]).ok())
            .map(|r| DeviceParser {
                regex: r,
                family: yaml::string_from_map(y, "device_replacement"),
                brand: yaml::string_from_map(y, "brand_replacement"),
                model: yaml::string_from_map(y, "model_replacement"),
            })
    }
    fn replace(captures: &Captures, s: String) -> String {
        captures.iter().zip((0..captures.len()))
            .fold(s, |a, (c, i)| a.replace(&format!("${}", i)[..], c.unwrap()))
    }

    pub fn parse(&self, agent: String) -> Option<Device> {
        self.regex.captures(&agent[..]).map(|c| {
            let family = self.family.clone()
                .map(|f| DeviceParser::replace(&c, f))
                .unwrap_or(c.at(1).unwrap().to_string());
            let brand = self.brand.clone()
                .map(|f| DeviceParser::replace(&c, f))
                .or(c.at(1).map(|s| s.to_string()));
            let model = self.model.clone()
                .map(|f| DeviceParser::replace(&c, f))
                .or(c.at(1).map(|s| s.to_string()));
            Device {
                family: family,
                brand: brand,
                model: model,
            }
        })
    }
}