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/// ```
14/// use css_lexer::*;
15/// let mut lexer = Lexer::new(".");
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	None = 0b000,
27	/// If the token before this one is not whitespace, then whitespace must be placed before this token.
28	EnforceBefore = 0b100,
29	/// The token must produce a whitespace token to separate it and the next token (if the next token is not already
30	/// whitespace).
31	EnforceAfter = 0b010,
32	/// The token after this one must not be whitespace, doing so would result in breaking a higher level association with
33	/// the adjacent token (for example a pseudo class such as `:hover`).
34	BanAfter = 0b001,
35}
36
37impl AssociatedWhitespaceRules {
38	pub(crate) const fn from_bits(bits: u8) -> Self {
39		Self { bits: bits & 0b111 }
40	}
41
42	pub(crate) const fn to_bits(self) -> u8 {
43		self.bits
44	}
45}
46
47#[test]
48fn size_test() {
49	assert_eq!(::std::mem::size_of::<AssociatedWhitespaceRules>(), 1);
50}
51
52#[test]
53fn test_from_bits() {
54	assert!(
55		AssociatedWhitespaceRules::from_bits(AssociatedWhitespaceRules::EnforceBefore.bits)
56			.contains(AssociatedWhitespaceRules::EnforceBefore)
57	);
58	assert!(
59		AssociatedWhitespaceRules::from_bits(AssociatedWhitespaceRules::EnforceAfter.bits)
60			.contains(AssociatedWhitespaceRules::EnforceAfter)
61	);
62	assert!(
63		AssociatedWhitespaceRules::from_bits(AssociatedWhitespaceRules::BanAfter.bits)
64			.contains(AssociatedWhitespaceRules::BanAfter)
65	);
66}