caniuse_serde/
AgentName.rs

1// This file is part of caniuse-serde. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/caniuse-serde/master/COPYRIGHT. No part of predicator, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
2// Copyright © 2017 The developers of caniuse-serde. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/caniuse-serde/master/COPYRIGHT.
3
4
5/// The name of this agent
6#[allow(missing_docs)]
7#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
8pub enum AgentName
9{
10	MicrosoftInternetExplorer,
11	MicrosoftEdge,
12	MozillaFirefox,
13	GoogleChrome,
14	AppleSafari,
15	Opera,
16	AppleSafariIOs,
17	OperaMini,
18	GoogleAndroidBrowserAndWebComponent,
19	Blackberry,
20	OperaMobile,
21	GoogleChromeAndroid,
22	MozillaFirefoxAndroid,
23	MicrosoftInternetExplorerMobile,
24	UcBrowserAndroid,
25	SamsungBrowserAndroid,
26	QqBrowserAndroid,
27	BaiduBrowserAndroid,
28	
29	#[doc(hidden)] __Nonexhaustive,
30	
31	/// An agent that did not exist in the caniuse.com data when this library was created
32	Unknown(String),
33}
34
35impl Default for AgentName
36{
37	/// Defaults to AgentName::GoogleChrome, the (as of October 2017) most common user agent
38	#[inline(always)]
39	fn default() -> Self
40	{
41		AgentName::GoogleChrome
42	}
43}
44
45impl<'de> Deserialize<'de> for AgentName
46{
47	/// Deserialize using Serde
48	#[inline(always)]
49	fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
50	{
51		struct AgentNameVisitor;
52		
53		impl<'de> Visitor<'de> for AgentNameVisitor
54		{
55			type Value = AgentName;
56			
57			#[inline(always)]
58			fn expecting(&self, formatter: &mut Formatter) -> fmt::Result
59			{
60				formatter.write_str("an era name starting with 'e' followed by a signed integer")
61			}
62			
63			#[inline(always)]
64			fn visit_str<E: de::Error>(self, v: &str) -> Result<Self::Value, E>
65			{
66				use self::AgentName::*;
67				
68				let result = match v
69				{
70					"ie" => MicrosoftInternetExplorer,
71					"edge" => MicrosoftEdge,
72					"firefox" => MozillaFirefox,
73					"chrome" => GoogleChrome,
74					"safari" => AppleSafari,
75					"opera" => Opera,
76					"ios_saf" => AppleSafariIOs,
77					"op_mini" => OperaMini,
78					"android" => GoogleAndroidBrowserAndWebComponent,
79					"bb" => Blackberry,
80					"op_mob" => OperaMobile,
81					"and_chr" => GoogleChromeAndroid,
82					"and_ff" => MozillaFirefoxAndroid,
83					"ie_mob" => MicrosoftInternetExplorerMobile,
84					"and_uc" => UcBrowserAndroid,
85					"samsung" => SamsungBrowserAndroid,
86					"and_qq" => QqBrowserAndroid,
87					"baidu" => BaiduBrowserAndroid,
88					
89					_ => Unknown(v.to_owned()),
90				};
91				Ok(result)
92			}
93			
94			#[inline(always)]
95			fn visit_string<E: de::Error>(self, v: String) -> Result<Self::Value, E>
96			{
97				use self::AgentName::*;
98				
99				let result = match &v[..]
100				{
101					"ie" => MicrosoftInternetExplorer,
102					"edge" => MicrosoftEdge,
103					"firefox" => MozillaFirefox,
104					"chrome" => GoogleChrome,
105					"safari" => AppleSafari,
106					"opera" => Opera,
107					"ios_saf" => AppleSafariIOs,
108					"op_mini" => OperaMini,
109					"android" => GoogleAndroidBrowserAndWebComponent,
110					"bb" => Blackberry,
111					"op_mob" => OperaMobile,
112					"and_chr" => GoogleChromeAndroid,
113					"and_ff" => MozillaFirefoxAndroid,
114					"ie_mob" => MicrosoftInternetExplorerMobile,
115					"and_uc" => UcBrowserAndroid,
116					"samsung" => SamsungBrowserAndroid,
117					"and_qq" => QqBrowserAndroid,
118					"baidu" => BaiduBrowserAndroid,
119					
120					_ => Unknown(v),
121				};
122				Ok(result)
123			}
124		}
125		
126		deserializer.deserialize_str(AgentNameVisitor)
127	}
128}
129
130impl AgentName
131{
132	/// Given an agent name and the CanIUse database, find the associated agent.
133	/// Returns None if this agent is not defined (this is typically either due to a typo or different versions of the caniuse.com database).
134	#[inline(always)]
135	pub fn agent<'a>(&'a self, can_i_use: &'a CanIUse) -> Option<Agent<'a>>
136	{
137		can_i_use.agent(self)
138	}
139}