css_parse/syntax/
no_prelude_allowed.rs

1use crate::{Cursor, CursorSink, Parse, Parser, Peek, Result, Span, T, ToCursors, ToSpan, diagnostics};
2
3/// A struct to provide to [AtRule][crate::AtRule] to disallow preludes.
4///
5/// Sometimes [AtRules][crate::syntax::AtRule] do not have a prelude. In those case, assigning this struct to the
6/// `Prelude` can be useful to ensure that the [AtRule][crate::syntax::AtRule] appropriately errors if it enters the
7/// Prelude parsing context.
8#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
10pub struct NoPreludeAllowed;
11
12impl<'a> Parse<'a> for NoPreludeAllowed {
13	fn parse(p: &mut Parser<'a>) -> Result<Self> {
14		if p.peek::<T![LeftCurly]>() || p.peek::<T![;]>() {
15			Ok(Self {})
16		} else {
17			Err(diagnostics::Unexpected(p.next()))?
18		}
19	}
20}
21
22impl<'a> Peek<'a> for NoPreludeAllowed {
23	fn peek(_: &Parser<'a>, _: Cursor) -> bool {
24		false
25	}
26}
27
28impl ToCursors for NoPreludeAllowed {
29	fn to_cursors(&self, _: &mut impl CursorSink) {
30		// No cursors
31	}
32}
33
34impl ToSpan for NoPreludeAllowed {
35	fn to_span(&self) -> Span {
36		Span::ZERO
37	}
38}