Skip to main content

lewp_css/domain/properties/
property_declarations.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, PropertyDeclaration},
6    crate::{
7        domain::HasPropertyDeclarations,
8        parsers::{
9            property_declaration_parser::PropertyDeclarationParser,
10            ParserContext,
11        },
12        CustomParseError,
13    },
14    cssparser::{DeclarationListParser, ParseError, Parser, ToCss},
15    std::{fmt, marker::PhantomData},
16};
17
18/// A list of property declarations
19#[derive(Default, Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
20pub struct PropertyDeclarations<I: HasImportance>(
21    pub Vec<PropertyDeclaration<I>>,
22);
23
24impl<I: HasImportance> ToCss for PropertyDeclarations<I> {
25    fn to_css<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result {
26        let length = self.0.len();
27        if length != 0 {
28            for index in 0..(length - 1) {
29                (unsafe { self.0.get_unchecked(index) }).to_css(dest)?;
30            }
31
32            (unsafe { self.0.get_unchecked(length - 1) })
33                .to_css_without_trailing_semicolon(dest)
34        } else {
35            Ok(())
36        }
37    }
38}
39
40impl<I: HasImportance> HasPropertyDeclarations<I> for PropertyDeclarations<I> {
41    #[inline(always)]
42    fn property_declarations(&self) -> &PropertyDeclarations<I> {
43        self
44    }
45
46    #[inline(always)]
47    fn property_declarations_mut(&mut self) -> &mut PropertyDeclarations<I> {
48        self
49    }
50
51    #[inline(always)]
52    fn property_declarations_slice(&self) -> &[PropertyDeclaration<I>] {
53        &self.0[..]
54    }
55
56    #[inline(always)]
57    fn property_declarations_vec(&self) -> &Vec<PropertyDeclaration<I>> {
58        &self.0
59    }
60
61    #[inline(always)]
62    fn property_declarations_vec_mut(
63        &mut self,
64    ) -> &mut Vec<PropertyDeclaration<I>> {
65        &mut self.0
66    }
67}
68
69impl<I: HasImportance> PropertyDeclarations<I> {
70    #[inline(always)]
71    pub fn is_empty(&self) -> bool {
72        self.0.is_empty()
73    }
74
75    // Parse a list of property declarations and return a property declaration block.
76    pub(crate) fn parse_property_declaration_list<'i: 't, 't>(
77        context: &ParserContext,
78        input: &mut Parser<'i, 't>,
79    ) -> Result<PropertyDeclarations<I>, ParseError<'i, CustomParseError<'i>>>
80    {
81        let mut propertyDeclarations = Vec::new();
82        let parsedPropertyDeclarations = DeclarationListParser::new(
83            input,
84            PropertyDeclarationParser {
85                context,
86                marker: PhantomData,
87            },
88        );
89
90        for propertyDeclaration in parsedPropertyDeclarations
91        {
92            match propertyDeclaration {
93                Ok(propertyDeclaration) => {
94                    propertyDeclarations.push(propertyDeclaration)
95                }
96                Err(preciseParseError) => return Err(preciseParseError.0),
97            }
98        }
99
100        Ok(PropertyDeclarations(propertyDeclarations))
101    }
102}