derive

Function derive 

Source
pub fn derive(input: DeriveInput) -> TokenStream
Expand description

Derive implementation of the ToTyped trait.

This derive supports both enums and structs:

  • Enums

    • Pure keyword enums: Enums made up entirely of unit variants (e.g. enum Visibility { Visible, Hidden, Collapse }). In this case, the derive optimizes by delegating to ToCss once for the whole value, then wrapping the result in TypedValue::Keyword.

    • Mixed enums with unit variants: Enums that contain both unit variants (keywords) and data-carrying variants. The derive generates a match implementation where unit variants are reified as TypedValue::Keyword, and fielded variants may be reified by calling .to_typed() on their inner values when the derive_fields attribute is enabled. Otherwise, those variants return None.

  • Structs

    • Structs are handled similarly to data-carrying variants in mixed enums. When derive_fields is enabled and the type is not marked as a bitflags type, .to_typed() may be generated for their inner value; otherwise, they return None.

Unit variants are mapped to keywords using their Rust identifier converted via to_css_identifier. Attributes like #[css(keyword = "...")] are not yet supported in this path (see bug 1995187).

For other kinds of types (e.g. unions), no to_typed method is generated; the default implementation applies, which always returns None.

This allows keywords to be reified automatically into CSSKeywordValue objects, while leaving more complex value types to be implemented incrementally as Typed OM support expands.