lewp_css/domain/at_rules/media/
media_resolution.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        domain::{
7            expressions::CalculablePropertyValue,
8            numbers::{CssNumber, CssSignedNumber},
9            units::{ResolutionUnit, Unit},
10        },
11        parsers::ParserContext,
12        CustomParseError,
13    },
14    cssparser::{ParseError, Parser, ToCss, Token},
15    std::fmt,
16};
17
18/// A resolution.
19#[derive(Clone, Debug, PartialEq)]
20pub enum MediaResolution {
21    infinite,
22
23    finite(CalculablePropertyValue<ResolutionUnit<CssSignedNumber>>),
24}
25
26impl ToCss for MediaResolution {
27    fn to_css<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result {
28        use self::MediaResolution::*;
29
30        match *self {
31            infinite => dest.write_str("infinite"),
32            finite(ref value) => value.to_css(dest),
33        }
34    }
35}
36
37impl MediaResolution {
38    // WebKit only supports integer values of 1 and 2, but we more liberally support floating point values of any value, positive or negative or zero
39    pub(crate) fn parseWebKit<'i, 't>(
40        input: &mut Parser<'i, 't>,
41    ) -> Result<Self, ParseError<'i, CustomParseError<'i>>> {
42        use self::MediaResolution::*;
43
44        let value = match *input.next()? {
45            Token::Number { value, .. } => CssSignedNumber::new(value).map_err(
46                |cssNumberConversionError| {
47                    ParseError::from(
48                        CustomParseError::CouldNotParseCssSignedNumber(
49                            cssNumberConversionError,
50                            value,
51                        ),
52                    )
53                },
54            ),
55
56            ref unexpectedToken => {
57                CustomParseError::unexpectedToken(unexpectedToken)
58            }
59        }?;
60
61        Ok(finite(CalculablePropertyValue::Constant(
62            ResolutionUnit::dppx(value),
63        )))
64    }
65
66    pub(crate) fn parse<'i, 't>(
67        context: &ParserContext,
68        input: &mut Parser<'i, 't>,
69    ) -> Result<Self, ParseError<'i, CustomParseError<'i>>> {
70        use self::MediaResolution::*;
71
72        if input.r#try(|i| i.expect_ident_matching("auto")).is_ok() {
73            return Ok(infinite);
74        }
75
76        Ok(finite(ResolutionUnit::parse_one_outside_calc_function(
77            context, input,
78        )?))
79    }
80}