style/values/specified/
ratio.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! Specified types for <ratio>.
6//!
7//! [ratio]: https://drafts.csswg.org/css-values/#ratios
8
9use crate::parser::{Parse, ParserContext};
10use crate::values::generics::ratio::Ratio as GenericRatio;
11use crate::values::specified::NonNegativeNumber;
12use crate::One;
13use cssparser::Parser;
14use style_traits::ParseError;
15
16/// A specified <ratio> value.
17pub type Ratio = GenericRatio<NonNegativeNumber>;
18
19impl Parse for Ratio {
20    fn parse<'i, 't>(
21        context: &ParserContext,
22        input: &mut Parser<'i, 't>,
23    ) -> Result<Self, ParseError<'i>> {
24        let a = NonNegativeNumber::parse(context, input)?;
25        let b = match input.try_parse(|input| input.expect_delim('/')) {
26            Ok(()) => NonNegativeNumber::parse(context, input)?,
27            _ => One::one(),
28        };
29
30        Ok(GenericRatio(a, b))
31    }
32}