lewp_css/domain/properties/
importance.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    super::HasImportance,
6    crate::CustomParseError,
7    cssparser::{parse_important, ParseError, Parser, ToCss},
8    std::fmt,
9    Importance::*,
10};
11
12/// A declaration [importance][importance].
13///
14/// [importance]: <https://drafts.csswg.org/css-cascade/#importance>
15#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
16pub enum Importance {
17    /// Indicates a declaration without `!important`.
18    Normal,
19
20    /// Indicates a declaration with `!important`.
21    Important,
22}
23
24impl Default for Importance {
25    #[inline(always)]
26    fn default() -> Self {
27        Importance::Normal
28    }
29}
30
31impl ToCss for Importance {
32    fn to_css<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result {
33        match *self {
34            Normal => Ok(()),
35            Important => dest.write_str("!important"),
36        }
37    }
38}
39
40impl HasImportance for Importance {
41    #[inline(always)]
42    fn validateParsedImportance<'i>(
43        importance: Importance,
44    ) -> Result<Self, ParseError<'i, CustomParseError<'i>>> {
45        Ok(importance)
46    }
47
48    /// Return whether this is an important declaration.
49    #[inline(always)]
50    fn isImportant(&self) -> bool {
51        match *self {
52            Normal => false,
53            Important => true,
54        }
55    }
56}
57
58impl Importance {
59    #[inline(always)]
60    pub fn from_bool(isImportant: bool) -> Self {
61        if isImportant {
62            Important
63        } else {
64            Normal
65        }
66    }
67
68    #[inline(always)]
69    pub(crate) fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Self {
70        Self::from_bool(input.r#try(parse_important).is_ok())
71    }
72}