Macro boolean_feature

Source
macro_rules! boolean_feature {
    ($(#[$meta:meta])* $vis:vis enum $feature: ident{$feature_name: path}) => { ... };
}
Expand description

This macro expands to define an enum which already implements Parse and BooleanFeature, for a one-liner definition of a BooleanFeature.

ยงExample

use css_lexer::*;
use css_parse::*;
use csskit_derives::*;
use derive_atom_set::*;
use bumpalo::Bump;

#[derive(Debug, Default, AtomSet, Copy, Clone, PartialEq)]
pub enum MyAtomSet {
  #[default]
  _None,
  TestFeature,
}
impl MyAtomSet {
  const ATOMS: MyAtomSet = MyAtomSet::_None;
}

// Define the Boolean Feature.
boolean_feature! {
    /// A boolean media feature: `(test-feature)`
    #[derive(ToCursors, ToSpan, Debug)]
    pub enum TestFeature{MyAtomSet::TestFeature}
}

// Test!
let allocator = Bump::new();
let source_text = "(test-feature)";
let lexer = Lexer::new( &MyAtomSet::ATOMS, &source_text);
let mut p = Parser::new(&allocator, &source_text, lexer);
let result = p.parse_entirely::<TestFeature>();
assert!(matches!(result.output, Some(TestFeature::Bare(open, ident, close))));

let source_text = "(test-feature: none)";
let lexer = Lexer::new(&MyAtomSet::ATOMS, &source_text);
let mut p = Parser::new(&allocator, &source_text, lexer);
let result = p.parse_entirely::<TestFeature>();
assert!(matches!(result.output, Some(TestFeature::WithValue(open, ident, colon, any, close))));