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