css_parse/syntax/
function_block.rs

1use crate::{ComponentValues, CursorSink, Parse, Parser, Result as ParserResult, Span, T, ToCursors, ToSpan};
2
3#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
4#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
5pub struct FunctionBlock<'a> {
6	name: T![Function],
7	params: ComponentValues<'a>,
8	close: T![')'],
9}
10
11// https://drafts.csswg.org/css-syntax-3/#consume-function
12impl<'a> Parse<'a> for FunctionBlock<'a> {
13	fn parse(p: &mut Parser<'a>) -> ParserResult<Self> {
14		let name = p.parse::<T![Function]>()?;
15		let params = p.parse::<ComponentValues>()?;
16		let close = p.parse::<T![')']>()?;
17		Ok(Self { name, params, close })
18	}
19}
20
21impl<'a> ToCursors for FunctionBlock<'a> {
22	fn to_cursors(&self, s: &mut impl CursorSink) {
23		ToCursors::to_cursors(&self.name, s);
24		ToCursors::to_cursors(&self.params, s);
25		ToCursors::to_cursors(&self.close, s);
26	}
27}
28
29impl<'a> ToSpan for FunctionBlock<'a> {
30	fn to_span(&self) -> Span {
31		self.name.to_span() + self.close.to_span()
32	}
33}
34
35#[cfg(test)]
36mod tests {
37	use super::*;
38	use crate::EmptyAtomSet;
39	use crate::test_helpers::*;
40
41	#[test]
42	fn size_test() {
43		assert_eq!(std::mem::size_of::<FunctionBlock>(), 56);
44	}
45
46	#[test]
47	fn test_writes() {
48		assert_parse!(EmptyAtomSet::ATOMS, FunctionBlock, "foo(bar)");
49		assert_parse!(EmptyAtomSet::ATOMS, FunctionBlock, "foo(bar{})");
50	}
51}