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