1use crate::parser::{Parse, ParserContext};
8use cssparser::Parser;
9use std::fmt::{self, Write};
10use style_traits::{CssWriter, ParseError, ToCss};
11
12#[derive(
15 Animate,
16 Clone,
17 ComputeSquaredDistance,
18 Copy,
19 Debug,
20 Deserialize,
21 MallocSizeOf,
22 PartialEq,
23 SpecifiedValueInfo,
24 Serialize,
25 ToAnimatedValue,
26 ToAnimatedZero,
27 ToComputedValue,
28 ToResolvedValue,
29 ToShmem,
30 ToTyped,
31)]
32#[repr(C)]
33pub struct Rect<T>(pub T, pub T, pub T, pub T);
34
35impl<T> Rect<T> {
36 pub fn new(first: T, second: T, third: T, fourth: T) -> Self {
38 Rect(first, second, third, fourth)
39 }
40}
41
42impl<T> Rect<T>
43where
44 T: Clone,
45{
46 pub fn all(v: T) -> Self {
48 Rect::new(v.clone(), v.clone(), v.clone(), v)
49 }
50
51 pub fn parse_with<'i, 't, Parse>(
53 context: &ParserContext,
54 input: &mut Parser<'i, 't>,
55 parse: Parse,
56 ) -> Result<Self, ParseError<'i>>
57 where
58 Parse: Fn(&ParserContext, &mut Parser<'i, 't>) -> Result<T, ParseError<'i>>,
59 {
60 let first = parse(context, input)?;
61 let second = if let Ok(second) = input.try_parse(|i| parse(context, i)) {
62 second
63 } else {
64 return Ok(Self::new(
66 first.clone(),
67 first.clone(),
68 first.clone(),
69 first,
70 ));
71 };
72 let third = if let Ok(third) = input.try_parse(|i| parse(context, i)) {
73 third
74 } else {
75 return Ok(Self::new(first.clone(), second.clone(), first, second));
77 };
78 let fourth = if let Ok(fourth) = input.try_parse(|i| parse(context, i)) {
79 fourth
80 } else {
81 return Ok(Self::new(first, second.clone(), third, second));
83 };
84 Ok(Self::new(first, second, third, fourth))
86 }
87
88 pub fn parse_all_components_with<'i, 't, Parse>(
91 context: &ParserContext,
92 input: &mut Parser<'i, 't>,
93 parse: Parse,
94 ) -> Result<Self, ParseError<'i>>
95 where
96 Parse: Fn(&ParserContext, &mut Parser<'i, 't>) -> Result<T, ParseError<'i>>,
97 {
98 let first = parse(context, input)?;
99 let second = parse(context, input)?;
100 let third = parse(context, input)?;
101 let fourth = parse(context, input)?;
102 Ok(Self::new(first, second, third, fourth))
104 }
105}
106
107impl<T> Parse for Rect<T>
108where
109 T: Clone + Parse,
110{
111 #[inline]
112 fn parse<'i, 't>(
113 context: &ParserContext,
114 input: &mut Parser<'i, 't>,
115 ) -> Result<Self, ParseError<'i>> {
116 Self::parse_with(context, input, T::parse)
117 }
118}
119
120impl<T> ToCss for Rect<T>
121where
122 T: PartialEq + ToCss,
123{
124 fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
125 where
126 W: Write,
127 {
128 self.0.to_css(dest)?;
129 let same_vertical = self.0 == self.2;
130 let same_horizontal = self.1 == self.3;
131 if same_vertical && same_horizontal && self.0 == self.1 {
132 return Ok(());
133 }
134 dest.write_char(' ')?;
135 self.1.to_css(dest)?;
136 if same_vertical && same_horizontal {
137 return Ok(());
138 }
139 dest.write_char(' ')?;
140 self.2.to_css(dest)?;
141 if same_horizontal {
142 return Ok(());
143 }
144 dest.write_char(' ')?;
145 self.3.to_css(dest)
146 }
147}