Skip to main content

css_parse/syntax/
function_block.rs

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