css_parse/syntax/
function_block.rs

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