Skip to main content

AtomSet

Derive Macro AtomSet 

Source
#[derive(AtomSet)]
{
    // Attributes available to this derive:
    #[default]
    #[atom]
}
Expand description

Derives an efficient AtomSet implementation (the trait from the atom_set crate) for interned identifiers.

This proc macro automatically generates optimized string-to-enum matching code.

§Variant Attributes

  • #[default]: Marks this variant as the empty/fallback value (returns empty string)
  • #[atom("custom")]: Overrides the default string representation

§Naming Convention

If #[atom("")] is not provided a string is derived from the variant name:

  • Px"px"
  • FontSize"font-size"
  • WebkitTransform"webkit-transform"

§Example

use atom_set::AtomSet;
use derive_atom_set::AtomSet as DeriveAtomSet;

#[derive(Debug, Default, Copy, Clone, PartialEq, DeriveAtomSet)]
pub enum MyAtomSet {
    #[default]
    Unknown, // Must provide an empty default!

    // Absolute units
    Px, Pt, Pc, In, Cm, Mm, Q,

    // Relative units
    Em, Ex, Ch, Rem, Lh,

    // Viewport units
    Vw, Vh, Vi, Vb, Vmin, Vmax,

    // Container query units
    Cqw, Cqh, Cqi, Cqb, Cqmin, Cqmax,

    // Special case
    #[atom("%")]
    Percent,
}

// Usage:
assert_eq!(MyAtomSet::from_str("px"), MyAtomSet::Px);
assert_eq!(MyAtomSet::from_str("PX"), MyAtomSet::Px);  // Case insensitive matches
assert_eq!(MyAtomSet::Px.to_str(), "px");
assert_eq!(MyAtomSet::Percent.to_str(), "%");
assert_eq!(MyAtomSet::from_str("unknown"), MyAtomSet::Unknown);