lewp_css/domain/units/
unit.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::conversions::{
6        FontRelativeLengthConversion,
7        PercentageConversion,
8        ViewportPercentageLengthConversion,
9    },
10    crate::{
11        domain::{
12            expressions::{CalcExpression, CalculablePropertyValue},
13            numbers::{CssNumber, CssNumberNewType},
14        },
15        parsers::ParserContext,
16        CustomParseError::{self, *},
17    },
18    cssparser::{ParseError, Parser, ToCss},
19    either::{Either, Right},
20};
21
22pub trait Unit:
23    Sized + ToCss + Default + CssNumberNewType<<Self as Unit>::Number>
24{
25    type Number: CssNumber;
26
27    const HasDimension: bool;
28
29    fn parse_one_outside_calc_function<'i, 't>(
30        context: &ParserContext,
31        input: &mut Parser<'i, 't>,
32    ) -> Result<
33        CalculablePropertyValue<Self>,
34        ParseError<'i, CustomParseError<'i>>,
35    >;
36
37    fn parse_one_inside_calc_function<'i, 't>(
38        context: &ParserContext,
39        input: &mut Parser<'i, 't>,
40    ) -> Result<
41        Either<CalculablePropertyValue<Self>, CalcExpression<Self>>,
42        ParseError<'i, CustomParseError<'i>>,
43    >;
44
45    #[inline(always)]
46    fn to_canonical_dimension(self) -> Self {
47        self
48    }
49
50    fn to_canonical_dimension_value<
51        Conversion: FontRelativeLengthConversion<Self::Number>
52            + ViewportPercentageLengthConversion<Self::Number>
53            + PercentageConversion<Self::Number>,
54    >(
55        &self,
56        conversion: &Conversion,
57    ) -> Self::Number;
58
59    fn from_raw_css_for_var_expression_evaluation(
60        value: &str,
61        is_not_in_page_rule: bool,
62    ) -> Option<Self>;
63
64    #[inline(always)]
65    fn number_inside_calc_function<'i>(
66        value: f32,
67    ) -> Result<
68        Either<CalculablePropertyValue<Self>, CalcExpression<Self>>,
69        ParseError<'i, CustomParseError<'i>>,
70    > {
71        let constant =
72            Self::Number::new(value).map_err(|cssNumberConversionError| {
73                ParseError::from(CouldNotParseCssUnsignedNumber(
74                    cssNumberConversionError,
75                    value,
76                ))
77            })?;
78        Ok(Right(CalcExpression::Number(constant)))
79    }
80}