lewp_css/domain/properties/
css_wide_keyword.rs

1// This file is part of css. 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/css/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 css. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/css/master/COPYRIGHT.
3
4use {
5    cssparser::{Parser, ToCss},
6    std::fmt,
7};
8
9/// An enum to represent a CSS-wide keyword.
10#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
11pub enum CssWideKeyword {
12    /// The `initial` keyword.
13    initial,
14
15    /// The `inherit` keyword.
16    inherit,
17
18    /// The `unset` keyword.
19    unset,
20}
21
22impl ToCss for CssWideKeyword {
23    fn to_css<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result {
24        use self::CssWideKeyword::*;
25
26        let value = match *self {
27            initial => "initial",
28            inherit => "inherit",
29            unset => "unset",
30        };
31
32        dest.write_str(value)
33    }
34}
35
36impl CssWideKeyword {
37    #[inline(always)]
38    pub fn to_str(&self) -> &'static str {
39        use self::CssWideKeyword::*;
40
41        match *self {
42            initial => "initial",
43            inherit => "inherit",
44            unset => "unset",
45        }
46    }
47
48    #[inline(always)]
49    fn from_ident<'i>(ident: &str) -> Option<Self> {
50        use self::CssWideKeyword::*;
51
52        match_ignore_ascii_case! {
53            ident,
54
55            "initial" => Some(initial),
56
57            "inherit" => Some(inherit),
58
59            "unset" => Some(unset),
60
61            _ => None
62        }
63    }
64
65    #[inline(always)]
66    pub(crate) fn parse(input: &mut Parser) -> Result<Self, ()> {
67        let ident = input.expect_ident().map_err(|_| ())?.clone();
68        input.expect_exhausted().map_err(|_| ())?;
69        Self::from_ident(&ident).ok_or(())
70    }
71}