Skip to main content

rspack_lightningcss/values/
rect.rs

1//! Generic values for four sided 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 for four sides of a box,
10/// e.g. border-width, margin, padding, etc.
11///
12/// When serialized, as few components as possible are written when
13/// there are duplicate values.
14#[derive(Clone, Debug, PartialEq, Eq)]
15#[cfg_attr(feature = "visitor", derive(Visit))]
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(rspack_lightningcss_static_self::IntoOwned))]
19pub struct Rect<T>(
20  /// The top component.
21  pub T,
22  /// The right component.
23  pub T,
24  /// The bottom component.
25  pub T,
26  /// The left component.
27  pub T,
28);
29
30impl<T> Rect<T> {
31  /// Returns a new `Rect<T>` value.
32  pub fn new(first: T, second: T, third: T, fourth: T) -> Self {
33    Rect(first, second, third, fourth)
34  }
35}
36
37impl<T: Default + Clone> Default for Rect<T> {
38  fn default() -> Rect<T> {
39    Rect::all(T::default())
40  }
41}
42
43impl<T> Rect<T>
44where
45  T: Clone,
46{
47  /// Returns a rect with all the values equal to `v`.
48  pub fn all(v: T) -> Self {
49    Rect::new(v.clone(), v.clone(), v.clone(), v)
50  }
51
52  /// Parses a new `Rect<T>` value with the given parse function.
53  pub fn parse_with<'i, 't, Parse>(
54    input: &mut Parser<'i, 't>,
55    parse: Parse,
56  ) -> Result<Self, ParseError<'i, ParserError<'i>>>
57  where
58    Parse: Fn(&mut Parser<'i, 't>) -> Result<T, ParseError<'i, ParserError<'i>>>,
59  {
60    let first = parse(input)?;
61    let second = if let Ok(second) = input.try_parse(|i| parse(i)) {
62      second
63    } else {
64      // <first>
65      return Ok(Self::new(first.clone(), first.clone(), first.clone(), first));
66    };
67    let third = if let Ok(third) = input.try_parse(|i| parse(i)) {
68      third
69    } else {
70      // <first> <second>
71      return Ok(Self::new(first.clone(), second.clone(), first, second));
72    };
73    let fourth = if let Ok(fourth) = input.try_parse(|i| parse(i)) {
74      fourth
75    } else {
76      // <first> <second> <third>
77      return Ok(Self::new(first, second.clone(), third, second));
78    };
79    // <first> <second> <third> <fourth>
80    Ok(Self::new(first, second, third, fourth))
81  }
82}
83
84impl<'i, T> Parse<'i> for Rect<T>
85where
86  T: Clone + PartialEq + Parse<'i>,
87{
88  fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
89    Self::parse_with(input, T::parse)
90  }
91}
92
93impl<T> ToCss for Rect<T>
94where
95  T: PartialEq + ToCss,
96{
97  fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
98    self.0.to_css(dest)?;
99    let same_vertical = self.0 == self.2;
100    let same_horizontal = self.1 == self.3;
101    if same_vertical && same_horizontal && self.0 == self.1 {
102      return Ok(());
103    }
104    dest.write_str(" ")?;
105    self.1.to_css(dest)?;
106    if same_vertical && same_horizontal {
107      return Ok(());
108    }
109    dest.write_str(" ")?;
110    self.2.to_css(dest)?;
111    if same_horizontal {
112      return Ok(());
113    }
114    dest.write_str(" ")?;
115    self.3.to_css(dest)
116  }
117}
118
119impl<T: IsCompatible> IsCompatible for Rect<T> {
120  fn is_compatible(&self, browsers: crate::targets::Browsers) -> bool {
121    self.0.is_compatible(browsers)
122      && self.1.is_compatible(browsers)
123      && self.2.is_compatible(browsers)
124      && self.3.is_compatible(browsers)
125  }
126}