Skip to main content

css_ast/functions/
stripes_function.rs

1use super::prelude::*;
2use crate::{types::Color, units::LengthPercentageOrFlex};
3
4/// <https://drafts.csswg.org/css-images-4/#typedef-image-1d>
5///
6/// ```text,ignore
7/// <stripes()> = stripes( <color-stripe># )
8/// <color-stripe> = <color> && [ <length-percentage> | <flex> ]?
9/// ```
10#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
11#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
12#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit)]
13#[derive(csskit_derives::NodeWithMetadata)]
14pub struct StripesFunction<'a> {
15	#[cfg_attr(feature = "visitable", visit(skip))]
16	#[atom(CssAtomSet::Stripes)]
17	pub name: T![Function],
18	pub params: CommaSeparated<'a, ColorStripe<'a>>,
19	#[cfg_attr(feature = "visitable", visit(skip))]
20	pub close: T![')'],
21}
22
23/// <https://drafts.csswg.org/css-images-4/#typedef-color-stripe>
24///
25/// ```text,ignore
26/// <color-stripe> = <color> && [ <length-percentage> | <flex> ]?
27/// ```
28#[derive(ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
29#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
30#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(children))]
31#[derive(csskit_derives::NodeWithMetadata)]
32pub struct ColorStripe<'a> {
33	pub color: Color<'a>,
34	pub thickness: Option<LengthPercentageOrFlex>,
35}
36
37impl<'a> Peek<'a> for ColorStripe<'a> {
38	const PEEK_KINDSET: KindSet = Color::PEEK_KINDSET.combine(LengthPercentageOrFlex::PEEK_KINDSET);
39
40	#[inline(always)]
41	fn peek<I>(p: &Parser<'a, I>, c: Cursor) -> bool
42	where
43		I: Iterator<Item = Cursor> + Clone,
44	{
45		Color::peek(p, c) || LengthPercentageOrFlex::peek(p, c)
46	}
47}
48
49impl<'a> Parse<'a> for ColorStripe<'a> {
50	fn parse<I>(p: &mut Parser<'a, I>) -> ParserResult<Self>
51	where
52		I: Iterator<Item = Cursor> + Clone,
53	{
54		let mut color = p.parse_if_peek::<Color>()?;
55		let thickness = p.parse_if_peek::<LengthPercentageOrFlex>()?;
56		if color.is_none() {
57			color = Some(p.parse::<Color>()?);
58		}
59		Ok(Self { color: color.unwrap(), thickness })
60	}
61}
62
63#[cfg(test)]
64mod tests {
65	use super::*;
66	use crate::CssAtomSet;
67	use css_parse::assert_parse;
68
69	#[test]
70	fn size_test() {
71		assert_eq!(std::mem::size_of::<StripesFunction>(), 56);
72		assert_eq!(std::mem::size_of::<ColorStripe>(), 40);
73	}
74
75	#[test]
76	fn test_writes() {
77		assert_parse!(CssAtomSet::ATOMS, StripesFunction, "stripes(red 1fr,green 2fr,blue 100px)");
78		assert_parse!(CssAtomSet::ATOMS, StripesFunction, "stripes(0.1fr red,0.2fr green,100px blue)");
79		assert_parse!(CssAtomSet::ATOMS, StripesFunction, "stripes(red 1fr,2fr green,blue 100px)");
80	}
81}