caniuse_serde/FeatureName.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/// A feature name is a lower case, possibly hyphenated string representing a particular HTML, CSS or like feature that agents may not have support for.
6#[derive(Deserialize, Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
7pub struct FeatureName(String);
8
9impl<I: Into<String>> From<I> for FeatureName
10{
11 /// Create a FeatureName from anything that can be converted into a String
12 #[inline(always)]
13 fn from(value: I) -> Self
14 {
15 FeatureName(value.into())
16 }
17}
18
19impl FromStr for FeatureName
20{
21 type Err = ();
22
23 /// Create a FeatureName from a &str
24 #[inline(always)]
25 fn from_str(s: &str) -> Result<Self, Self::Err>
26 {
27 Ok(FeatureName(s.to_owned()))
28 }
29}
30
31impl Deref for FeatureName
32{
33 type Target = str;
34
35 /// Dereference a FeatureName to a &str.
36 /// May be removed in the future if the definition of FeatureName changes.
37 #[inline(always)]
38 fn deref(&self) -> &Self::Target
39 {
40 &self.0
41 }
42}
43
44impl FeatureName
45{
46 /// Given a feature name and the CanIUse database, find the associated feature.
47 /// Returns None if this feature is not defined (this is typically either due to a typo or different versions of the caniuse.com database).
48 #[inline(always)]
49 pub fn feature<'a>(&'a self, can_i_use: &'a CanIUse) -> Option<Feature<'a>>
50 {
51 can_i_use.feature(self)
52 }
53}