css_lexer/
associated_whitespace_rules.rs

1use bitmask_enum::bitmask;
2
3/// A [bitmask][bitmask_enum] representing rules around the whitespace surrounding a [Kind::Delim][crate::Kind::Delim]
4/// token.
5///
6/// A [Token][crate::Token] with [Kind::Delim][crate::Kind::Delim] or one of the other single character tokens (such as
7/// [Kind::LeftCurly][crate::Kind::LeftCurly] will store this data internal to the token. Using
8/// [Token::associated_whitespace()][crate::Token::associated_whitespace()] will return this bitmask, depending on what
9/// rules are set for this token. By default the [Lexer][crate::Lexer] will produce tokens with
10/// [AssociatedWhitespaceRules::none()], but new tokens can be created which can be accompanied with a different set of
11/// rules.
12///
13/// ```rust
14/// use css_lexer::*;
15/// let mut lexer = Lexer::new(&EmptyAtomSet::ATOMS, ".");
16/// {
17///		// This token will be a Delim of `.`
18///		let token = lexer.advance();
19///		assert_eq!(token, Kind::Delim);
20///		assert_eq!(token, AssociatedWhitespaceRules::none());
21/// }
22/// ```
23#[bitmask(u8)]
24#[bitmask_config(vec_debug)]
25pub enum AssociatedWhitespaceRules {
26	/// If the token before this one is not whitespace, then whitespace must be placed before this token.
27	EnforceBefore = 0b100,
28	/// The token must produce a whitespace token to separate it and the next token (if the next token is not already
29	/// whitespace).
30	EnforceAfter = 0b010,
31	/// The token after this one must not be whitespace, doing so would result in breaking a higher level association with
32	/// the adjacent token (for example a pseudo class such as `:hover`).
33	BanAfter = 0b001,
34}
35
36impl AssociatedWhitespaceRules {
37	pub(crate) const fn from_bits(bits: u8) -> Self {
38		Self { bits: bits & 0b111 }
39	}
40
41	pub(crate) const fn to_bits(self) -> u8 {
42		self.bits
43	}
44}
45
46#[test]
47fn size_test() {
48	assert_eq!(::std::mem::size_of::<AssociatedWhitespaceRules>(), 1);
49}
50
51#[test]
52fn test_from_bits() {
53	assert!(
54		AssociatedWhitespaceRules::from_bits(AssociatedWhitespaceRules::EnforceBefore.bits)
55			.contains(AssociatedWhitespaceRules::EnforceBefore)
56	);
57	assert!(
58		AssociatedWhitespaceRules::from_bits(AssociatedWhitespaceRules::EnforceAfter.bits)
59			.contains(AssociatedWhitespaceRules::EnforceAfter)
60	);
61	assert!(
62		AssociatedWhitespaceRules::from_bits(AssociatedWhitespaceRules::BanAfter.bits)
63			.contains(AssociatedWhitespaceRules::BanAfter)
64	);
65}