Skip to main content

css_parse/syntax/
simple_block.rs

1use crate::{
2	CursorSink, KindSet, Parse, Parser, Peek, Result as ParserResult, SemanticEq, Span, T, ToCursors, ToSpan,
3	syntax::ComponentValues,
4};
5
6#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
8pub struct SimpleBlock<'a> {
9	pub open: T![PairWiseStart],
10	pub values: ComponentValues<'a>,
11	pub close: Option<T![PairWiseEnd]>,
12}
13
14impl<'a> Peek<'a> for SimpleBlock<'a> {
15	const PEEK_KINDSET: KindSet = KindSet::PAIRWISE_START;
16}
17
18// https://drafts.csswg.org/css-syntax-3/#consume-a-simple-block
19impl<'a> Parse<'a> for SimpleBlock<'a> {
20	fn parse<Iter>(p: &mut Parser<'a, Iter>) -> ParserResult<Self>
21	where
22		Iter: Iterator<Item = crate::Cursor> + Clone,
23	{
24		let open = p.parse::<T![PairWiseStart]>()?;
25		let stop = p.set_stop(KindSet::new(&[open.end()]));
26		let values = p.parse::<ComponentValues>();
27		p.set_stop(stop);
28		let values = values?;
29		if <T![PairWiseEnd]>::peek(p, p.peek_n(1)) {
30			return Ok(Self { open, values, close: p.parse::<T![PairWiseEnd]>().ok() });
31		}
32		Ok(Self { open, values, close: None })
33	}
34}
35
36impl<'a> ToCursors for SimpleBlock<'a> {
37	fn to_cursors(&self, s: &mut impl CursorSink) {
38		ToCursors::to_cursors(&self.open, s);
39		ToCursors::to_cursors(&self.values, s);
40		ToCursors::to_cursors(&self.close, s);
41	}
42}
43
44impl<'a> ToSpan for SimpleBlock<'a> {
45	fn to_span(&self) -> Span {
46		self.open.to_span() + if let Some(close) = self.close { close.to_span() } else { self.values.to_span() }
47	}
48}
49
50impl<'a> SemanticEq for SimpleBlock<'a> {
51	fn semantic_eq(&self, other: &Self) -> bool {
52		self.open.semantic_eq(&other.open)
53			&& self.values.semantic_eq(&other.values)
54			&& self.close.semantic_eq(&other.close)
55	}
56}
57
58#[cfg(test)]
59mod tests {
60	use super::*;
61	use crate::EmptyAtomSet;
62	use crate::test_helpers::*;
63
64	#[test]
65	fn size_test() {
66		assert_eq!(std::mem::size_of::<SimpleBlock>(), 64);
67	}
68
69	#[test]
70	fn test_writes() {
71		assert_parse!(EmptyAtomSet::ATOMS, SimpleBlock, "[foo]");
72		assert_parse!(EmptyAtomSet::ATOMS, SimpleBlock, "(one two three)");
73		assert_parse!(EmptyAtomSet::ATOMS, SimpleBlock, "{}");
74		assert_parse!(EmptyAtomSet::ATOMS, SimpleBlock, "{foo}");
75		assert_parse!(EmptyAtomSet::ATOMS, SimpleBlock, "{foo:bar}");
76		assert_parse!(EmptyAtomSet::ATOMS, SimpleBlock, "{one(two)}");
77		assert_parse!(EmptyAtomSet::ATOMS, SimpleBlock, "(one(two))");
78		// Incomplete but recoverable
79		assert_parse!(EmptyAtomSet::ATOMS, SimpleBlock, "[foo");
80		assert_parse!(EmptyAtomSet::ATOMS, SimpleBlock, "{foo:bar");
81		assert_parse!(EmptyAtomSet::ATOMS, SimpleBlock, "(one(two)");
82		// assert_parse!(EmptyAtomSet::ATOMS, SimpleBlock, "(one(two");
83	}
84
85	#[test]
86	fn test_peek() {
87		assert_peek_false!(EmptyAtomSet::ATOMS, SimpleBlock, "foo");
88		assert_peek_false!(EmptyAtomSet::ATOMS, SimpleBlock, "");
89	}
90}