dioxus_style_macro 0.5.0

Procedural macros for dioxus_style with SCSS/SASS compilation support - internal use only
Documentation
//! Shared parsing utilities for CSS/SCSS content
//!
//! This module provides reusable parsing functions for extracting information
//! from CSS and SCSS content.

use std::iter::Peekable;
use std::str::Chars;

/// Extracts a CSS/SCSS identifier from a character stream
///
/// An identifier can contain alphanumeric characters, hyphens, and underscores.
/// 
/// # Arguments
/// * `chars` - Peekable character iterator
///
/// # Returns
/// The extracted identifier, or None if no valid identifier found
pub fn extract_identifier(chars: &mut Peekable<Chars>) -> Option<String> {
    let mut name = String::new();
    
    while let Some(&ch) = chars.peek() {
        if ch.is_alphanumeric() || ch == '-' || ch == '_' {
            name.push(chars.next().unwrap());
        } else {
            break;
        }
    }
    
    if name.is_empty() {
        None
    } else {
        Some(name)
    }
}

/// Skips a string literal in CSS/SCSS
///
/// Properly handles escaped quotes within the string.
///
/// # Arguments
/// * `quote` - The quote character (' or ")
/// * `chars` - Peekable character iterator
pub fn skip_string_literal(quote: char, chars: &mut Peekable<Chars>) {
    while let Some(c) = chars.next() {
        if c == quote {
            break;
        }
        if c == '\\' {
            chars.next(); // Skip escaped character
        }
    }
}

/// Skips a CSS comment (/* ... */)
///
/// # Arguments
/// * `chars` - Peekable character iterator
pub fn skip_comment(chars: &mut Peekable<Chars>) {
    chars.next(); // consume '*' after '/'
    while let Some(c) = chars.next() {
        if c == '*' && chars.peek() == Some(&'/') {
            chars.next(); // consume '/'
            break;
        }
    }
}

/// Checks if a '#' followed by characters is a color code
///
/// Detects patterns like #fff, #ffffff, #rrggbbaa
///
/// # Arguments
/// * `chars` - Peekable character iterator (cloned for lookahead)
///
/// # Returns
/// true if it's a valid color code pattern
pub fn is_color_code(chars: &mut Peekable<Chars>) -> bool {
    let mut count = 0;
    for ch in chars.clone() {
        if ch.is_ascii_hexdigit() {
            count += 1;
        } else {
            break;
        }
    }
    // Valid color codes: #RGB (3), #RRGGBB (6), #RRGGBBAA (8)
    count == 3 || count == 6 || count == 8
}

/// Converts a CSS identifier to a valid Rust identifier
///
/// # Rules
/// - Hyphens are converted to underscores
/// - If the name starts with a digit, prefix with underscore
/// - Invalid characters are replaced with underscores
///
/// # Examples
/// ```
/// use dioxus_style_macro::parsing_utils::css_to_rust_ident;
/// 
/// assert_eq!(css_to_rust_ident("btn-primary"), "btn_primary");
/// assert_eq!(css_to_rust_ident("123-start"), "_123_start");
/// assert_eq!(css_to_rust_ident("my@class"), "my_class");
/// ```
pub fn css_to_rust_ident(name: &str) -> String {
    let mut result = String::with_capacity(name.len() + 1); // Pre-allocate
    
    for (i, ch) in name.chars().enumerate() {
        if i == 0 {
            // First character: must be letter or underscore
            if ch.is_ascii_digit() {
                result.push('_');
                result.push(ch);
            } else if ch.is_alphanumeric() || ch == '_' {
                result.push(ch);
            } else if ch == '-' {
                result.push('_');
            } else {
                result.push('_');
            }
        } else {
            // Subsequent characters
            if ch.is_alphanumeric() || ch == '_' {
                result.push(ch);
            } else {
                // Replace hyphens and invalid characters with underscore
                result.push('_');
            }
        }
    }
    
    // Ensure we have a valid identifier
    if result.is_empty() {
        result.push('_');
    }
    
    result
}