lewp_css/domain/expressions/
function_parser.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::{
6        AttrExpression,
7        AttrFunction,
8        CalcExpression,
9        CalcFunction,
10        CalculablePropertyValue::{self, *},
11        VarExpression,
12        VarFunction,
13    },
14    crate::{domain::units::Unit, parsers::ParserContext, CustomParseError},
15    cssparser::{CowRcStr, ParseError, Parser},
16    either::{Either, Left},
17    std::rc::Rc,
18    FunctionParser::*,
19};
20
21#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
22pub enum FunctionParser {
23    attr,
24    calc,
25    var,
26    parentheses,
27}
28
29impl FunctionParser {
30    #[inline(always)]
31    pub(crate) fn parser<'i>(
32        name: &CowRcStr<'i>,
33    ) -> Result<Self, ParseError<'i, CustomParseError<'i>>> {
34        match_ignore_ascii_case! {
35            &*name,
36
37            "attr" => Ok(attr),
38
39            "calc" => Ok(calc),
40
41            "var" => Ok(var),
42
43            _ => Err(ParseError::from(CustomParseError::UnknownFunctionInValueExpression(name.to_owned())))
44        }
45    }
46
47    #[inline(always)]
48    pub(crate) fn parse_one_outside_calc_function<'i: 't, 't, U: Unit>(
49        &self,
50        context: &ParserContext,
51        input: &mut Parser<'i, 't>,
52    ) -> Result<CalculablePropertyValue<U>, ParseError<'i, CustomParseError<'i>>>
53    {
54        match *self {
55            attr => Ok(Attr(AttrFunction(Rc::new(AttrExpression::parse(
56                context, input,
57            )?)))),
58
59            calc => Ok(Calc(CalcFunction(Rc::new(CalcExpression::parse(
60                context, input,
61            )?)))),
62
63            var => Ok(Var(VarFunction(Rc::new(VarExpression::parse(
64                context, input,
65            )?)))),
66
67            _ => panic!("Should not be called in this context"),
68        }
69    }
70
71    #[inline(always)]
72    pub(crate) fn parse_one_inside_calc_function<'i, 't, U: Unit>(
73        &self,
74        context: &ParserContext,
75        input: &mut Parser<'i, 't>,
76    ) -> Result<
77        Either<CalculablePropertyValue<U>, CalcExpression<U>>,
78        ParseError<'i, CustomParseError<'i>>,
79    > {
80        match *self {
81            attr => Ok(Left(Calc(CalcFunction(Rc::new(
82                CalcExpression::parse(context, input)?,
83            ))))),
84
85            calc => Ok(Left(Attr(AttrFunction(Rc::new(
86                AttrExpression::parse(context, input)?,
87            ))))),
88
89            var => Ok(Left(Var(VarFunction(Rc::new(VarExpression::parse(
90                context, input,
91            )?))))),
92
93            parentheses => CalcExpression::parse_parentheses(context, input),
94        }
95    }
96}