macro_rules! pseudo_class {
($(#[$meta:meta])* $vis:vis enum $name: ident { $first_variant: ident: $atoms: ident::$first:ident, $( $variant: ident: $variant_pat: pat$(,)?)* }) => { ... };
}Expand description
A macro for defining pseudo classes.
This makes it much easier to define a pseudo class. Parsing is also a little bit delicate, as the two Cursors must appear next to each other - no whitespace nor comments can be present betwixt the colon and ident.
ยงExample
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,
Foo,
Bar,
Baz,
}
impl MyAtomSet {
const ATOMS: MyAtomSet = MyAtomSet::_None;
}
pseudo_class!(
/// Some docs on this type...
#[derive(Debug, ToCursors, ToSpan)]
pub enum MyPseudoClass {
Foo: MyAtomSet::Foo,
Bar: MyAtomSet::Bar,
Baz: MyAtomSet::Baz,
}
);
// The result will be one of the variants in the enum, matching the keyword.
assert_parse!(MyAtomSet::ATOMS, MyPseudoClass, ":foo");
// Matches are case insensitive
assert_parse!(MyAtomSet::ATOMS, MyPseudoClass, ":BaR");
// Words that do not match will fail to parse.
assert_parse_error!(MyAtomSet::ATOMS, MyPseudoClass, ":bing");
// The `:` is also required
assert_parse_error!(MyAtomSet::ATOMS, MyPseudoClass, "baz");
// Any tokens between the `:` and ident result in a parse error:
assert_parse_error!(MyAtomSet::ATOMS, MyPseudoClass, ": foo");