css_ast/functions/
stripes_function.rs1use super::prelude::*;
2use crate::{types::Color, units::LengthPercentageOrFlex};
3
4#[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>,
19 #[cfg_attr(feature = "visitable", visit(skip))]
20 pub close: T![')'],
21}
22
23#[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 {
33 pub color: Color,
34 pub thickness: Option<LengthPercentageOrFlex>,
35}
36
37impl<'a> Peek<'a> for ColorStripe {
38 fn peek<I>(p: &Parser<'a, I>, c: Cursor) -> bool
39 where
40 I: Iterator<Item = Cursor> + Clone,
41 {
42 Color::peek(p, c) || LengthPercentageOrFlex::peek(p, c)
43 }
44}
45
46impl<'a> Parse<'a> for ColorStripe {
47 fn parse<I>(p: &mut Parser<'a, I>) -> ParserResult<Self>
48 where
49 I: Iterator<Item = Cursor> + Clone,
50 {
51 let mut color = p.parse_if_peek::<Color>()?;
52 let thickness = p.parse_if_peek::<LengthPercentageOrFlex>()?;
53 if color.is_none() {
54 color = Some(p.parse::<Color>()?);
55 }
56 Ok(Self { color: color.unwrap(), thickness })
57 }
58}
59
60#[cfg(test)]
61mod tests {
62 use super::*;
63 use crate::CssAtomSet;
64 use css_parse::assert_parse;
65
66 #[test]
67 fn size_test() {
68 assert_eq!(std::mem::size_of::<StripesFunction>(), 56);
69 assert_eq!(std::mem::size_of::<ColorStripe>(), 156);
70 }
71
72 #[test]
73 fn test_writes() {
74 assert_parse!(CssAtomSet::ATOMS, StripesFunction, "stripes(red 1fr,green 2fr,blue 100px)");
75 assert_parse!(CssAtomSet::ATOMS, StripesFunction, "stripes(0.1fr red,0.2fr green,100px blue)");
76 assert_parse!(CssAtomSet::ATOMS, StripesFunction, "stripes(red 1fr,2fr green,blue 100px)");
77 }
78}