lewp_css/domain/at_rules/font_feature_values/
pair_values.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    crate::{
6        parsers::{Parse, ParserContext},
7        CustomParseError,
8    },
9    cssparser::{ParseError, Parser, ToCss, Token},
10    std::fmt,
11};
12
13/// A @font-feature-values block declaration value that keeps one or two values.
14#[derive(Clone, Debug, PartialEq)]
15pub struct PairValues(pub u32, pub Option<u32>);
16
17impl Parse for PairValues {
18    fn parse<'i, 't>(
19        _context: &ParserContext,
20        input: &mut Parser<'i, 't>,
21    ) -> Result<PairValues, ParseError<'i, CustomParseError<'i>>> {
22        let first = match *input.next()? {
23            Token::Number {
24                int_value: Some(firstValue),
25                ..
26            } if firstValue >= 0 => firstValue as u32,
27            ref unexpectedToken => {
28                return CustomParseError::unexpectedToken(unexpectedToken)
29            }
30        };
31
32        match input.next() {
33            Ok(&Token::Number {
34                int_value: Some(secondValue),
35                ..
36            }) if secondValue >= 0 => {
37                Ok(PairValues(first, Some(secondValue as u32)))
38            }
39
40            // It can't be anything other than number.
41            Ok(unexpectedToken) => {
42                CustomParseError::unexpectedToken(unexpectedToken)
43            }
44
45            // It can be just one value.
46            Err(_) => Ok(PairValues(first, None)),
47        }
48    }
49}
50
51impl ToCss for PairValues {
52    fn to_css<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result {
53        self.0.to_css(dest)?;
54
55        if let Some(second) = self.1 {
56            dest.write_char(' ')?;
57            second.to_css(dest)?;
58        }
59        Ok(())
60    }
61}