/rust/registry/src/index.crates.io-1949cf8c6b5b557f/lightningcss-1.0.0-alpha.72/src/values/ratio.rs
Line | Count | Source |
1 | | //! CSS ratio values. |
2 | | |
3 | | use super::number::CSSNumber; |
4 | | use crate::error::{ParserError, PrinterError}; |
5 | | use crate::printer::Printer; |
6 | | use crate::traits::{Parse, ToCss}; |
7 | | #[cfg(feature = "visitor")] |
8 | | use crate::visitor::Visit; |
9 | | use cssparser::*; |
10 | | |
11 | | /// A CSS [`<ratio>`](https://www.w3.org/TR/css-values-4/#ratios) value, |
12 | | /// representing the ratio of two numeric values. |
13 | | #[derive(Debug, Clone, PartialEq)] |
14 | 0 | #[cfg_attr(feature = "visitor", derive(Visit))] Unexecuted instantiation: <lightningcss::values::ratio::Ratio as lightningcss::visitor::Visit<lightningcss::parser::DefaultAtRule, fallow_extract::css_metrics::ValueCollector>>::visit_children Unexecuted instantiation: <lightningcss::values::ratio::Ratio as lightningcss::visitor::Visit<lightningcss::parser::DefaultAtRule, fallow_extract::css_metrics::FirstRgbColorCollector>>::visit_children Unexecuted instantiation: <lightningcss::values::ratio::Ratio as lightningcss::visitor::Visit<_, _>>::visit_children |
15 | | #[cfg_attr(feature = "visitor", visit(visit_ratio, RATIOS))] |
16 | | #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
17 | | #[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))] |
18 | | #[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))] |
19 | | pub struct Ratio(pub CSSNumber, pub CSSNumber); |
20 | | |
21 | | impl<'i> Parse<'i> for Ratio { |
22 | 1 | fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> { |
23 | 1 | let first = CSSNumber::parse(input)?; |
24 | 0 | let second = if input.try_parse(|input| input.expect_delim('/')).is_ok() { |
25 | 0 | CSSNumber::parse(input)? |
26 | | } else { |
27 | 0 | 1.0 |
28 | | }; |
29 | | |
30 | 0 | Ok(Ratio(first, second)) |
31 | 1 | } |
32 | | } |
33 | | |
34 | | impl Ratio { |
35 | | /// Parses a ratio where both operands are required. |
36 | 24.6k | pub fn parse_required<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> { |
37 | 24.6k | let first = CSSNumber::parse(input)?; |
38 | 9.53k | input.expect_delim('/')?; |
39 | 13 | let second = CSSNumber::parse(input)?; |
40 | 12 | Ok(Ratio(first, second)) |
41 | 24.6k | } |
42 | | } |
43 | | |
44 | | impl ToCss for Ratio { |
45 | 0 | fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError> |
46 | 0 | where |
47 | 0 | W: std::fmt::Write, |
48 | | { |
49 | 0 | self.0.to_css(dest)?; |
50 | 0 | if self.1 != 1.0 { |
51 | 0 | dest.delim('/', true)?; |
52 | 0 | self.1.to_css(dest)?; |
53 | 0 | } |
54 | 0 | Ok(()) |
55 | 0 | } |
56 | | } |
57 | | |
58 | | impl std::ops::Add<CSSNumber> for Ratio { |
59 | | type Output = Self; |
60 | | |
61 | 0 | fn add(self, other: CSSNumber) -> Ratio { |
62 | 0 | Ratio(self.0 + other, self.1) |
63 | 0 | } |
64 | | } |