Skip to main content

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(context: &ParserContext, input: &mut Parser) -> Result<Self, ParseError> {
21        let a = NonNegativeNumber::parse(context, input)?;
22        let b = match input.try_parse(|input| input.expect_delim('/')) {
23            Ok(()) => NonNegativeNumber::parse(context, input)?,
24            _ => One::one(),
25        };
26
27        Ok(GenericRatio(a, b))
28    }
29}