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)]
19pub enum NumericDeclaration {
20 NoCalc(NoCalcNumeric),
22
23 Calc(CalcNode),
27}
28
29impl Parse for NumericDeclaration {
30 fn parse<'i, 't>(
32 context: &ParserContext,
33 input: &mut Parser<'i, 't>,
34 ) -> Result<Self, ParseError<'i>> {
35 let location = input.current_source_location();
36
37 let token = input.next()?;
39
40 match *token {
42 Token::Dimension {
43 value, ref unit, ..
44 } => {
45 NoCalcLength::parse_dimension_with_context(context, value, unit)
46 .map(NoCalcNumeric::Length)
47 .map(Self::NoCalc)
48 .map_err(|()| location.new_unexpected_token_error(token.clone()))
49
50 },
57
58 Token::Function(ref name) => {
59 let function = CalcNode::math_function(context, name, location)?;
60 let allow_all_units = AllowParse::new(CalcUnits::ALL);
61 let node = CalcNode::parse(context, input, function, allow_all_units)?;
62
63 let allow_all_types = AllowedNumericType::All;
64 let _ = node
65 .clone()
66 .into_length_or_percentage(allow_all_types)
67 .map_err(|()| {
68 location.new_custom_error(StyleParseErrorKind::UnspecifiedError)
69 })?;
70
71 Ok(Self::Calc(node))
74 },
75
76 ref token => return Err(location.new_unexpected_token_error(token.clone())),
77 }
78 }
79}