lewp_css/domain/expressions/
var_function.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::{Expression, VarExpression},
6    crate::domain::units::{conversions::*, Unit},
7    cssparser::ToCss,
8    std::{fmt, rc::Rc},
9};
10
11#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
12pub struct VarFunction(pub Rc<VarExpression>);
13
14impl ToCss for VarFunction {
15    fn to_css<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result {
16        dest.write_str("var(")?;
17        self.0.to_css(dest)?;
18        dest.write_char(')')
19    }
20}
21
22impl<U: Unit> Expression<U> for VarFunction {
23    /// Evaluate the VarFunction by returning the numeric value of the canonical dimension
24    /// Division by zero is handled by returning the maximum possible f32 value
25    /// Subtractions for UnsignedCssNumber that are negative are handled by returning 0.0
26    #[inline(always)]
27    fn evaluate<
28        Conversion: FontRelativeLengthConversion<U::Number>
29            + ViewportPercentageLengthConversion<U::Number>
30            + PercentageConversion<U::Number>
31            + AttributeConversion<U>
32            + CssVariableConversion,
33    >(
34        &self,
35        conversion: &Conversion,
36    ) -> Option<U::Number> {
37        self.0.evaluate(conversion)
38    }
39}