style/typed_om/
numeric_declaration.rs1use crate::derives::*;
8use crate::parser::{Parse, ParserContext};
9use crate::typed_om::numeric_values::NoCalcNumeric;
10use crate::values::generics::calc::CalcUnits;
11use crate::values::specified::calc::{AllowParse, CalcNode};
12use crate::values::specified::NoCalcLength;
13use cssparser::{Parser, Token};
14use style_traits::values::specified::AllowedNumericType;
15use style_traits::{ParseError, StyleParseErrorKind};
16
17#[derive(Clone, ToTyped)]
19#[typed_value(derive_fields)]
20pub enum NumericDeclaration {
21 NoCalc(NoCalcNumeric),
23
24 Calc(CalcNode),
28}
29
30impl Parse for NumericDeclaration {
31 fn parse<'i, 't>(
33 context: &ParserContext,
34 input: &mut Parser<'i, 't>,
35 ) -> Result<Self, ParseError<'i>> {
36 let location = input.current_source_location();
37
38 let token = input.next()?;
40
41 match *token {
43 Token::Dimension {
44 value, ref unit, ..
45 } => {
46 NoCalcLength::parse_dimension_with_context(context, value, unit)
47 .map(NoCalcNumeric::Length)
48 .map(Self::NoCalc)
49 .map_err(|()| location.new_unexpected_token_error(token.clone()))
50
51 },
58
59 Token::Function(ref name) => {
60 let function = CalcNode::math_function(context, name, location)?;
61 let allow_all_units = AllowParse::new(CalcUnits::ALL);
62 let node = CalcNode::parse(context, input, function, allow_all_units)?;
63
64 let allow_all_types = AllowedNumericType::All;
65 let _ = node
66 .clone()
67 .into_length_or_percentage(allow_all_types)
68 .map_err(|()| {
69 location.new_custom_error(StyleParseErrorKind::UnspecifiedError)
70 })?;
71
72 Ok(Self::Calc(node))
75 },
76
77 ref token => return Err(location.new_unexpected_token_error(token.clone())),
78 }
79 }
80}