ratio.rs (1036B)
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 9 use crate::parser::{Parse, ParserContext}; 10 use crate::values::generics::ratio::Ratio as GenericRatio; 11 use crate::values::specified::NonNegativeNumber; 12 use crate::One; 13 use cssparser::Parser; 14 use style_traits::ParseError; 15 16 /// A specified <ratio> value. 17 pub type Ratio = GenericRatio<NonNegativeNumber>; 18 19 impl 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 }