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 const NONE: AssociatedWhitespaceRules = AssociatedWhitespaceRules::none();
38
39 pub(crate) const fn from_bits(bits: u8) -> Self {
40 Self { bits: bits & 0b111 }
41 }
42
43 pub(crate) const fn to_bits(self) -> u8 {
44 self.bits
45 }
46}
47
48#[test]
49fn size_test() {
50 assert_eq!(::std::mem::size_of::<AssociatedWhitespaceRules>(), 1);
51}
52
53#[test]
54fn test_from_bits() {
55 assert!(
56 AssociatedWhitespaceRules::from_bits(AssociatedWhitespaceRules::EnforceBefore.bits)
57 .contains(AssociatedWhitespaceRules::EnforceBefore)
58 );
59 assert!(
60 AssociatedWhitespaceRules::from_bits(AssociatedWhitespaceRules::EnforceAfter.bits)
61 .contains(AssociatedWhitespaceRules::EnforceAfter)
62 );
63 assert!(
64 AssociatedWhitespaceRules::from_bits(AssociatedWhitespaceRules::BanAfter.bits)
65 .contains(AssociatedWhitespaceRules::BanAfter)
66 );
67}