Home / Function/ classify_bytes_derive() — tailwindcss Function Reference

classify_bytes_derive() — tailwindcss Function Reference

Architecture documentation for the classify_bytes_derive() function in lib.rs from the tailwindcss codebase.

Entity Profile

Dependency Diagram

graph TD
  47350b78_d14a_2c8b_c341_8605fd90c62a["classify_bytes_derive()"]
  8866d709_3b59_d730_edfc_80aef369b653["lib.rs"]
  47350b78_d14a_2c8b_c341_8605fd90c62a -->|defined in| 8866d709_3b59_d730_edfc_80aef369b653
  79dc1d67_b42f_0457_02ff_31f55f19bbae["has_fallback_attr()"]
  47350b78_d14a_2c8b_c341_8605fd90c62a -->|calls| 79dc1d67_b42f_0457_02ff_31f55f19bbae
  5d67b63a_7932_6cc1_ba9d_6cfe96eb45e5["get_bytes_attrs()"]
  47350b78_d14a_2c8b_c341_8605fd90c62a -->|calls| 5d67b63a_7932_6cc1_ba9d_6cfe96eb45e5
  07ac266e_f8b4_f731_cd1b_5199f8df45fe["get_bytes_range_attrs()"]
  47350b78_d14a_2c8b_c341_8605fd90c62a -->|calls| 07ac266e_f8b4_f731_cd1b_5199f8df45fe
  style 47350b78_d14a_2c8b_c341_8605fd90c62a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

crates/classification-macros/src/lib.rs lines 33–119

pub fn classify_bytes_derive(input: TokenStream) -> TokenStream {
    let ast = parse_macro_input!(input as DeriveInput);

    // This derive only works on an enum
    let Data::Enum(DataEnum { variants, .. }) = &ast.data else {
        return syn::Error::new_spanned(
            &ast.ident,
            "ClassifyBytes can only be derived on an enum.",
        )
        .to_compile_error()
        .into();
    };

    let enum_name = &ast.ident;

    let mut byte_map: [Option<Ident>; 256] = [const { None }; 256];
    let mut fallback_variant: Option<Ident> = None;

    // Start parsing the variants
    for variant in variants {
        let variant_ident = &variant.ident;

        // If this variant has #[fallback], record it
        if has_fallback_attr(variant) {
            if fallback_variant.is_some() {
                let err = syn::Error::new_spanned(
                    variant_ident,
                    "Multiple variants have #[fallback]. Only one allowed.",
                );
                return err.to_compile_error().into();
            }
            fallback_variant = Some(variant_ident.clone());
        }

        // Get #[bytes(…)]
        let single_bytes = get_bytes_attrs(&variant.attrs);

        // Get #[bytes_range(…)]
        let range_bytes = get_bytes_range_attrs(&variant.attrs);

        // Combine them
        let all_bytes = single_bytes
            .into_iter()
            .chain(range_bytes)
            .collect::<Vec<_>>();

        // Mark them in the table
        for b in all_bytes {
            byte_map[b as usize] = Some(variant_ident.clone());
        }
    }

    // If no fallback variant is found, default to "Other"
    let fallback_ident = fallback_variant.expect("A variant marked with #[fallback] is missing");

    // For each of the 256 byte values, fill the table
    let fill = byte_map
        .clone()
        .into_iter()
        .map(|variant_opt| match variant_opt {
            Some(ident) => quote!(#enum_name::#ident),
            None => quote!(#enum_name::#fallback_ident),
        });

    // Generate the final expanded code
    let expanded = quote! {
        impl #enum_name {
            pub const TABLE: [#enum_name; 256] = [
                #(#fill),*
            ];
        }

        impl From<u8> for #enum_name {
            fn from(byte: u8) -> Self {
                #enum_name::TABLE[byte as usize]
            }
        }

        impl From<&u8> for #enum_name {
            fn from(byte: &u8) -> Self {
                #enum_name::TABLE[*byte as usize]

Domain

Subdomains

Frequently Asked Questions

What does classify_bytes_derive() do?
classify_bytes_derive() is a function in the tailwindcss codebase, defined in crates/classification-macros/src/lib.rs.
Where is classify_bytes_derive() defined?
classify_bytes_derive() is defined in crates/classification-macros/src/lib.rs at line 33.
What does classify_bytes_derive() call?
classify_bytes_derive() calls 3 function(s): get_bytes_attrs, get_bytes_range_attrs, has_fallback_attr.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free