user-agent-parser 0.4.0

A parser to get the product, OS, device, cpu, and engine information from a user agent, inspired by https://github.com/faisalman/ua-parser-js and https://github.com/ua-parser/uap-core
Documentation
use regex::Regex;
use yaml_rust::Yaml;

use crate::{
    UserAgentParserError,
    regexes::{Replacement, from_yaml_section, optional_replacement},
};

#[derive(Debug)]
pub(crate) struct DeviceRegex {
    pub(crate) regex:              Regex,
    pub(crate) device_replacement: Option<Replacement>,
    pub(crate) brand_replacement:  Option<Replacement>,
    pub(crate) model_replacement:  Option<Replacement>,
}

impl DeviceRegex {
    pub(crate) fn from_yaml(yaml: &Yaml) -> Result<Vec<DeviceRegex>, UserAgentParserError> {
        let yaml_device_replacement = Yaml::String("device_replacement".to_string());
        let yaml_brand_replacement = Yaml::String("brand_replacement".to_string());
        let yaml_model_replacement = Yaml::String("model_replacement".to_string());

        from_yaml_section(yaml, |regex, yaml| {
            Ok(DeviceRegex {
                regex,
                device_replacement: optional_replacement(yaml, &yaml_device_replacement)?,
                brand_replacement: optional_replacement(yaml, &yaml_brand_replacement)?,
                model_replacement: optional_replacement(yaml, &yaml_model_replacement)?,
            })
        })
    }
}