dioxus_style_macro 0.5.3

Procedural macros for dioxus_style with SCSS/SASS compilation support - internal use only
Documentation
//! CSS selector extraction for namespace module generation.
//!
//! This module parses CSS/SCSS and extracts selectors (classes, IDs, elements, pseudo-classes)
//! to generate Rust constants for the namespace syntax feature.

use std::collections::HashSet;
use crate::css_constants::{is_valid_html_element, is_valid_pseudo_class};
use crate::parsing_utils::{extract_identifier, skip_string_literal, skip_comment, is_color_code, css_to_rust_ident};

/// Information about a CSS selector
#[derive(Debug, Clone)]
pub enum SelectorInfo {
    Class(String),      // Regular class selector
    Id(String),         // ID selector
    Element(String),    // Element selector
    Pseudo(String),     // Pseudo-class selector
}

impl SelectorInfo {
    pub fn name(&self) -> &str {
        match self {
            SelectorInfo::Class(n) => n,
            SelectorInfo::Id(n) => n,
            SelectorInfo::Element(n) => n,
            SelectorInfo::Pseudo(n) => n,
        }
    }
}

/// Main function to extract all CSS selectors from CSS/SCSS content
pub fn extract_class_names(css: &str) -> Vec<SelectorInfo> {
    let mut classes = HashSet::new();
    let mut ids = HashSet::new();
    let mut elements = HashSet::new();
    let mut pseudo_classes = HashSet::new();
    
    let mut chars = css.chars().peekable();
    
    while let Some(ch) = chars.next() {
        match ch {
            // Class selector: .class-name
            '.' => {
                if let Some(name) = extract_identifier(&mut chars) {
                    classes.insert(css_to_rust_ident(&name));
                }
            }
            
            // ID selector: #id-name
            '#' => {
                // Check if it's not a color code
                if !is_color_code(&mut chars.clone()) {
                    if let Some(name) = extract_identifier(&mut chars) {
                        ids.insert(css_to_rust_ident(&name));
                    }
                }
            }
            
            // Pseudo-class: :hover, :active, etc.
            ':' => {
                // Skip :: (pseudo-elements like ::before)
                if chars.peek() != Some(&':') {
                    if let Some(name) = extract_identifier(&mut chars) {
                        // Common pseudo-classes
                        if is_valid_pseudo_class(&name) {
                            pseudo_classes.insert(css_to_rust_ident(&name));
                        }
                    }
                } else {
                    chars.next(); // Skip second ':'
                }
            }
            
            // String literals - skip
            '"' | '\'' => {
                skip_string_literal(ch, &mut chars);
            }
            
            // Comments - skip
            '/' if chars.peek() == Some(&'*') => {
                skip_comment(&mut chars);
            }
            
            // Potential element selector
            _ if ch.is_alphabetic() => {
                let mut element_name = String::new();
                element_name.push(ch);
                
                // Read full element name
                while let Some(&next_ch) = chars.peek() {
                    if next_ch.is_alphanumeric() || next_ch == '-' {
                        element_name.push(chars.next().unwrap());
                    } else {
                        break;
                    }
                }
                
                // Check if followed by { or space (likely element selector)
                let next = chars.peek();
                if next == Some(&'{') || next == Some(&' ') || next == Some(&'.') || 
                   next == Some(&'#') || next == Some(&':') {
                    if is_valid_html_element(&element_name) {
                        elements.insert(css_to_rust_ident(&element_name));
                    }
                }
            }
            
            _ => {}
        }
    }
    
    let mut all_selectors = Vec::new();
    
    // Classes (no prefix)
    for class in sorted_vec(classes) {
        all_selectors.push(SelectorInfo::Class(class));
    }
    
    // IDs (no prefix)
    for id in sorted_vec(ids) {
        all_selectors.push(SelectorInfo::Id(id));
    }
    
    // Elements (with el_ prefix in name)
    for element in sorted_vec(elements) {
        all_selectors.push(SelectorInfo::Element(format!("el_{}", element)));
    }
    
    // Pseudo-classes (with pseudo_ prefix in name)
    for pseudo in sorted_vec(pseudo_classes) {
        all_selectors.push(SelectorInfo::Pseudo(format!("pseudo_{}", pseudo)));
    }
    
    all_selectors
}

// All parsing utilities have been moved to shared modules:

/// Convert HashSet to sorted Vec
fn sorted_vec(set: HashSet<String>) -> Vec<String> {
    let mut vec: Vec<String> = set.into_iter().collect();
    vec.sort();
    vec
}

// css_to_rust_ident has been moved to parsing_utils module