Skip to main content

rspack_lightningcss/values/
size.rs

1//! Generic values for two component properties.
2
3use crate::error::{ParserError, PrinterError};
4use crate::traits::{IsCompatible, Parse, ToCss};
5#[cfg(feature = "visitor")]
6use crate::visitor::Visit;
7use cssparser::*;
8
9/// A generic value that represents a value with two components, e.g. a border radius.
10///
11/// When serialized, only a single component will be written if both are equal.
12#[derive(Debug, Clone, PartialEq)]
13#[cfg_attr(feature = "visitor", derive(Visit))]
14#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
15#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
16#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
17pub struct Size2D<T>(pub T, pub T);
18
19impl<'i, T> Parse<'i> for Size2D<T>
20where
21  T: Parse<'i> + Clone,
22{
23  fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
24    let first = T::parse(input)?;
25    let second = input.try_parse(T::parse).unwrap_or_else(|_| first.clone());
26    Ok(Size2D(first, second))
27  }
28}
29
30impl<T> ToCss for Size2D<T>
31where
32  T: ToCss + PartialEq,
33{
34  fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
35    self.0.to_css(dest)?;
36    if self.1 != self.0 {
37      dest.write_str(" ")?;
38      self.1.to_css(dest)?;
39    }
40    Ok(())
41  }
42}
43
44impl<T: IsCompatible> IsCompatible for Size2D<T> {
45  fn is_compatible(&self, browsers: crate::targets::Browsers) -> bool {
46    self.0.is_compatible(browsers) && self.1.is_compatible(browsers)
47  }
48}