dioxus_style_macro 0.5.2

Procedural macros for dioxus_style with SCSS/SASS compilation support - internal use only
Documentation
//! Helpers for generating scoped CSS class names and token streams
//!
//! This module provides utilities for creating the Rust code that represents
//! scoped CSS classes in the generated macro output.

use quote::quote;
use proc_macro2::TokenStream;
use syn::Ident;

use crate::class_extractor::SelectorInfo;

/// Generates a TokenStream for a scoped CSS class constant
///
/// # Arguments
/// * `selector_info` - Information about the CSS selector
/// * `scope_hash` - The unique scope hash for this CSS
///
/// # Returns
/// A TokenStream representing the constant definition
pub fn generate_class_constant(
    selector_info: &SelectorInfo,
    scope_hash: &str,
) -> TokenStream {
    let rust_ident = selector_info.name();
    let ident = Ident::new(rust_ident, proc_macro2::Span::call_site());
    
    match selector_info {
        SelectorInfo::Id(id_name) => {
            // For IDs, return the actual ID string (without scoping)
            let actual_id = id_name.replace('_', "-");
            quote! {
                #[allow(non_upper_case_globals)]
                pub const #ident: &'static str = #actual_id;
            }
        }
        SelectorInfo::Element(_) | SelectorInfo::Pseudo(_) | SelectorInfo::Class(_) => {
            // For elements, pseudo-classes, and regular classes, generate scoped CssClass
            let css_class_hyphen = rust_ident.replace('_', "-");
            let scoped_class_name = format!("{}_{}", scope_hash, css_class_hyphen);
            quote! {
                #[allow(non_upper_case_globals)]
                pub const #ident: ::dioxus_style::CssClass = ::dioxus_style::CssClass::new(#scoped_class_name);
            }
        }
    }
}

/// Generates a namespace module with CSS class constants
///
/// # Arguments
/// * `namespace_name` - Name of the module to generate
/// * `scope_hash` - Unique scope identifier
/// * `scoped_css` - The scoped CSS content
/// * `class_constants` - TokenStreams for class constant definitions
/// * `include_quote` - Optional include_str! directive for file tracking
///
/// # Returns
/// A TokenStream representing the complete namespace module
pub fn generate_namespace_module(
    namespace_name: &Ident,
    scope_hash: &str,
    scoped_css: &str,
    class_constants: &[TokenStream],
    include_quote: &TokenStream,
) -> TokenStream {
    quote! {
        mod #namespace_name {
            use ::std::sync::OnceLock;
            static STYLE_INSTANCE: OnceLock<::dioxus_style::ScopedStyle> = OnceLock::new();
            
            #[allow(dead_code)]
            pub fn get_scope() -> &'static ::dioxus_style::ScopedStyle {
                STYLE_INSTANCE.get_or_init(|| {
                    #include_quote
                    ::dioxus_style::ScopedStyle::new(#scope_hash, #scoped_css)
                })
            }
            
            #(#class_constants)*
        }
    }
}

/// Generates an include_str! directive for file dependency tracking
///
/// # Arguments
/// * `include_path` - Optional path to the CSS/SCSS file
///
/// # Returns
/// A TokenStream with the include_str! directive, or empty if no path
pub fn generate_include_quote(include_path: Option<String>) -> TokenStream {
    match include_path {
        Some(p) => quote! { let _css_tracker = include_str!(#p); },
        None => quote! {}
    }
}