style/typed_om/
numeric_declaration.rs1use crate::derives::*;
8use crate::parser::{Parse, ParserContext};
9use crate::typed_om::numeric::NoCalcNumeric;
10use crate::values::specified::calc::{CalcNode, CalcParseFlags, PercentageContext};
11use crate::values::specified::{
12 NoCalcAngle, NoCalcLength, NoCalcNumber, NoCalcPercentage, NoCalcTime,
13};
14use cssparser::{Parser, Token};
15use style_traits::values::specified::AllowedNumericType;
16use style_traits::{ParseError, StyleParseErrorKind};
17
18#[derive(Clone, ToTyped)]
20pub enum NumericDeclaration {
21 NoCalc(NoCalcNumeric),
23
24 Calc(CalcNode),
28}
29
30impl Parse for NumericDeclaration {
31 fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ParseError> {
33 let token = input.next()?;
35
36 match *token {
38 Token::Number { value, .. } => Ok(Self::NoCalc(NoCalcNumeric::Number(
39 NoCalcNumber::new(value),
40 ))),
41
42 Token::Percentage { unit_value, .. } => Ok(Self::NoCalc(NoCalcNumeric::Percentage(
43 NoCalcPercentage::new(unit_value),
44 ))),
45
46 Token::Dimension {
47 value, ref unit, ..
48 } => {
49 if let Ok(length) = NoCalcLength::parse_dimension_with_context(context, value, unit)
50 {
51 return Ok(Self::NoCalc(NoCalcNumeric::Length(length)));
52 }
53
54 if let Ok(angle) = NoCalcAngle::parse_dimension(value, unit) {
55 return Ok(Self::NoCalc(NoCalcNumeric::Angle(angle)));
56 }
57
58 if let Ok(time) = NoCalcTime::parse_dimension(value, unit) {
59 return Ok(Self::NoCalc(NoCalcNumeric::Time(time)));
60 }
61
62 Err(ParseError::unexpected_token())
63
64 },
69
70 Token::Function(ref name) => {
71 let function = CalcNode::math_function(context, name)?;
72 let node = CalcNode::parse(
73 context,
74 input,
75 function,
76 CalcParseFlags::new(PercentageContext::allowed()),
77 )?;
78
79 let allow_all_types = AllowedNumericType::All;
80 let _ = node
81 .clone()
82 .into_length_or_percentage(allow_all_types)
83 .map_err(|()| ParseError::custom(StyleParseErrorKind::UnspecifiedError))?;
84
85 Ok(Self::Calc(node))
88 },
89
90 _ => Err(ParseError::unexpected_token()),
91 }
92 }
93}