lewp_css/domain/at_rules/keyframes/
keyframe_selector.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::KeyframePercentage,
6    crate::CustomParseError,
7    cssparser::{ParseError, Parser, ToCss},
8    std::fmt,
9};
10
11/// A keyframes selector is a list of percentages or from/to symbols, which are converted at parse time to percentages.
12#[derive(Clone, Debug, Eq, PartialEq)]
13pub struct KeyframeSelector(pub Vec<KeyframePercentage>);
14
15impl ToCss for KeyframeSelector {
16    fn to_css<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result {
17        let mut iter = self.0.iter();
18        iter.next().unwrap().to_css(dest)?;
19        for percentage in iter {
20            dest.write_char(',')?;
21            percentage.to_css(dest)?;
22        }
23        Ok(())
24    }
25}
26
27impl KeyframeSelector {
28    /// Return the list of percentages this selector contains.
29    #[inline]
30    pub fn percentages(&self) -> &[KeyframePercentage] {
31        &self.0
32    }
33
34    /// Parse a keyframe selector from CSS input.
35    pub(crate) fn parse<'i, 't>(
36        input: &mut Parser<'i, 't>,
37    ) -> Result<Self, ParseError<'i, CustomParseError<'i>>> {
38        input
39            .parse_comma_separated(KeyframePercentage::parse)
40            .map(KeyframeSelector)
41    }
42}