lewp_css/domain/at_rules/counter_style/
additive_tuple.rs

1use {
2    super::Symbol,
3    crate::{
4        parsers::{
5            separators::{Comma, Separated},
6            Parse,
7            ParserContext,
8        },
9        CustomParseError,
10    },
11    cssparser::{ParseError, Parser, ToCss},
12    std::fmt,
13};
14
15// 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.
16// 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.
17
18/// <integer> && <symbol>
19#[derive(Clone, Debug)]
20pub struct AdditiveTuple {
21    /// <integer>
22    pub weight: u32,
23
24    /// <symbol>
25    pub symbol: Symbol,
26}
27
28impl ToCss for AdditiveTuple {
29    fn to_css<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result {
30        self.weight.to_css(dest)?;
31        dest.write_char(' ')?;
32        self.symbol.to_css(dest)
33    }
34}
35
36impl Separated for AdditiveTuple {
37    type Delimiter = Comma;
38}
39
40impl Parse for AdditiveTuple {
41    fn parse<'i, 't>(
42        context: &ParserContext,
43        input: &mut Parser<'i, 't>,
44    ) -> Result<Self, ParseError<'i, CustomParseError<'i>>> {
45        let symbol = input.r#try(|input| Symbol::parse(context, input));
46        let weight = input.expect_integer()?;
47        if weight < 0 {
48            return Err(ParseError::from(
49                CustomParseError::CounterStyleAdditiveTupleWeightCanNotBeNegative(weight),
50            ));
51        }
52        let symbol = symbol.or_else(|_| Symbol::parse(context, input))?;
53        Ok(Self {
54            weight: weight as u32,
55            symbol,
56        })
57    }
58}