lightningcss/values/
size.rs

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